View source
<?php
namespace Drupal\entity_field_condition\Plugin\Condition;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
class NodeField extends ConditionPluginBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $entityFieldManager;
protected $fieldTypePluginManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_plugin_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->fieldTypePluginManager = $field_type_plugin_manager;
}
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('entity_field.manager'), $container
->get('plugin.manager.field.field_type'));
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['entity_bundle'] = [
'#type' => 'select',
'#title' => $this
->t('Node type'),
'#options' => $this
->getNodeTypes(),
'#validated' => TRUE,
'#ajax' => [
'callback' => [
$this,
'fieldsCallback',
],
'wrapper' => 'field-wrapper',
'event' => 'change',
'progress' => [
'type' => 'throbber',
'message' => $this
->t('Loading fields...'),
],
],
'#default_value' => $this->configuration['entity_bundle'],
];
$form['field'] = [
'#type' => 'select',
'#prefix' => '<div id="field-wrapper">',
'#suffix' => '</div>',
'#title' => $this
->t('Field'),
'#validated' => TRUE,
'#options' => $this
->getNodeFields($this->configuration['entity_bundle']),
'#default_value' => $this->configuration['field'],
];
$form['value_source'] = [
'#type' => 'select',
'#title' => $this
->t('Value Source'),
'#options' => [
'null' => $this
->t('Is NULL'),
'specified' => $this
->t('Specified'),
],
'#default_value' => $this->configuration['value_source'],
];
$form['value'] = [
'#type' => 'textfield',
'#title' => $this
->t('Value to be compared'),
'#default_value' => $this->configuration['value'],
];
return parent::buildConfigurationForm($form, $form_state);
}
protected function getNodeTypes() {
$node_types = $this->entityTypeManager
->getStorage('node_type')
->loadMultiple();
$node_type_options = $this
->getEmptyOption();
foreach ($node_types as $node_type) {
$node_type_options[$node_type
->id()] = $node_type
->label();
}
return $node_type_options;
}
public function getEmptyOption() {
return [
'' => $this
->t('None'),
];
}
protected function getNodeFields($node_type) {
$labels = $this
->getEmptyOption();
if (empty($node_type)) {
return $labels;
}
$node_fields = $this->entityFieldManager
->getFieldDefinitions('node', $node_type);
$field_types = $this->fieldTypePluginManager
->getDefinitions();
foreach ($node_fields as $field) {
$field_type_label = $field_types[$field
->getType()]['label']
->getUntranslatedString();
$labels[$field_type_label][$field
->getName()] = $field
->getLabel();
}
return $labels;
}
public function fieldsCallback(array $form, FormStateInterface $form_state) {
$node_type = $form_state
->getValues()['visibility']['node_field']['entity_bundle'];
$form['visibility']['node_field']['field']['#options'] = $this
->getNodeFields($node_type);
return $form['visibility']['node_field']['field'];
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$entity_bundle = $form_state
->getValue('entity_bundle');
$field = $form_state
->getValue('field');
if ($entity_bundle && empty($field)) {
$form_state
->setErrorByName('field', $this
->t('If you select a node type, you must specify a field.'));
}
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['entity_bundle'] = $form_state
->getValue('entity_bundle');
$this->configuration['field'] = $form_state
->getValue('field');
$this->configuration['value_source'] = $form_state
->getValue('value_source');
$this->configuration['value'] = $form_state
->getValue('value');
parent::submitConfigurationForm($form, $form_state);
}
public function defaultConfiguration() {
$configuration = [
'entity_type_id' => 'node',
'entity_bundle' => '',
'field' => '',
'value_source' => 'null',
'value' => '',
];
return $configuration + parent::defaultConfiguration();
}
public function evaluate() {
if (empty($this->configuration['field']) && !$this
->isNegated()) {
return TRUE;
}
$entity_type_id = $this->configuration['entity_type_id'];
$entity_bundle = $this->configuration['entity_bundle'];
$field = $this->configuration['field'];
$entity = $this
->getContextValue($entity_type_id);
if (is_subclass_of($entity, 'Drupal\\Core\\Entity\\ContentEntityBase') && $entity
->getEntityTypeId() === $entity_type_id && $entity
->getType() === $entity_bundle) {
$value = $entity
->get($field)
->getValue();
$value_to_compare = NULL;
if (is_array($value)) {
if (!empty($value)) {
foreach ($value as $value_item) {
if (isset($value_item['target_id'])) {
$value_to_compare = $value_item['target_id'];
}
elseif (isset($value_item['uri'])) {
$value_to_compare = $value_item['uri'];
}
else {
$value_to_compare = $value_item['value'];
}
if ($value_to_compare === $this->configuration['value']) {
return TRUE;
}
}
}
}
else {
$value_to_compare = $value;
}
if ($this->configuration['value_source'] === 'null') {
return is_null($value_to_compare);
}
return $value_to_compare === $this->configuration['value'];
}
return FALSE;
}
public function summary() {
$entity_type_id = $this->configuration['entity_type_id'];
$entity_type_definition = $this->entityTypeManager
->getDefinition($entity_type_id);
$entity_bundle = $this->configuration['entity_bundle'];
$field = $this->configuration['field'];
$field_label = '';
foreach ($this->entityFieldManager
->getFieldDefinitions($entity_type_id, $entity_bundle) as $field_definition) {
if ($field_definition
->getName() === $field) {
$field_label = (string) $field_definition
->getLabel();
}
}
return $this
->t('@entity_type "@entity_bundle" field "@field" is "@value"', [
'@entity_type' => $entity_type_definition
->getLabel(),
'@entity_bundle' => $entity_bundle,
'@field' => $field_label,
'@value' => $this->configuration['value_source'] === 'null' ? 'is NULL' : $this->configuration['value'],
]);
}
}