PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Library/drush/commands/core/field.drush.inc

http://github.com/jyr/MNPP
Pascal | 359 lines | 256 code | 14 blank | 89 comment | 12 complexity | 9da713dbe23eee3cec5a689b4266c2d4 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, LGPL-2.0, LGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0, BSD-3-Clause, GPL-3.0, BSD-2-Clause
  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. 'required-arguments' => 1,
  30. 'options' => array(
  31. 'entity_type' => 'Type of entity (e.g. node, user, comment). Defaults to node.',
  32. ),
  33. 'examples' => array(
  34. 'drush field-create article' => 'Define new article fields via interactive prompts.',
  35. 'open `drush field-create article`' => 'Define new article fields and then open field edit form for refinement.',
  36. 'drush field-create article city,text,text_textfield subtitle,text,text_textfield' => 'Create two new fields.'
  37. ),
  38. );
  39. $items['field-update'] = array(
  40. 'description' => 'Return URL for field editing web page.',
  41. 'core' => array('7+'),
  42. 'drupal_dependencies' => array('field_ui'),
  43. 'arguments' => array(
  44. 'field_name' => 'Name of field that needs updating.',
  45. ),
  46. 'examples' => array(
  47. 'field-update comment_body' => 'Quickly navigate to a field edit web page.',
  48. ),
  49. );
  50. $items['field-delete'] = array(
  51. 'description' => 'Delete a field and its instances.',
  52. 'core' => array('7+'),
  53. 'arguments' => array(
  54. 'field_name' => 'Name of field to delete.',
  55. ),
  56. 'options' => array(
  57. 'bundle' => 'Only delete the instance attached to this bundle. If omitted, admin can choose to delete one instance or whole field.',
  58. 'entity_type' => 'Disambiguate a particular bundle from identically named bundles. Usually not needed.'
  59. ),
  60. 'examples' => array(
  61. 'field-delete city' => 'Delete the city field and any instances it might have.',
  62. 'field-delete city --bundle=article' => 'Delete the city instance on the article bundle',
  63. ),
  64. );
  65. $items['field-clone'] = array(
  66. 'description' => 'Clone a field and all its instances.',
  67. 'core' => array('7+'),
  68. 'arguments' => array(
  69. 'source_field_name' => 'Name of field that will be cloned',
  70. 'target_field_name' => 'Name of new, cloned field.',
  71. ),
  72. 'examples' => array(
  73. 'field-clone tags labels' => 'Copy \'tags\' field into a new field \'labels\' field which has same instances.',
  74. 'open `field-clone tags labels`' => 'Clone field and then open field edit forms for refinement.',
  75. ),
  76. );
  77. $items['field-info'] = array(
  78. 'description' => 'View information about fields, field_types, and widgets.',
  79. 'drupal_dependencies' => array('field_ui'),
  80. 'core' => array('7+'),
  81. 'arguments' => array(
  82. 'type' => 'Recognized values: fields, types. If omitted, a choice list appears.',
  83. ),
  84. 'options' => array(
  85. 'pipe' => 'Return field information table as CSV.',
  86. ),
  87. 'examples' => array(
  88. 'field-info types' => 'Show a table which lists all field types and their available widgets',
  89. ),
  90. );
  91. return $items;
  92. }
  93. /**
  94. * Command argument complete callback.
  95. */
  96. function field_field_create_complete() {
  97. if (drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  98. $all = array();
  99. $info = field_info_bundles();
  100. foreach ($info as $entity_type => $bundles) {
  101. $all = array_merge($all, array_keys($bundles));
  102. }
  103. return array('values' => array_unique($bundles));
  104. }
  105. }
  106. /**
  107. * Command argument complete callback.
  108. */
  109. function field_field_update_complete() {
  110. return field_field_complete_field_names();
  111. }
  112. /**
  113. * Command argument complete callback.
  114. */
  115. function field_field_delete_complete() {
  116. return field_field_complete_field_names();
  117. }
  118. /**
  119. * Command argument complete callback.
  120. */
  121. function field_field_clone_complete() {
  122. return field_field_complete_field_names();
  123. }
  124. /**
  125. * Command argument complete callback.
  126. */
  127. function field_field_info_complete() {
  128. return array('values' => array('fields', 'types'));
  129. }
  130. /**
  131. * List field names for completion.
  132. *
  133. * @return
  134. * Array of available site aliases.
  135. */
  136. function field_field_complete_field_names() {
  137. if (drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  138. $info = field_info_fields();
  139. return array('values' => array_keys($info));
  140. }
  141. }
  142. function drush_field_create($bundle) {
  143. $entity_type = drush_get_option('entity_type', 'node');
  144. $args = func_get_args();
  145. array_shift($args);
  146. if (empty($args)) {
  147. // Just one item in this array for now.
  148. $args[] = drush_field_create_wizard();
  149. }
  150. // Iterate over each field spec.
  151. foreach ($args as $string) {
  152. list($name, $type, $widget) = explode(',', $string);
  153. $info = field_info_field($name);
  154. if (empty($info)) {
  155. // Field does not exist already. Create it.
  156. $field = array(
  157. 'field_name' => $name,
  158. 'type' => $type,
  159. );
  160. drush_op('field_create_field', $field);
  161. }
  162. // Create the instance.
  163. $instance = array(
  164. 'field_name' => $name,
  165. 'entity_type' => $entity_type,
  166. 'bundle' => $bundle,
  167. );
  168. if ($widget) {
  169. $instance['widget'] = array('type' => $widget);
  170. }
  171. drush_op('field_create_instance', $instance);
  172. $urls[] = url(drush_field_ui_bundle_admin_path($entity_type, $bundle) . '/fields/' . $name, array('absolute' => TRUE));
  173. }
  174. drush_print(implode(' ', $urls));
  175. }
  176. // Copy of function _field_ui_bundle_admin_path() since we don't want to load UI module.
  177. function drush_field_ui_bundle_admin_path($entity_type, $bundle_name) {
  178. $bundles = field_info_bundles($entity_type);
  179. $bundle_info = $bundles[$bundle_name];
  180. if (isset($bundle_info['admin'])) {
  181. return isset($bundle_info['admin']['real path']) ? $bundle_info['admin']['real path'] : $bundle_info['admin']['path'];
  182. }
  183. }
  184. function drush_field_update($field_name) {
  185. $info = field_info_field($field_name);
  186. foreach ($info['bundles'] as $entity_type => $bundles) {
  187. foreach ($bundles as $bundle) {
  188. $urls[] = url(drush_field_ui_bundle_admin_path($entity_type, $bundle) . '/fields/' . $field_name, array('absolute' => TRUE));
  189. }
  190. }
  191. drush_print(implode(' ', $urls));
  192. }
  193. function drush_field_delete($field_name) {
  194. $info = field_info_field($field_name);
  195. $confirm = TRUE;
  196. if (!$bundle = drush_get_option('bundle')) {
  197. foreach ($info['bundles'] as $entity_type => $bundles) {
  198. foreach ($bundles as $bundle) {
  199. $all_bundles[] = $bundle;
  200. }
  201. }
  202. if (count($bundles) > 1) {
  203. $options = array_merge(array('all' => dt('All bundles')), drupal_map_assoc($bundles));
  204. $bundle = drush_choice($options, dt("Choose a particular bundle or 'All bundles'"));
  205. $confirm = FALSE;
  206. }
  207. else {
  208. if (!drush_confirm(dt('Do you want to delete the %field_name field?', array('%field_name' => $field_name)))) {
  209. return drush_user_abort();
  210. }
  211. }
  212. }
  213. if ($bundle == 'all') {
  214. foreach ($info['bundles'] as $entity_type => $bundles) {
  215. foreach ($bundles as $bundle) {
  216. $instance = field_info_instance($entity_type, $field_name, $bundle);
  217. drush_op('field_delete_instance', $instance);
  218. }
  219. }
  220. }
  221. else {
  222. $entity_type = drush_field_get_entity_from_bundle($bundle);
  223. $instance = field_info_instance($entity_type, $field_name, $bundle);
  224. drush_op('field_delete_instance', $instance);
  225. }
  226. // If there are no more bundles, delete the field.
  227. $info = field_info_field($field_name);
  228. if (empty($info['bundles'])) {
  229. drush_op('field_delete_field', $field_name);
  230. }
  231. }
  232. function drush_field_clone($source_field_name, $target_field_name) {
  233. if (!$info = field_info_field($source_field_name)) {
  234. return drush_set_error(dt('%source not found in field list.', array('%source' => $source_field_name)));
  235. }
  236. unset($info['id']);
  237. $info['field_name'] = $target_field_name;
  238. $target = drush_op('field_create_field', $info);
  239. foreach ($info['bundles'] as $entity_type => $bundles) {
  240. foreach ($bundles as $bundle) {
  241. $instance = field_info_instance($entity_type, $source_field_name, $bundle);
  242. $instance['field_name'] = $target_field_name;
  243. unset($instance['id']);
  244. $instance['field_id'] = $target['id'];
  245. drush_op('field_create_instance', $instance);
  246. $urls[] = url(drush_field_ui_bundle_admin_path($entity_type, $bundle) . '/fields/' . $target_field_name, array('absolute' => TRUE));
  247. }
  248. }
  249. drush_print(implode(' ', $urls));
  250. }
  251. function drush_field_info($type = NULL) {
  252. if (is_null($type)) {
  253. $type = drush_choice(drupal_map_assoc(array('types', 'fields')), dt('Which information do you wish to see?'));
  254. }
  255. switch ($type) {
  256. case 'fields':
  257. $rows[] = array(
  258. dt('Field name'),
  259. dt('Field type'),
  260. dt('Bundles'),
  261. );
  262. $info = field_info_fields();
  263. foreach ($info as $field_name => $field) {
  264. $bundle_strs = array();
  265. foreach ($field['bundles'] as $entity_type => $bundles) {
  266. $bundle_strs[] = implode(',', $bundles);
  267. }
  268. $row = array(
  269. $field_name,
  270. $field['type'],
  271. implode(' ', $bundle_strs),
  272. );
  273. $rows[] = $row;
  274. $pipe[] = implode(',', $row);
  275. }
  276. break;
  277. case 'types':
  278. $rows[] = array(
  279. dt('Field type'),
  280. dt('Default widget'),
  281. dt('Widgets'),
  282. );
  283. $info = field_info_field_types();
  284. module_load_include('inc', 'field_ui', 'field_ui.admin');
  285. $widgets = field_info_widget_types();
  286. foreach ($info as $type_name => $type) {
  287. $widgets = field_ui_widget_type_options($type_name);
  288. $row = array(
  289. $type_name,
  290. $type['default_widget'],
  291. implode(', ', array_keys($widgets)),
  292. );
  293. $rows[] = $row;
  294. $pipe[] = implode(',', $row);
  295. }
  296. break;
  297. }
  298. drush_print_table($rows, TRUE);
  299. drush_print_pipe($pipe);
  300. return $rows;
  301. }
  302. /**
  303. * Prompt user enough to create basic field and instance.
  304. *
  305. * @return array $field_spec
  306. * An array of brief field specifications.
  307. */
  308. function drush_field_create_wizard() {
  309. $specs[] = drush_prompt(dt('Field name'));
  310. module_load_include('inc', 'field_ui', 'field_ui.admin');
  311. $types = field_ui_field_type_options();
  312. $field_type = drush_choice($types, dt('Choose a field type'));
  313. $specs[] = $field_type;
  314. $widgets = field_ui_widget_type_options($field_type);
  315. $specs[] = drush_choice($widgets, dt('Choose a widget'));
  316. return implode(',', $specs);
  317. }
  318. function drush_field_get_entity_from_bundle($bundle) {
  319. if (drush_get_option('entity_type')) {
  320. return drush_get_option('entity_type');
  321. }
  322. else {
  323. $info = field_info_bundles();
  324. foreach ($info as $entity_type => $bundles) {
  325. if (isset($bundles[$bundle])) {
  326. return $entity_type;
  327. }
  328. }
  329. }
  330. }