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.
*/
public function getTermForUri($uri) {
$results = $this->entityQuery->get('taxonomy_term')
->condition(self::EXTERNAL_URI_FIELD . '.uri', $uri)
// 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;
$query = $this->entityQuery->get('taxonomy_term');
$orGroup = $query->orConditionGroup();
foreach ($fields as $field) {
$orGroup->condition("$field.uri", $uri);
}
$results = $query
->condition($orGroup)
->execute();
if (empty($results)) {
return NULL;
}
return $this->entityTypeManager->getStorage('taxonomy_term')->load(reset($results));
}
@ -258,16 +278,33 @@ class IslandoraUtils {
* be created.
*/
public function getUriForTerm(TermInterface $term) {
if ($term && $term->hasField(self::EXTERNAL_URI_FIELD)) {
$field = $term->get(self::EXTERNAL_URI_FIELD);
$fields = $this->getUriFieldNamesForTerms();
foreach ($fields as $field_name) {
if ($term && $term->hasField($field_name)) {
$field = $term->get($field_name);
if (!$field->isEmpty()) {
$link = $field->first()->getValue();
return $link['uri'];
}
}
}
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.
*

34
src/Plugin/Condition/NodeHasTerm.php

@ -123,6 +123,22 @@ class NodeHasTerm extends ConditionPluginBase implements ContainerFactoryPluginI
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}
*/
@ -176,15 +192,23 @@ class NodeHasTerm extends ConditionPluginBase implements ContainerFactoryPluginI
*/
protected function evaluateEntity(EntityInterface $entity) {
// Find the terms on the node.
$terms = array_filter($entity->referencedEntities(), function ($entity) {
return $entity->getEntityTypeId() == 'taxonomy_term' &&
$entity->hasField(IslandoraUtils::EXTERNAL_URI_FIELD) &&
!$entity->get(IslandoraUtils::EXTERNAL_URI_FIELD)->isEmpty();
$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 $term->get(IslandoraUtils::EXTERNAL_URI_FIELD)->first()->getValue()['uri'];
return $this->utils->getUriForTerm($term);
},
$terms
);

Loading…
Cancel
Save