title.drush.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @file
  4. * Drush commands for Title module.
  5. */
  6. /**
  7. * Implements hook_drush_help().
  8. */
  9. function title_drush_help($section) {
  10. switch ($section) {
  11. case 'drush:upgrade-title-fields':
  12. return dt('Upgrade title fields to use title module, so that titles can be localized.');
  13. }
  14. }
  15. /**
  16. * Implements hook_drush_command().
  17. */
  18. function title_drush_command() {
  19. $items['upgrade-title-fields'] = array(
  20. 'description' => 'Upgrade title fields to use title module, so that titles can be localized.',
  21. 'drupal dependencies' => array('title', 'entity_translation'),
  22. );
  23. $items['upgrade-title-field'] = array(
  24. 'description' => 'Upgrade a title field on a legacy field. DO NOT call this command directly, it does not do any checking.',
  25. 'hidden' => TRUE,
  26. 'arguments' => array(
  27. 'entity_type' => 'The type of entity, be it a node, user, term, etc.',
  28. 'bundle' => 'The entity bundle.',
  29. 'legacy_field' => 'The machine name of the legace field.',
  30. ),
  31. );
  32. return $items;
  33. }
  34. /**
  35. * Command callback for upgrade-title-fields.
  36. */
  37. function drush_title_upgrade_title_fields() {
  38. $entity_type = 'node';
  39. $entity_info = entity_get_info($entity_type);
  40. if (empty($entity_info['field replacement'])) {
  41. drush_log(t('There are no available title fields to upgrade.'), 'error');
  42. return FALSE;
  43. }
  44. $field_replacement_info = $entity_info['field replacement'];
  45. $upgraded = $failed = $skipped = 0;
  46. foreach (title_content_types() as $bundle) {
  47. $dt_args['@bundle'] = $bundle;
  48. $field_instances = field_info_instances($entity_type, $bundle);
  49. $extra_fields = field_info_extra_fields($entity_type, $bundle, 'form');
  50. $possible_fields = array_merge($field_instances, $extra_fields);
  51. if ($info = array_intersect_key($field_replacement_info, $possible_fields)) {
  52. foreach (array_keys($info) as $legacy_field) {
  53. $dt_args['@legacy_field'] = $legacy_field;
  54. if (drush_invoke_process('@self', 'upgrade-title-field', array($entity_type, $bundle, $legacy_field))) {
  55. $upgraded++;
  56. }
  57. else {
  58. $failed++;
  59. }
  60. }
  61. }
  62. else {
  63. $skipped++;
  64. drush_log(dt('Title fields have already been upgraded on content type @bundle.', $dt_args), 'ok');
  65. }
  66. }
  67. $messages = array();
  68. if ($upgraded) {
  69. $dt_args['@upgraded'] = $upgraded;
  70. $messages[] = 'Upgraded @upgraded title field(s).';
  71. }
  72. if ($skipped) {
  73. $dt_args['@skipped'] = $skipped;
  74. $messages[] = 'Skipped @skipped title field(s).';
  75. }
  76. if ($failed) {
  77. $dt_args['@failed'] = $failed;
  78. $messages[] = '@failed failed.';
  79. }
  80. $message = empty($messages) ? 'No fields upgraded.' : implode(' ', $messages);
  81. drush_log(dt($message, $dt_args), 'completed');
  82. if ($failed) {
  83. return FALSE;
  84. }
  85. }
  86. /**
  87. * Command callback for upgrade-title-field.
  88. */
  89. function drush_title_upgrade_title_field($entity_type, $bundle, $legacy_field) {
  90. $dt_args['@entity_type'] = $entity_type;
  91. $dt_args['@bundle'] = $bundle;
  92. $dt_args['@field'] = $legacy_field;
  93. if (!title_field_replacement_toggle($entity_type, $bundle, $legacy_field)) {
  94. drush_log(dt("Failed to upgrade title field '@field' on content type @bundle.", $dt_args), 'warning');
  95. return FALSE;
  96. }
  97. title_field_replacement_batch_set($entity_type, $bundle, $legacy_field);
  98. drush_backend_batch_process();
  99. if (drush_get_error()) {
  100. drush_log(dt("Failed upgrading title field '@field' on bundle '@bundle'. Your database is likely messed up now. Sorry about that.", $dt_args), 'error');
  101. return FALSE;
  102. }
  103. drush_log(dt("Upgraded title field '@field' on content type @bundle.", $dt_args), 'success');
  104. return TRUE;
  105. }