Version 1
[yaffs-website] / vendor / drush / drush / commands / core / field.drush.inc
1 <?php
2
3 /**
4  * @file
5  *  Field API's drush integration
6  */
7
8 /**
9  * Implementation of hook_drush_help().
10  */
11 function field_drush_help($section) {
12   switch ($section) {
13     case 'meta:field:title':
14       return dt('Field commands');
15     case 'meta:field:summary':
16       return dt('Manipulate Drupal 7+ fields.');
17   }
18 }
19
20 /**
21  * Implementation of hook_drush_command().
22  */
23 function field_drush_command() {
24   $items['field-create'] = array(
25     'description' => 'Create fields and instances. Returns urls for field editing.',
26     'core' => array('7+'),
27     'arguments' => array(
28       'bundle' => 'Content type (for nodes). Name of bundle to attach fields to. Required.',
29       'field_spec' => 'Comma delimited triple in the form: field_name,field_type,widget_name. If widget_name is omitted, the default widget will be used. Separate multiple fields by space. If omitted, a wizard will prompt you.'
30     ),
31     'required-arguments' => 1,
32     'options' => array(
33       'entity_type' => 'Type of entity (e.g. node, user, comment). Defaults to node.',
34     ),
35     'examples' => array(
36       'drush field-create article' => 'Define new article fields via interactive prompts.',
37       'open `drush field-create article`' => 'Define new article fields and then open field edit form for refinement.',
38       'drush field-create article city,text,text_textfield subtitle,text,text_textfield' => 'Create two new fields.'
39     ),
40   );
41   $items['field-update'] = array(
42     'description' => 'Return URL for field editing web page.',
43     'core' => array('7+'),
44     'arguments' => array(
45       'field_name' => 'Name of field that needs updating.',
46     ),
47     'examples' => array(
48       'field-update comment_body' => 'Quickly navigate to a field edit web page.',
49     ),
50   );
51   $items['field-delete'] = array(
52     'description' => 'Delete a field and its instances.',
53     'core' => array('7+'),
54     'arguments' => array(
55       'field_name' => 'Name of field to delete.',
56     ),
57     'options' => array(
58       'bundle' => 'Only delete the instance attached to this bundle. If omitted, admin can choose to delete one instance or whole field.',
59       'entity_type' => 'Disambiguate a particular bundle from identically named bundles. Usually not needed.'
60     ),
61     'examples' => array(
62       'field-delete city' => 'Delete the city field and any instances it might have.',
63       'field-delete city --bundle=article' => 'Delete the city instance on the article bundle',
64     ),
65   );
66   $items['field-clone'] = array(
67     'description' => 'Clone a field and all its instances.',
68     'core' => array('7+'),
69     'arguments' => array(
70       'source_field_name' => 'Name of field that will be cloned',
71       'target_field_name' => 'Name of new, cloned field.',
72     ),
73     'examples' => array(
74       'field-clone tags labels' => 'Copy \'tags\' field into a new field \'labels\' field which has same instances.',
75       'open `field-clone tags labels`' => 'Clone field and then open field edit forms for refinement.',
76     ),
77   );
78   $items['field-info'] = array(
79     'description' => 'View information about fields, field_types, and widgets.',
80     'core' => array('7+'),
81     'arguments' => array(
82       'type' => 'Recognized values: fields, types. If omitted, a choice list appears.',
83     ),
84     'examples' => array(
85       'field-info types' => 'Show a table which lists all field types and their available widgets',
86     ),
87     'outputformat' => array(
88       'default' => 'table',
89       'pipe-format' => 'csv',
90       'field-labels' => array(
91         'field-name' => 'Field name',
92         'type' => 'Field type',
93         'bundle' => 'Field bundle',
94         'type-name' => 'Type name',
95         'widget' => 'Default widget',
96         'widgets' => 'Widgets',
97       ),
98       'table-metadata' => array(
99         'process-cell' => '_drush_field_info_process_cell',
100       ),
101       'output-data-type' => 'format-table',
102     ),
103   );
104   return $items;
105 }
106
107 /**
108  * Command argument complete callback.
109  */
110 function field_field_create_complete() {
111   if (drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
112     $all = array();
113     $info = field_info_bundles();
114     foreach ($info as $entity_type => $bundles) {
115       $all = array_merge($all, array_keys($bundles));
116     }
117     return array('values' => array_unique($bundles));
118   }
119 }
120
121 /**
122  * Command argument complete callback.
123  */
124 function field_field_update_complete() {
125   return field_field_complete_field_names();
126 }
127
128 /**
129  * Command argument complete callback.
130  */
131 function field_field_delete_complete() {
132   return field_field_complete_field_names();
133 }
134
135 /**
136  * Command argument complete callback.
137  */
138 function field_field_clone_complete() {
139   return field_field_complete_field_names();
140 }
141
142 /**
143  * Command argument complete callback.
144  */
145 function field_field_info_complete() {
146   return array('values' => array('fields', 'types'));
147 }
148
149 /**
150  * List field names for completion.
151  *
152  * @return
153  *  Array of available site aliases.
154  */
155 function field_field_complete_field_names() {
156   if (drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
157     $info = field_info_fields();
158     return array('values' => array_keys($info));
159   }
160 }
161
162 function drush_field_create($bundle) {
163   $entity_type = drush_get_option('entity_type', 'node');
164
165   $args = func_get_args();
166   array_shift($args);
167   if (empty($args)) {
168     // Just one item in this array for now.
169     $args[] = drush_field_create_wizard();
170   }
171
172   // Iterate over each field spec.
173   foreach ($args as $string) {
174     list($name, $type, $widget) = explode(',', $string);
175     $info = field_info_field($name);
176     if (empty($info)) {
177       // Field does not exist already. Create it.
178       $field = array(
179         'field_name' => $name,
180         'type' => $type,
181       );
182       drush_op('field_create_field', $field);
183     }
184
185     // Create the instance.
186     $instance = array(
187       'field_name' => $name,
188       'entity_type' => $entity_type,
189       'bundle' => $bundle,
190     );
191     if ($widget) {
192       $instance['widget'] = array('type' => $widget);
193     }
194     drush_op('field_create_instance', $instance);
195
196     $urls[] = drush_url(drush_field_ui_bundle_admin_path($entity_type, $bundle) . '/fields/' . $name, array('absolute' => TRUE));
197   }
198   drush_print(implode(' ', $urls));
199 }
200
201 // Copy of function _field_ui_bundle_admin_path() since we don't want to load UI module.
202 function drush_field_ui_bundle_admin_path($entity_type, $bundle_name) {
203   $bundles = field_info_bundles($entity_type);
204   $bundle_info = $bundles[$bundle_name];
205   if (isset($bundle_info['admin'])) {
206     return isset($bundle_info['admin']['real path']) ? $bundle_info['admin']['real path'] : $bundle_info['admin']['path'];
207   }
208 }
209
210 function drush_field_update($field_name) {
211    $info = field_info_field($field_name);
212    foreach ($info['bundles'] as $entity_type => $bundles) {
213      foreach ($bundles as $bundle) {
214        $urls[] = drush_url(drush_field_ui_bundle_admin_path($entity_type, $bundle) . '/fields/' . $field_name, array('absolute' => TRUE));
215      }
216    }
217   drush_print(implode(' ', $urls));
218 }
219
220 function drush_field_delete($field_name) {
221   $info = field_info_field($field_name);
222   $confirm = TRUE;
223
224   if (!$bundle = drush_get_option('bundle')) {
225     foreach ($info['bundles'] as $entity_type => $bundles) {
226       foreach ($bundles as $bundle) {
227         $all_bundles[] = $bundle;
228       }
229     }
230     if (count($all_bundles) > 1) {
231       $options = array_merge(array('all' => dt('All bundles')), array_combine($all_bundles, $all_bundles));
232       $bundle = drush_choice($options, dt("Choose a particular bundle or 'All bundles'"));
233       if (!$bundle) {
234         return drush_user_abort();
235       }
236       $confirm = FALSE;
237     }
238     else {
239       if (!drush_confirm(dt('Do you want to delete the !field_name field?', array('!field_name' => $field_name)))) {
240         return drush_user_abort();
241       }
242     }
243   }
244
245   if ($bundle == 'all') {
246     foreach ($info['bundles'] as $entity_type => $bundles) {
247        foreach ($bundles as $bundle) {
248          $instance = field_info_instance($entity_type, $field_name, $bundle);
249          drush_op('field_delete_instance', $instance);
250        }
251      }
252   }
253   else {
254     $entity_type = drush_field_get_entity_from_bundle($bundle);
255     $instance = field_info_instance($entity_type, $field_name, $bundle);
256     drush_op('field_delete_instance', $instance);
257   }
258
259   // If there are no more bundles, delete the field.
260   $info = field_info_field($field_name);
261   if (empty($info['bundles'])) {
262     drush_op('field_delete_field', $field_name);
263   }
264 }
265
266 function drush_field_clone($source_field_name, $target_field_name) {
267    if (!$info = field_info_field($source_field_name)) {
268      return drush_set_error(dt('!source not found in field list.', array('!source' => $source_field_name)));
269    }
270
271    unset($info['id']);
272    $info['field_name'] = $target_field_name;
273    $target = drush_op('field_create_field', $info);
274
275    foreach ($info['bundles'] as $entity_type => $bundles) {
276      foreach ($bundles as $bundle) {
277        $instance = field_info_instance($entity_type, $source_field_name, $bundle);
278        $instance['field_name'] = $target_field_name;
279        unset($instance['id']);
280        $instance['field_id'] = $target['id'];
281        drush_op('field_create_instance', $instance);
282        $urls[] = drush_url(drush_field_ui_bundle_admin_path($entity_type, $bundle) . '/fields/' . $target_field_name, array('absolute' => TRUE));
283      }
284    }
285
286   drush_print(implode(' ', $urls));
287 }
288
289 function drush_field_info($type = NULL) {
290   if (!isset($type)) {
291     // Don't ask in 'pipe' mode -- just default to 'fields'.
292     if (drush_get_context('DRUSH_PIPE')) {
293       $type = 'fields';
294     }
295     else {
296       $type = drush_choice(array_combine(array('types', 'fields'), array('types', 'fields')), dt('Which information do you wish to see?'));
297     }
298   }
299
300   $result = array();
301   switch ($type) {
302     case 'fields':
303       drush_hide_output_fields(array('type-name', 'widget', 'widgets'));
304       $info = field_info_fields();
305       foreach ($info as $field_name => $field) {
306         $bundle_strs = array();
307         foreach ($field['bundles'] as $entity_type => $bundles) {
308           $bundle_strs += $bundles;
309         }
310         $result[$field_name] = array(
311           'field-name' => $field_name,
312           'type' => $field['type'],
313           'bundle' => $bundle_strs,
314         );
315       }
316       break;
317     case 'types':
318       drush_hide_output_fields(array('field-name', 'type', 'bundle'));
319       $info = field_info_field_types();
320       module_load_include('inc', 'field_ui', 'field_ui.admin');
321       $widgets = field_info_widget_types();
322       foreach ($info as $type_name => $type) {
323         $widgets = field_ui_widget_type_options($type_name);
324         $result[$type_name] = array(
325           'type-name' => $type_name,
326           'widget' => $type['default_widget'],
327           'widgets' => $widgets,
328         );
329       }
330       break;
331     default:
332       return drush_set_error('DRUSH_FIELD_INVALID_SELECTION', dt("Argument for drush field-info must be 'fields' or 'types'"));
333   }
334
335   return $result;
336 }
337
338 /**
339  * We need to handle the formatting of cells in table-format
340  * output specially.  In 'types' output, the output data is a simple
341  * associative array of machine names => human-readable names.
342  * We choose to show the machine names.  In 'fields' output, the
343  * output data is a list of entity types, each of which contains a list
344  * of bundles.  We comma-separate the bundles, and space-separate
345  * the entities.
346  */
347 function _drush_field_info_process_cell($data, $metadata) {
348   $first = reset($data);
349   if (is_array($first)) {
350     foreach($data as $entity => $bundles) {
351       $list[] = drush_format($bundles, array(), 'csv');
352     }
353     return drush_format($list, array(), 'list');
354   }
355   return drush_format(array_keys($data), array(), 'csv');
356 }
357
358 /**
359  * Prompt user enough to create basic field and instance.
360  *
361  * @return array $field_spec
362  *   An array of brief field specifications.
363  */
364 function drush_field_create_wizard() {
365   $specs[] = drush_prompt(dt('Field name'));
366   module_load_include('inc', 'field_ui', 'field_ui.admin');
367   $types = field_ui_field_type_options();
368   $field_type = drush_choice($types, dt('Choose a field type'));
369   $specs[] = $field_type;
370   $widgets = field_ui_widget_type_options($field_type);
371   $specs[] = drush_choice($widgets, dt('Choose a widget'));
372   return implode(',', $specs);
373 }
374
375 function drush_field_get_entity_from_bundle($bundle) {
376   if (drush_get_option('entity_type')) {
377     return drush_get_option('entity_type');
378   }
379   else {
380     $info = field_info_bundles();
381     foreach ($info as $entity_type => $bundles) {
382       if (isset($bundles[$bundle])) {
383         return $entity_type;
384       }
385     }
386   }
387 }