entity_translation_upgrade.drush.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Implements hook_drush_command().
  4. */
  5. function entity_translation_upgrade_drush_command() {
  6. $items = array();
  7. $items['entity-translation-upgrade'] = array(
  8. 'description' => "Upgrades all nodes of the specified content type from Content Translation to Entity Translation.",
  9. 'arguments' => array(
  10. 'content_type' => 'Content type of nodes to be upgraded.',
  11. ),
  12. 'examples' => array(
  13. 'drush entity-translation-upgrade article' => 'Upgrades all nodes of content type "article" from Content Translation to Entity Translation.',
  14. ),
  15. 'aliases' => array('etu'),
  16. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  17. );
  18. return $items;
  19. }
  20. /**
  21. * Implements hook_drush_help().
  22. *
  23. * @param
  24. * A string with the help section
  25. *
  26. * @return
  27. * A string with the help text for the entity-translation-upgrade command
  28. */
  29. function entity_translation_upgrade_drush_help($section) {
  30. switch ($section) {
  31. case 'drush:entity-translation-upgrade':
  32. return dt("Brief help for Drush command entity-translation-upgrade.");
  33. case 'meta:entity_translation_upgrade:title':
  34. return dt("Entity Translation Upgrade commands");
  35. case 'meta:entity_translation_upgrade:summary':
  36. return dt("Upgrading nodes to Entity Translation.");
  37. }
  38. }
  39. /**
  40. * Implements drush_hook_COMMAND().
  41. *
  42. * @param
  43. * The content type of which the nodes shall be upgraded
  44. *
  45. * Runs the batch upgrading nodes of the specified content_type to Entity
  46. * Translation. Lets user choose content type from a list, if argument has
  47. * not been provided.
  48. */
  49. function drush_entity_translation_upgrade($content_type = "") {
  50. // Get all installed content types.
  51. $available_types = array();
  52. $available_types_chose = array();
  53. $available_types_str = '';
  54. foreach (node_type_get_types() as $type) {
  55. $available_types[$type->type] = $type->type;
  56. $available_types_chose[$type->type] = $type->name;
  57. if (strlen($available_types_str) > 0) {
  58. $available_types_str .= ', ';
  59. }
  60. $available_types_str .= $type->type;
  61. }
  62. // If argument content_type is empty, prompt user for content type.
  63. if (!$content_type) {
  64. $content_type = drush_choice($available_types_chose, dt('Choose the content type of the nodes to be upgraded to Entity Translation:'));
  65. }
  66. // Do content type argument checks.
  67. if (!$content_type) {
  68. return TRUE;
  69. }
  70. if (strlen($available_types_str) == 0) {
  71. return drush_set_error(dt('Entity Translation Upgrade cannot run as no content type has been installed.'));
  72. }
  73. if (!in_array($content_type, $available_types)) {
  74. return drush_set_error(dt('"@content_type" is not a valid content type machine name. Please use one of these installed content types as argument: @available_types_str.', array('@content_type' => $content_type, '@available_types_str' => $available_types_str)));
  75. }
  76. // Start batch to upgrade nodes of the specified content type.
  77. $types = array($content_type => $content_type);
  78. $batch = array(
  79. 'operations' => array(
  80. array('entity_translation_upgrade_do', array($types)),
  81. array('entity_translation_upgrade_complete', array()),
  82. ),
  83. 'finished' => 'entity_translation_upgrade_drush_end',
  84. 'file' => drupal_get_path('module', 'entity_translation_upgrade') . '/entity_translation_upgrade.admin.inc',
  85. 'progressive' => FALSE,
  86. );
  87. batch_set($batch);
  88. drush_backend_batch_process();
  89. }
  90. /**
  91. * This is the 'finished' batch callback, drush version.
  92. */
  93. function entity_translation_upgrade_drush_end($success, $results, $operations, $elapsed) {
  94. // Print result messages.
  95. if (!empty($results)) {
  96. $message = format_plural($results['total'], '1 node translation successfully upgraded.', '@count node translations successfully upgraded.');
  97. $severity = 'ok';
  98. watchdog('Entity Translation upgrade', '@count node translations successfully upgraded.', array('@count' => $results['total']), WATCHDOG_INFO);
  99. }
  100. else {
  101. $message = t('No node translation available for the upgrade.');
  102. $severity = 'warning';
  103. }
  104. drush_log($message, $severity);
  105. }