forked from alxp/islandora
-
Notifications
You must be signed in to change notification settings - Fork 118
/
NodeHasTerm.php
241 lines (217 loc) · 6.62 KB
/
NodeHasTerm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
namespace Drupal\islandora\Plugin\Condition;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\islandora\IslandoraUtils;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a 'Term' condition for nodes.
*
* @Condition(
* id = "node_has_term",
* label = @Translation("Node has term with URI"),
* context_definitions = {
* "node" = @ContextDefinition("entity:node", required = TRUE , label = @Translation("node"))
* }
* )
*/
class NodeHasTerm extends ConditionPluginBase implements ContainerFactoryPluginInterface {
/**
* Islandora utils.
*
* @var \Drupal\islandora\IslandoraUtils
*/
protected $utils;
/**
* Term storage.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructor.
*
* @param array $configuration
* The plugin configuration, i.e. an array with configuration values keyed
* by configuration option name. The special key 'context' may be used to
* initialize the defined contexts by setting it to an array of context
* values keyed by context names.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\islandora\IslandoraUtils $utils
* Islandora utils.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Entity type manager.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
IslandoraUtils $utils,
EntityTypeManagerInterface $entity_type_manager
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->utils = $utils;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('islandora.utils'),
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return array_merge(
[
'logic' => 'and',
'uri' => NULL,
],
parent::defaultConfiguration()
);
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$default = [];
if (isset($this->configuration['uri']) && !empty($this->configuration['uri'])) {
$uris = explode(',', $this->configuration['uri']);
foreach ($uris as $uri) {
$default[] = $this->utils->getTermForUri($uri);
}
}
$form['term'] = [
'#type' => 'entity_autocomplete',
'#title' => $this->t('Term'),
'#description' => $this->t('Only terms that have external URIs/URLs will appear here.'),
'#tags' => TRUE,
'#default_value' => $default,
'#target_type' => 'taxonomy_term',
'#selection_handler' => 'islandora:external_uri',
];
$form['logic'] = [
'#type' => 'radios',
'#title' => $this->t('Logic'),
'#description' => $this->t('Whether to use AND or OR logic to evaluate multiple terms'),
'#options' => [
'and' => 'And',
'or' => 'Or',
],
'#default_value' => $this->configuration['logic'],
];
return parent::buildConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
// Set URI for term if possible.
$this->configuration['uri'] = NULL;
$value = $form_state->getValue('term');
$uris = [];
if (!empty($value)) {
foreach ($value as $target) {
$tid = $target['target_id'];
$term = $this->entityTypeManager->getStorage('taxonomy_term')->load($tid);
$uri = $this->utils->getUriForTerm($term);
if ($uri) {
$uris[] = $uri;
}
}
if (!empty($uris)) {
$this->configuration['uri'] = implode(',', $uris);
}
}
$this->configuration['logic'] = $form_state->getValue('logic');
parent::submitConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function evaluate() {
if (empty($this->configuration['uri']) && !$this->isNegated()) {
return TRUE;
}
$node = $this->getContextValue('node');
if (!$node) {
return FALSE;
}
return $this->evaluateEntity($node);
}
/**
* Evaluates if an entity has the specified term(s).
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to evalute.
*
* @return bool
* TRUE if entity has all the specified term(s), otherwise FALSE.
*/
protected function evaluateEntity(EntityInterface $entity) {
// Find the terms on the node.
$field_names = $this->utils->getUriFieldNamesForTerms();
$terms = array_filter($entity->referencedEntities(), function ($entity) use ($field_names) {
if ($entity->getEntityTypeId() != 'taxonomy_term') {
return FALSE;
}
foreach ($field_names as $field_name) {
if ($entity->hasField($field_name) && !$entity->get($field_name)->isEmpty()) {
return TRUE;
}
}
return FALSE;
});
// Get their URIs.
$haystack = array_map(function ($term) {
return $this->utils->getUriForTerm($term);
},
$terms
);
// FALSE if there's no URIs on the node.
if (empty($haystack)) {
return FALSE;
}
// Get the URIs to look for. It's a required field, so there
// will always be one.
$needles = explode(',', $this->configuration['uri']);
// TRUE if every needle is in the haystack.
if ($this->configuration['logic'] == 'and') {
if (count(array_intersect($needles, $haystack)) == count($needles)) {
return TRUE;
}
return FALSE;
}
// TRUE if any needle is in the haystack.
else {
if (count(array_intersect($needles, $haystack)) > 0) {
return TRUE;
}
return FALSE;
}
}
/**
* {@inheritdoc}
*/
public function summary() {
if (!empty($this->configuration['negate'])) {
return $this->t('The node is not associated with taxonomy term with uri @uri.', ['@uri' => $this->configuration['uri']]);
}
else {
return $this->t('The node is associated with taxonomy term with uri @uri.', ['@uri' => $this->configuration['uri']]);
}
}
}