|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\islandora_drush_utils\Drush\Commands; |
| 4 | + |
| 5 | +use Consolidation\AnnotatedCommand\Attributes\HookSelector; |
| 6 | +use Drupal\Core\DependencyInjection\AutowireTrait; |
| 7 | +use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
| 8 | +use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 9 | +use Drupal\Core\Messenger\MessengerInterface; |
| 10 | +use Drupal\Core\StringTranslation\StringTranslationTrait; |
| 11 | +use Drush\Attributes as CLI; |
| 12 | +use Drush\Commands\DrushCommands; |
| 13 | +use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| 14 | + |
| 15 | +/** |
| 16 | + * Commands to update display hints en masse. |
| 17 | + */ |
| 18 | +class UpdateDisplayHints extends DrushCommands { |
| 19 | + |
| 20 | + use AutowireTrait; |
| 21 | + use DependencySerializationTrait; |
| 22 | + use StringTranslationTrait; |
| 23 | + |
| 24 | + public const MODEL_URI = 'https://schema.org/Book'; |
| 25 | + public const DISPLAY_HINT_URI = 'https://projectmirador.org'; |
| 26 | + |
| 27 | + /** |
| 28 | + * Construct. |
| 29 | + */ |
| 30 | + public function __construct( |
| 31 | + #[Autowire(service: 'entity_type.manager')] |
| 32 | + protected EntityTypeManagerInterface $entityTypeManager, |
| 33 | + #[Autowire(service: 'messenger')] |
| 34 | + protected MessengerInterface $messenger, |
| 35 | + ) { |
| 36 | + parent::__construct(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Feeder command to grab NIDs of items to update. |
| 41 | + */ |
| 42 | + #[CLI\Command(name: 'islandora_drush_utils:display-hint-feeder')] |
| 43 | + #[CLI\Option(name: 'term-uris', description: 'Comma separated list of Islandora Model term URIs to target.')] |
| 44 | + #[HookSelector(name: 'islandora-drush-utils-user-wrap')] |
| 45 | + public function displayHintFeeder( |
| 46 | + array $options = [ |
| 47 | + 'term-uris' => self::MODEL_URI, |
| 48 | + ], |
| 49 | + ) : void { |
| 50 | + $uris = array_map('trim', explode(',', $options['term-uris'])); |
| 51 | + |
| 52 | + $termStorage = $this->entityTypeManager->getStorage('taxonomy_term'); |
| 53 | + $terms = $termStorage->getQuery() |
| 54 | + ->accessCheck(FALSE) |
| 55 | + ->condition('field_external_uri', $uris, 'IN') |
| 56 | + ->condition('vid', 'islandora_models') |
| 57 | + ->execute(); |
| 58 | + |
| 59 | + if (empty($terms)) { |
| 60 | + $this->messenger->addError($this->t('No terms found with URIs: @uris', [ |
| 61 | + '@uris' => implode(', ', $uris), |
| 62 | + ])); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + $termIds = array_keys($terms); |
| 67 | + $nodeStorage = $this->entityTypeManager->getStorage('node'); |
| 68 | + |
| 69 | + $nids = $nodeStorage->getQuery() |
| 70 | + ->accessCheck(FALSE) |
| 71 | + ->condition('field_model', $termIds, 'IN') |
| 72 | + ->execute(); |
| 73 | + |
| 74 | + if (empty($nids)) { |
| 75 | + $this->messenger->addError($this->t('No applicable nodes found.')); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + $this->output()->writeln(implode("\n", $nids)); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Updates nodes with display hints, consumes from STDIN. |
| 84 | + */ |
| 85 | + #[CLI\Command(name: 'islandora_drush_utils:update-display-hints')] |
| 86 | + #[CLI\Option(name: 'term-uri', description: 'Islandora Display term URI to update field_display_hints to.')] |
| 87 | + #[CLI\Option(name: 'dry-run', description: 'Flag to avoid making changes.')] |
| 88 | + #[HookSelector(name: 'islandora-drush-utils-user-wrap')] |
| 89 | + public function updateDisplayHints( |
| 90 | + array $options = [ |
| 91 | + 'term-uri' => self::DISPLAY_HINT_URI, |
| 92 | + 'dry-run' => self::OPT, |
| 93 | + ], |
| 94 | + ) : void { |
| 95 | + $termStorage = $this->entityTypeManager->getStorage('taxonomy_term'); |
| 96 | + $terms = $termStorage->getQuery() |
| 97 | + ->accessCheck(FALSE) |
| 98 | + ->condition('field_external_uri', $options['term-uri']) |
| 99 | + ->condition('vid', 'islandora_display') |
| 100 | + ->execute(); |
| 101 | + |
| 102 | + if (empty($terms)) { |
| 103 | + $this->messenger->addError($this->t('No terms found with URI: @uri', [ |
| 104 | + '@uri' => $options['term-uri'], |
| 105 | + ])); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + $termId = reset($terms); |
| 110 | + $nodeIds = []; |
| 111 | + |
| 112 | + while ($row = fgetcsv(STDIN)) { |
| 113 | + [$node_id] = $row; |
| 114 | + $nodeIds[] = $node_id; |
| 115 | + } |
| 116 | + |
| 117 | + $nodes = $this->entityTypeManager->getStorage('node')->loadMultiple(array_filter(array_map('trim', $nodeIds))); |
| 118 | + foreach ($nodes as $node) { |
| 119 | + if (!$options['dry-run']) { |
| 120 | + $node->set('field_display_hints', $termId); |
| 121 | + $node->save(); |
| 122 | + $this->messenger->addMessage($this->t('Updated display hints for @nid.', [ |
| 123 | + '@nid' => $node->id(), |
| 124 | + ])); |
| 125 | + } |
| 126 | + else { |
| 127 | + $this->messenger->addMessage($this->t('Would update display hints for @nid (dry run).', [ |
| 128 | + '@nid' => $node->id(), |
| 129 | + ])); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments