filefield_paths.drush.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * @file
  4. * Drush integration.
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. */
  9. function filefield_paths_drush_command() {
  10. $items = array();
  11. $items['ffp-update'] = array(
  12. 'description' => 'Retroactively updates all File (Field) Paths of a chosen field instance.',
  13. 'arguments' => array(
  14. 'entity_type' => 'Entity type.',
  15. 'bundle_name' => 'Bundle name.',
  16. 'field_name' => 'Field name.'
  17. ),
  18. 'options' => array(
  19. 'all' => 'Retroactively update all File (Field) Paths.',
  20. ),
  21. 'examples' => array(
  22. 'drush ffp-update' => 'Retroactively updates the File (Field) Paths of the instances choosen via an interactive menu.',
  23. 'drush ffp-update node article field_image' => 'Retroactively updates the File (Field) Paths of all instances of the Article content types Image field.',
  24. 'drush ffp-update --all' => 'Retroactively update all File (Field) Paths.',
  25. ),
  26. 'aliases' => array('ffpu'),
  27. );
  28. return $items;
  29. }
  30. /**
  31. * Retroactively updates all File (Field) Paths of a chosen field instance.
  32. *
  33. * @param null $entity_type
  34. * @param null $bundle_name
  35. * @param null $field_name
  36. *
  37. * @return string
  38. */
  39. function drush_filefield_paths_ffp_update($entity_type = NULL, $bundle_name = NULL, $field_name = NULL) {
  40. // Build array of information of all entity types, bundle names and field
  41. // names.
  42. $field_types = array_keys(_filefield_paths_get_field_types());
  43. $info = array();
  44. foreach (field_info_fields() as $field) {
  45. if (in_array($field['type'], $field_types)) {
  46. foreach ($field['bundles'] as $entity_type_name => $bundles) {
  47. if (!isset($info[$entity_type_name])) {
  48. $entity_type_info = entity_get_info($entity_type_name);
  49. $info[$entity_type_name] = array(
  50. '#label' => "{$entity_type_info['label']} ({$entity_type_name})",
  51. );
  52. }
  53. $bundles_info = field_info_bundles($entity_type_name);
  54. foreach ($bundles as $bundle) {
  55. if (!isset($info[$entity_type_name][$bundle])) {
  56. $info[$entity_type_name][$bundle] = array(
  57. '#label' => "{$bundles_info[$bundle]['label']} ({$bundle})",
  58. );
  59. }
  60. $field = field_info_instance($entity_type_name, $field['field_name'], $bundle);
  61. $info[$entity_type_name][$bundle][$field['field_name']] = "{$field['label']} ({$field['field_name']})";
  62. }
  63. }
  64. }
  65. }
  66. if (drush_get_option('all')) {
  67. $instances = array();
  68. foreach ($info as $entity_type => $bundles) {
  69. foreach (element_children($bundles) as $bundle_name) {
  70. foreach (element_children($info[$entity_type][$bundle_name]) as $field_name) {
  71. $instances[] = field_info_instance($entity_type, $field_name, $bundle_name);
  72. }
  73. }
  74. }
  75. _filefield_paths_drush_ffp_update($instances);
  76. return '';
  77. }
  78. // Get entity type.
  79. if (empty($entity_type)) {
  80. $choices = array('all' => dt('All'));
  81. foreach ($info as $entity_type => $entity_type_info) {
  82. $choices[$entity_type] = $entity_type_info['#label'];
  83. }
  84. $entity_type = drush_choice($choices, dt("Choose an Entity type."));
  85. }
  86. if (empty($entity_type)) {
  87. return '';
  88. }
  89. if ($entity_type == 'all') {
  90. $instances = array();
  91. foreach ($info as $entity_type => $bundles) {
  92. foreach (element_children($bundles) as $bundle_name) {
  93. foreach (element_children($info[$entity_type][$bundle_name]) as $field_name) {
  94. $instances[] = field_info_instance($entity_type, $field_name, $bundle_name);
  95. }
  96. }
  97. }
  98. _filefield_paths_drush_ffp_update($instances);
  99. return '';
  100. }
  101. // Get bundle.
  102. if (empty($bundle_name)) {
  103. $choices = array('all' => dt('All'));
  104. foreach (element_children($info[$entity_type]) as $bundle_name) {
  105. $choices[$bundle_name] = $info[$entity_type][$bundle_name]['#label'];
  106. }
  107. $bundle_name = drush_choice($choices, dt("Choose a Bundle."));
  108. }
  109. if (empty($bundle_name)) {
  110. return '';
  111. }
  112. if ($bundle_name == 'all') {
  113. $instances = array();
  114. foreach (element_children($info[$entity_type]) as $bundle_name) {
  115. foreach (element_children($info[$entity_type][$bundle_name]) as $field_name) {
  116. $instances[] = field_info_instance($entity_type, $field_name, $bundle_name);
  117. }
  118. }
  119. _filefield_paths_drush_ffp_update($instances);
  120. return '';
  121. }
  122. // Get field.
  123. if (empty($field_name)) {
  124. $choices = array('all' => dt('All'));
  125. foreach (element_children($info[$entity_type][$bundle_name]) as $field_name) {
  126. $choices[$field_name] = $info[$entity_type][$bundle_name][$field_name];
  127. }
  128. $field_name = drush_choice($choices, dt("Choose a Field."));
  129. }
  130. if (empty($field_name)) {
  131. return '';
  132. }
  133. if ($field_name == 'all') {
  134. $instances = array();
  135. foreach (element_children($info[$entity_type][$bundle_name]) as $field_name) {
  136. $instances[] = field_info_instance($entity_type, $field_name, $bundle_name);
  137. }
  138. _filefield_paths_drush_ffp_update($instances);
  139. return '';
  140. }
  141. // Invoke Retroactive update.
  142. $instance = field_info_instance($entity_type, $field_name, $bundle_name);
  143. _filefield_paths_drush_ffp_update(array($instance));
  144. }
  145. /**
  146. * Helper function; Invokes File (Field) Paths Retroactive updates.
  147. *
  148. * @param $instances
  149. */
  150. function _filefield_paths_drush_ffp_update($instances) {
  151. foreach ($instances as $instance) {
  152. if (filefield_paths_batch_update($instance)) {
  153. $batch =& batch_get();
  154. $batch['progressive'] = FALSE;
  155. drush_backend_batch_process();
  156. drush_log(dt('!field_name File (Field) Paths updated.', array('!field_name' => "{$instance['label']} ({$instance['entity_type']}-{$instance['bundle']}-{$instance['field_name']})")), 'success');
  157. }
  158. }
  159. }