View source
<?php
namespace Drupal\term_condition\Plugin\Condition;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Term extends ConditionPluginBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_manager, array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('entity_type.manager'), $configuration, $plugin_id, $plugin_definition);
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$default_terms = [];
if (!empty($this->configuration['tid'])) {
$terms = $this->configuration['tid'];
if (!empty($terms)) {
if (is_array($terms)) {
foreach ($terms as $key => $term) {
$term = array_pop($term);
$default_terms[] = $this->entityTypeManager
->getStorage('taxonomy_term')
->load($term);
}
}
else {
$default_terms = $this->entityTypeManager
->getStorage('taxonomy_term')
->load($terms);
}
}
}
$form['tid'] = array(
'#type' => 'entity_autocomplete',
'#title' => $this
->t('Select taxonomy term(s)'),
'#default_value' => $default_terms,
'#target_type' => 'taxonomy_term',
'#tags' => TRUE,
);
return parent::buildConfigurationForm($form, $form_state);
}
public function defaultConfiguration() {
return [
'tid' => NULL,
] + parent::defaultConfiguration();
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['tid'] = $form_state
->getValue('tid');
parent::submitConfigurationForm($form, $form_state);
}
public function evaluate() {
if (empty($this->configuration['tid']) && !$this
->isNegated()) {
return TRUE;
}
if (is_array($this->configuration['tid'])) {
$tids = $this->configuration['tid'];
unset($this->configuration['tid']);
foreach ($tids as $tid) {
$this->configuration['tid'][] = array_pop($tid);
}
}
$entity = $this
->getContextValue('node');
if (!$entity) {
$potentialRouteMatches = [
'taxonomy_term' => 'taxonomy_term',
'node' => 'node',
'node_revision' => 'node_revision',
];
foreach ($potentialRouteMatches as $key => $potentialRouteMatch) {
$entity = \Drupal::routeMatch()
->getParameter($potentialRouteMatch);
if ($entity instanceof EntityInterface) {
break;
}
elseif (is_string($entity)) {
$entity = $this->entityTypeManager
->getStorage($key)
->loadRevision($entity);
break;
}
}
if (!$entity) {
return FALSE;
}
}
foreach ($entity
->referencedEntities() as $referenced_entity) {
if (is_array($this->configuration['tid'])) {
if ($referenced_entity
->getEntityTypeId() == 'taxonomy_term' && in_array($referenced_entity
->id(), $this->configuration['tid'])) {
return TRUE;
}
}
else {
if ($referenced_entity
->getEntityTypeId() == 'taxonomy_term' && $referenced_entity
->id() == $this->configuration['tid']) {
return TRUE;
}
}
}
return FALSE;
}
public function summary() {
$terms = [];
$term_ids = $this->configuration['tid'];
foreach ($term_ids as $term_id) {
$terms[] = $this->entityTypeManager
->getStorage('taxonomy_term')
->load(array_pop($term_id))
->getName();
}
$term_list = implode(", ", $terms);
if (!empty($this->configuration['negate'])) {
return $this
->t('The node is not associated with taxonomy term(s) @term_list.', array(
'@term_list' => $term_list,
));
}
else {
return $this
->t('The node is associated with taxonomy term(s) @term_list.', array(
'@term_list' => $term_list,
));
}
}
}