field.drush.inc

  1. 8.0.x commands/core/field.drush.inc
  2. 6.x commands/core/field.drush.inc
  3. 7.x commands/core/field.drush.inc
  4. 4.x commands/core/field.drush.inc
  5. 5.x commands/core/field.drush.inc

Field API's drush integration

Functions

File

commands/core/field.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Field API's drush integration
  5. */
  6. /**
  7. * Implementation of hook_drush_help().
  8. */
  9. function field_drush_help($section) {
  10. switch ($section) {
  11. case 'meta:field:title':
  12. return dt('Field commands');
  13. case 'meta:field:summary':
  14. return dt('Manipulate Drupal 7+ fields.');
  15. }
  16. }
  17. /**
  18. * Implementation of hook_drush_command().
  19. */
  20. function field_drush_command() {
  21. $items['field-create'] = array(
  22. 'description' => 'Create fields and instances. Returns urls for field editing.',
  23. 'core' => array('7+'),
  24. 'drupal_dependencies' => array('field_ui'),
  25. 'arguments' => array(
  26. 'bundle' => 'Content type (for nodes). Name of bundle to attach fields to. Required.',
  27. '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.'
  28. ),
  29. 'options' => array(
  30. 'entity_type' => 'Type of entity (e.g. node, user, comment). Defaults to node.',
  31. ),
  32. 'examples' => array(
  33. 'drush field-create article' => 'Define new article fields via interactive prompts.',
  34. 'open `drush field-create article`' => 'Define new article fields and then open field edit form for refinement.',
  35. 'drush field-create article city,text,text_textfield subtitle,text,text_textfield' => 'Create two new fields.'
  36. ),
  37. );
  38. $items['field-update'] = array(
  39. 'description' => 'Return URL for field editing web page.',
  40. 'core' => array('7+'),
  41. 'drupal_dependencies' => array('field_ui'),
  42. 'arguments' => array(
  43. 'field_name' => 'Name of field that needs updating.',
  44. ),
  45. 'examples' => array(
  46. 'field-update comment_body' => 'Quickly navigate to a field edit web page.',
  47. ),
  48. );
  49. $items['field-delete'] = array(
  50. 'description' => 'Delete a field and its instances.',
  51. 'core' => array('7+'),
  52. 'arguments' => array(
  53. 'field_name' => 'Name of field to delete.',
  54. ),
  55. 'options' => array(
  56. 'bundle' => 'Only delete the instance attached to this bundle. If omitted, admin can choose to delete one instance or whole field.',
  57. 'entity_type' => 'Disambiguate a particular bundle from identically named bundles. Usually not needed.'
  58. ),
  59. 'examples' => array(
  60. 'field-delete city' => 'Delete the city field and any instances it might have.',
  61. 'field-delete city --bundle=article' => 'Delete the city instance on the article bundle',
  62. ),
  63. );
  64. $items['field-clone'] = array(
  65. 'description' => 'Clone a field and all its instances.',
  66. 'core' => array('7+'),
  67. 'arguments' => array(
  68. 'source_field_name' => 'Name of field that will be cloned',
  69. 'target_field_name' => 'Name of new, cloned field.',
  70. ),
  71. 'examples' => array(
  72. 'field-clone tags labels' => 'Copy \'tags\' field into a new field \'labels\' field which has same instances.',
  73. 'open `field-clone tags labels`' => 'Clone field and then open field edit forms for refinement.',
  74. ),
  75. );
  76. $items['field-info'] = array(
  77. 'description' => 'View information about fields, field_types, and widgets.',
  78. 'drupal_dependencies' => array('field_ui'),
  79. 'core' => array('7+'),
  80. 'arguments' => array(
  81. 'type' => 'Recognized values: fields, types. If omitted, a choice list appears.',
  82. ),
  83. 'options' => array(
  84. 'pipe' => 'Return field information table as CSV.',
  85. ),
  86. 'examples' => array(
  87. 'field-info types' => 'Show a table which lists all field types and their available widgets',
  88. ),
  89. );
  90. return $items;
  91. }
  92. function drush_field_create($bundle) {
  93. $entity_type = drush_get_option('entity_type', 'node');
  94. $args = func_get_args();
  95. array_shift($args);
  96. if (empty($args)) {
  97. // Just one item in this array for now.
  98. $args[] = drush_field_create_wizard();
  99. }
  100. // Iterate over each field spec.
  101. foreach ($args as $string) {
  102. list($name, $type, $widget) = explode(',', $string);
  103. $info = field_info_field($name);
  104. if (empty($info)) {
  105. // Field does not exist already. Create it.
  106. $field = array(
  107. 'field_name' => $name,
  108. 'type' => $type,
  109. );
  110. drush_op('field_create_field', $field);
  111. }
  112. // Create the instance.
  113. $instance = array(
  114. 'field_name' => $name,
  115. 'entity_type' => $entity_type,
  116. 'bundle' => $bundle,
  117. );
  118. if ($widget) {
  119. $instance['widget'] = array('type' => $widget);
  120. }
  121. drush_op('field_create_instance', $instance);
  122. $urls[] = url(drush_field_ui_bundle_admin_path($entity_type, $bundle) . '/fields/' . $name, array('absolute' => TRUE));
  123. }
  124. drush_print(implode(' ', $urls));
  125. }
  126. // Copy of function _field_ui_bundle_admin_path() since we don't want to load UI module.
  127. function drush_field_ui_bundle_admin_path($entity_type, $bundle_name) {
  128. $bundles = field_info_bundles($entity_type);
  129. $bundle_info = $bundles[$bundle_name];
  130. if (isset($bundle_info['admin'])) {
  131. return isset($bundle_info['admin']['real path']) ? $bundle_info['admin']['real path'] : $bundle_info['admin']['path'];
  132. }
  133. }
  134. function drush_field_update($field_name) {
  135. $info = field_info_field($field_name);
  136. foreach ($info['bundles'] as $entity_type => $bundles) {
  137. foreach ($bundles as $bundle) {
  138. $urls[] = url(drush_field_ui_bundle_admin_path($entity_type, $bundle) . '/fields/' . $field_name, array('absolute' => TRUE));
  139. }
  140. }
  141. drush_print(implode(' ', $urls));
  142. }
  143. function drush_field_delete($field_name) {
  144. $info = field_info_field($field_name);
  145. $confirm = TRUE;
  146. if (!$bundle = drush_get_option('bundle')) {
  147. foreach ($info['bundles'] as $entity_type => $bundles) {
  148. foreach ($bundles as $bundle) {
  149. $all_bundles[] = $bundle;
  150. }
  151. }
  152. if (count($bundles) > 1) {
  153. $options = array_merge(array('all' => dt('All bundles')), drupal_map_assoc($bundles));
  154. $bundle = drush_choice($options, dt("Choose a particular bundle or 'All bundles'"));
  155. $confirm = FALSE;
  156. }
  157. else {
  158. if (!drush_confirm(dt('Do you want to delete the %field_name field?', array('%field_name' => $field_name)))) {
  159. return drush_user_abort();
  160. }
  161. }
  162. }
  163. if ($bundle == 'all') {
  164. foreach ($info['bundles'] as $entity_type => $bundles) {
  165. foreach ($bundles as $bundle) {
  166. $instance = field_info_instance($entity_type, $field_name, $bundle);
  167. drush_op('field_delete_instance', $instance);
  168. }
  169. }
  170. }
  171. else {
  172. $entity_type = drush_field_get_entity_from_bundle($bundle);
  173. $instance = field_info_instance($entity_type, $field_name, $bundle);
  174. drush_op('field_delete_instance', $instance);
  175. }
  176. // If there are no more bundles, delete the field.
  177. $info = field_info_field($field_name);
  178. if (empty($info['bundles'])) {
  179. drush_op('field_delete_field', $field_name);
  180. }
  181. }
  182. function drush_field_clone($source_field_name, $target_field_name) {
  183. if (!$info = field_info_field($source_field_name)) {
  184. return drush_set_error(dt('%source not found in field list.', array('%source' => $source_field_name)));
  185. }
  186. unset($info['id']);
  187. $info['field_name'] = $target_field_name;
  188. $target = drush_op('field_create_field', $info);
  189. foreach ($info['bundles'] as $entity_type => $bundles) {
  190. foreach ($bundles as $bundle) {
  191. $instance = field_info_instance($entity_type, $source_field_name, $bundle);
  192. $instance['field_name'] = $target_field_name;
  193. unset($instance['id']);
  194. $instance['field_id'] = $target['id'];
  195. drush_op('field_create_instance', $instance);
  196. $urls[] = url(drush_field_ui_bundle_admin_path($entity_type, $bundle) . '/fields/' . $target_field_name, array('absolute' => TRUE));
  197. }
  198. }
  199. drush_print(implode(' ', $urls));
  200. }
  201. function drush_field_info($type = NULL) {
  202. if (is_null($type)) {
  203. $type = drush_choice(drupal_map_assoc(array('types', 'fields')), dt('Which information do you wish to see?'));
  204. }
  205. switch ($type) {
  206. case 'fields':
  207. $rows[] = array(
  208. dt('Field name'),
  209. dt('Field type'),
  210. dt('Bundles'),
  211. );
  212. $info = field_info_fields();
  213. foreach ($info as $field_name => $field) {
  214. $bundle_strs = array();
  215. foreach ($field['bundles'] as $entity_type => $bundles) {
  216. $bundle_strs[] = implode(',', $bundles);
  217. }
  218. $row = array(
  219. $field_name,
  220. $field['type'],
  221. implode(' ', $bundle_strs),
  222. );
  223. $rows[] = $row;
  224. $pipe[] = implode(',', $row);
  225. }
  226. break;
  227. case 'types':
  228. $rows[] = array(
  229. dt('Field type'),
  230. dt('Default widget'),
  231. dt('Widgets'),
  232. );
  233. $info = field_info_field_types();
  234. module_load_include('inc', 'field_ui', 'field_ui.admin');
  235. $widgets = field_info_widget_types();
  236. foreach ($info as $type_name => $type) {
  237. $widgets = field_ui_widget_type_options($type_name);
  238. $row = array(
  239. $type_name,
  240. $type['default_widget'],
  241. implode(', ', array_keys($widgets)),
  242. );
  243. $rows[] = $row;
  244. $pipe[] = implode(',', $row);
  245. }
  246. break;
  247. }
  248. drush_print_table($rows, TRUE);
  249. drush_print_pipe($pipe);
  250. return $rows;
  251. }
  252. /**
  253. * Prompt user enough to create basic field and instance.
  254. *
  255. * @return array $field_spec
  256. * An array of brief field specifications.
  257. */
  258. function drush_field_create_wizard() {
  259. $specs[] = drush_prompt(dt('Field name'));
  260. module_load_include('inc', 'field_ui', 'field_ui.admin');
  261. $types = field_ui_field_type_options();
  262. $field_type = drush_choice($types, dt('Choose a field type'));
  263. $specs[] = $field_type;
  264. $widgets = field_ui_widget_type_options($field_type);
  265. $specs[] = drush_choice($widgets, dt('Choose a widget'));
  266. return implode(',', $specs);
  267. }
  268. function drush_field_get_entity_from_bundle($bundle) {
  269. if (drush_get_option('entity_type')) {
  270. return drush_get_option('entity_type');
  271. }
  272. else {
  273. $info = field_info_bundles();
  274. foreach ($info as $entity_type => $bundles) {
  275. if (isset($bundles[$bundle])) {
  276. return $entity_type;
  277. }
  278. }
  279. }
  280. }