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

Namesort descending Description
drush_field_clone
drush_field_create
drush_field_create_wizard Prompt user enough to create basic field and instance.
drush_field_delete
drush_field_get_entity_from_bundle
drush_field_info
drush_field_ui_bundle_admin_path
drush_field_update
field_drush_command Implementation of hook_drush_command().
field_drush_help Implementation of hook_drush_help().
field_field_clone_complete Command argument complete callback.
field_field_complete_field_names List field names for completion.
field_field_create_complete Command argument complete callback.
field_field_delete_complete Command argument complete callback.
field_field_info_complete Command argument complete callback.
field_field_update_complete Command argument complete callback.
_drush_field_info_process_cell We need to handle the formatting of cells in table-format output specially. In 'types' output, the output data is a simple associative array of machine names => human-readable names. We choose to show the machine names. In…

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