Browse Source

Filtering out terms that don't have external uris from the autocomplete

pull/745/head
dannylamb 5 years ago
parent
commit
e417b0a84c
  1. 21
      src/Plugin/Condition/NodeHasTerm.php
  2. 125
      src/Plugin/EntityReferenceSelection/ExternalUriSelection.php

21
src/Plugin/Condition/NodeHasTerm.php

@ -104,10 +104,12 @@ class NodeHasTerm extends ConditionPluginBase implements ContainerFactoryPluginI
$form['term'] = [ $form['term'] = [
'#type' => 'entity_autocomplete', '#type' => 'entity_autocomplete',
'#title' => $this->t('Term'), '#title' => $this->t('Term'),
'#description' => $this->t('Terms need an Authority Link or External Uri to be compatible with this Condition. If the term you are looking for is not appearing in the autocomplete, please ensure it has a value for its Authority Link or External Uri field.'),
'#tags' => TRUE, '#tags' => TRUE,
'#default_value' => $default, '#default_value' => $default,
'#target_type' => 'taxonomy_term', '#target_type' => 'taxonomy_term',
'#required' => TRUE, '#required' => TRUE,
'#selection_handler' => 'islandora:external_uri',
]; ];
$form['logic'] = [ $form['logic'] = [
@ -123,25 +125,6 @@ class NodeHasTerm extends ConditionPluginBase implements ContainerFactoryPluginI
return parent::buildConfigurationForm($form, $form_state); return parent::buildConfigurationForm($form, $form_state);
} }
/**
* {@inheritdoc}
*/
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}
*/ */

125
src/Plugin/EntityReferenceSelection/ExternalUriSelection.php

@ -0,0 +1,125 @@
<?php
namespace Drupal\islandora\Plugin\EntityReferenceSelection;
use Drupal\taxonomy\Plugin\EntityReferenceSelection\TermSelection;
use Drupal\islandora\IslandoraUtils;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Filters by looking for entities with Authority Links or External Uris.
*
* @EntityReferenceSelection(
* id = "islandora:external_uri",
* label = @Translation("Taxonomy Term with external URI selection"),
* entity_types = {"taxonomy_term"},
* group = "islandora",
* weight = 1
* )
*/
class ExternalUriSelection extends TermSelection {
/**
* Islandora utils.
*
* @var \Drupal\islandora\IslandoraUtils
*/
protected $utils;
/**
* Constructs a new ExternalUriSelection object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity manager service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle info service.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
* @param \Drupal\islandora\IslandoraUtils $utils
* Islandora utils.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
EntityTypeManagerInterface $entity_type_manager,
ModuleHandlerInterface $module_handler,
AccountInterface $current_user,
EntityFieldManagerInterface $entity_field_manager = NULL,
EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL,
EntityRepositoryInterface $entity_repository = NULL,
IslandoraUtils $utils
) {
parent::__construct(
$configuration,
$plugin_id,
$plugin_definition,
$entity_type_manager,
$module_handler,
$current_user,
$entity_field_manager,
$entity_type_bundle_info,
$entity_repository
);
$this->utils = $utils;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager'),
$container->get('module_handler'),
$container->get('current_user'),
$container->get('entity_field.manager'),
$container->get('entity_type.bundle.info'),
$container->get('entity.repository'),
$container->get('islandora.utils')
);
}
/**
* {@inheritdoc}
*/
public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
$options = parent::getReferenceableEntities($match, $match_operator, $limit);
foreach (array_keys($options) as $vid) {
foreach (array_keys($options[$vid]) as $tid) {
$term = $this->entityTypeManager->getStorage('taxonomy_term')->load($tid);
$uri = $this->utils->getUriForTerm($term);
if (empty($uri)) {
unset($options[$vid][$tid]);
}
}
if (empty($options[$vid])) {
unset($options[$vid]);
}
}
return $options;
}
}
Loading…
Cancel
Save