5 * Field API's drush integration
9 * Implementation of hook_drush_help().
11 function field_drush_help($section) {
13 case 'meta:field:title':
14 return dt('Field commands');
15 case 'meta:field:summary':
16 return dt('Manipulate Drupal 7+ fields.');
21 * Implementation of hook_drush_command().
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+'),
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.'
31 'required-arguments' => 1,
33 'entity_type' => 'Type of entity (e.g. node, user, comment). Defaults to node.',
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.'
41 $items['field-update'] = array(
42 'description' => 'Return URL for field editing web page.',
43 'core' => array('7+'),
45 'field_name' => 'Name of field that needs updating.',
48 'field-update comment_body' => 'Quickly navigate to a field edit web page.',
51 $items['field-delete'] = array(
52 'description' => 'Delete a field and its instances.',
53 'core' => array('7+'),
55 'field_name' => 'Name of field to delete.',
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.'
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',
66 $items['field-clone'] = array(
67 'description' => 'Clone a field and all its instances.',
68 'core' => array('7+'),
70 'source_field_name' => 'Name of field that will be cloned',
71 'target_field_name' => 'Name of new, cloned field.',
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.',
78 $items['field-info'] = array(
79 'description' => 'View information about fields, field_types, and widgets.',
80 'core' => array('7+'),
82 'type' => 'Recognized values: fields, types. If omitted, a choice list appears.',
85 'field-info types' => 'Show a table which lists all field types and their available widgets',
87 'outputformat' => array(
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',
98 'table-metadata' => array(
99 'process-cell' => '_drush_field_info_process_cell',
101 'output-data-type' => 'format-table',
108 * Command argument complete callback.
110 function field_field_create_complete() {
111 if (drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
113 $info = field_info_bundles();
114 foreach ($info as $entity_type => $bundles) {
115 $all = array_merge($all, array_keys($bundles));
117 return array('values' => array_unique($bundles));
122 * Command argument complete callback.
124 function field_field_update_complete() {
125 return field_field_complete_field_names();
129 * Command argument complete callback.
131 function field_field_delete_complete() {
132 return field_field_complete_field_names();
136 * Command argument complete callback.
138 function field_field_clone_complete() {
139 return field_field_complete_field_names();
143 * Command argument complete callback.
145 function field_field_info_complete() {
146 return array('values' => array('fields', 'types'));
150 * List field names for completion.
153 * Array of available site aliases.
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));
162 function drush_field_create($bundle) {
163 $entity_type = drush_get_option('entity_type', 'node');
165 $args = func_get_args();
168 // Just one item in this array for now.
169 $args[] = drush_field_create_wizard();
172 // Iterate over each field spec.
173 foreach ($args as $string) {
174 list($name, $type, $widget) = explode(',', $string);
175 $info = field_info_field($name);
177 // Field does not exist already. Create it.
179 'field_name' => $name,
182 drush_op('field_create_field', $field);
185 // Create the instance.
187 'field_name' => $name,
188 'entity_type' => $entity_type,
192 $instance['widget'] = array('type' => $widget);
194 drush_op('field_create_instance', $instance);
196 $urls[] = drush_url(drush_field_ui_bundle_admin_path($entity_type, $bundle) . '/fields/' . $name, array('absolute' => TRUE));
198 drush_print(implode(' ', $urls));
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'];
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));
217 drush_print(implode(' ', $urls));
220 function drush_field_delete($field_name) {
221 $info = field_info_field($field_name);
224 if (!$bundle = drush_get_option('bundle')) {
225 foreach ($info['bundles'] as $entity_type => $bundles) {
226 foreach ($bundles as $bundle) {
227 $all_bundles[] = $bundle;
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'"));
234 return drush_user_abort();
239 if (!drush_confirm(dt('Do you want to delete the !field_name field?', array('!field_name' => $field_name)))) {
240 return drush_user_abort();
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);
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);
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);
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)));
272 $info['field_name'] = $target_field_name;
273 $target = drush_op('field_create_field', $info);
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));
286 drush_print(implode(' ', $urls));
289 function drush_field_info($type = NULL) {
291 // Don't ask in 'pipe' mode -- just default to 'fields'.
292 if (drush_get_context('DRUSH_PIPE')) {
296 $type = drush_choice(array_combine(array('types', 'fields'), array('types', 'fields')), dt('Which information do you wish to see?'));
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;
310 $result[$field_name] = array(
311 'field-name' => $field_name,
312 'type' => $field['type'],
313 'bundle' => $bundle_strs,
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,
332 return drush_set_error('DRUSH_FIELD_INVALID_SELECTION', dt("Argument for drush field-info must be 'fields' or 'types'"));
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
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');
353 return drush_format($list, array(), 'list');
355 return drush_format(array_keys($data), array(), 'csv');
359 * Prompt user enough to create basic field and instance.
361 * @return array $field_spec
362 * An array of brief field specifications.
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);
375 function drush_field_get_entity_from_bundle($bundle) {
376 if (drush_get_option('entity_type')) {
377 return drush_get_option('entity_type');
380 $info = field_info_bundles();
381 foreach ($info as $entity_type => $bundles) {
382 if (isset($bundles[$bundle])) {