Browse Source

Using Authority Link fields in addition to field_external_uri

pull/745/head
dannylamb 5 years ago
parent
commit
12dfcf7be3
  1. 45
      src/IslandoraUtils.php
  2. 34
      src/Plugin/Condition/NodeHasTerm.php

45
src/IslandoraUtils.php

@ -235,12 +235,32 @@ class IslandoraUtils {
* Calling getStorage() throws if the storage handler couldn't be loaded. * Calling getStorage() throws if the storage handler couldn't be loaded.
*/ */
public function getTermForUri($uri) { public function getTermForUri($uri) {
$results = $this->entityQuery->get('taxonomy_term') // Get authority link fields to search.
->condition(self::EXTERNAL_URI_FIELD . '.uri', $uri) $field_map = $this->entityFieldManager->getFieldMap();
$fields = [];
foreach ($field_map['taxonomy_term'] as $field_name => $field_data) {
if ($field_data['type'] == 'authority_link') {
$fields[] = $field_name;
}
}
// Add field_external_uri.
$fields[] = self::EXTERNAL_URI_FIELD;
$query = $this->entityQuery->get('taxonomy_term');
$orGroup = $query->orConditionGroup();
foreach ($fields as $field) {
$orGroup->condition("$field.uri", $uri);
}
$results = $query
->condition($orGroup)
->execute(); ->execute();
if (empty($results)) { if (empty($results)) {
return NULL; return NULL;
} }
return $this->entityTypeManager->getStorage('taxonomy_term')->load(reset($results)); return $this->entityTypeManager->getStorage('taxonomy_term')->load(reset($results));
} }
@ -258,16 +278,33 @@ class IslandoraUtils {
* be created. * be created.
*/ */
public function getUriForTerm(TermInterface $term) { public function getUriForTerm(TermInterface $term) {
if ($term && $term->hasField(self::EXTERNAL_URI_FIELD)) { $fields = $this->getUriFieldNamesForTerms();
$field = $term->get(self::EXTERNAL_URI_FIELD); foreach ($fields as $field_name) {
if ($term && $term->hasField($field_name)) {
$field = $term->get($field_name);
if (!$field->isEmpty()) { if (!$field->isEmpty()) {
$link = $field->first()->getValue(); $link = $field->first()->getValue();
return $link['uri']; return $link['uri'];
} }
} }
}
return NULL; return NULL;
} }
public function getUriFieldNamesForTerms() {
// Get authority link fields to search.
$field_map = $this->entityFieldManager->getFieldMap();
$fields = [];
foreach ($field_map['taxonomy_term'] as $field_name => $field_data) {
if ($field_data['type'] == 'authority_link') {
$fields[] = $field_name;
}
}
// Add field_external_uri.
$fields[] = self::EXTERNAL_URI_FIELD;
return $fields;
}
/** /**
* Executes context reactions for a Node. * Executes context reactions for a Node.
* *

34
src/Plugin/Condition/NodeHasTerm.php

@ -123,6 +123,22 @@ class NodeHasTerm extends ConditionPluginBase implements ContainerFactoryPluginI
return parent::buildConfigurationForm($form, $form_state); return parent::buildConfigurationForm($form, $form_state);
} }
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::validateConfigurationForm($form, $form_state);
$value = $form_state->getValue('term');
foreach ($value as $target) {
$tid = $target['target_id'];
$term = $this->entityTypeManager->getStorage('taxonomy_term')->load($tid);
$uri = $this->utils->getUriForTerm($term);
if (empty($uri)) {
$form_state->setErrorByName(
'term',
$this->t('@name does not have an external URI. Give it an Authority Link or the External Uri field.', ['@name' => $term->label()])
);
}
}
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -176,15 +192,23 @@ class NodeHasTerm extends ConditionPluginBase implements ContainerFactoryPluginI
*/ */
protected function evaluateEntity(EntityInterface $entity) { protected function evaluateEntity(EntityInterface $entity) {
// Find the terms on the node. // Find the terms on the node.
$terms = array_filter($entity->referencedEntities(), function ($entity) { $field_names = $this->utils->getUriFieldNamesForTerms();
return $entity->getEntityTypeId() == 'taxonomy_term' && $terms = array_filter($entity->referencedEntities(), function ($entity) use ($field_names) {
$entity->hasField(IslandoraUtils::EXTERNAL_URI_FIELD) && if ($entity->getEntityTypeId() != 'taxonomy_term') {
!$entity->get(IslandoraUtils::EXTERNAL_URI_FIELD)->isEmpty(); 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. // Get their URIs.
$haystack = array_map(function ($term) { $haystack = array_map(function ($term) {
return $term->get(IslandoraUtils::EXTERNAL_URI_FIELD)->first()->getValue()['uri']; return $this->utils->getUriForTerm($term);
}, },
$terms $terms
); );

Loading…
Cancel
Save