Skip to content

Commit ffb5ede

Browse files
Merge pull request #32 from discoverygarden/MRDI-16
MRDI-16: Add command to update field_display_hints.
2 parents 318d245 + 549fe0e commit ffb5ede

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,33 @@ There are a few related commands:
118118

119119
Before Drush 9, there was a "--user" option that could be used to run commands as other users. Here, a new "@islandora_drush_utils-user-wrap" annotation is provided, which can be used to allow the --user option in commands.
120120

121+
### Display hint updater
122+
123+
When rolling out additional viewers, depending on how the front end is
124+
configured, it may be necessary to update the display hints for nodes.
125+
126+
The provided Drush commands will:
127+
1. `islandora_drush_utils:display-hint-feeder` - Returns all nodes with any
128+
specified taxonomy term URI(s) representing model(s) to update within
129+
`field_model`. Defaults to `https://schema.org/Book`.
130+
2. `islandora_drush_utils:update-display-hints` - Update all nodes passed with a
131+
specified taxonomy term URI in a replacement scenario. Defaults to
132+
`https://projectmirador.org`.
133+
134+
To invoke the updater command:
135+
```bash
136+
drush islandora_drush_utils:display-hint-feeder --user=1 --term-uris='https://schema.org/Book' > nodes.csv
137+
drush islandora_drush_utils:update-display-hints --user=1 --term-uri='https://projectmirador.org' < nodes.csv
138+
```
139+
140+
An alternative, more optimal approach would be to use GNU Parallel with
141+
two processes each processing 100 items at a time:
142+
143+
```bash
144+
drush islandora_drush_utils:display-hint-feeder --user=1 --term-uris='https://schema.org/Book' | parallel --pipe --max-args 100 -j2 drush islandora_drush_utils:update-display-hints --user=1 --term-uri='https://projectmirador.org'
145+
```
146+
147+
121148
## Troubleshooting/Issues
122149

123150
Having problems or solved a problem? Contact [discoverygarden](http://support.discoverygarden.ca).

drush.10.services.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,10 @@ services:
5959
- '@queue'
6060
tags:
6161
- name: drush.command
62+
islandora_drush_utils.display_hint_updater:
63+
class: \Drupal\islandora_drush_utils\Drush\Commands\UpdateDisplayHints
64+
arguments:
65+
- '@entity_type.manager'
66+
- '@messenger'
67+
tags:
68+
- name: drush.command
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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

Comments
 (0)