From 7de7a08ac199483674fe66fdcdc3f5dec0e104f8 Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Thu, 19 Nov 2020 23:20:15 -0800 Subject: [PATCH] node_referenced_by_field condition plugin --- .../Condition/NodeReferencedByField.php | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 src/Plugin/Condition/NodeReferencedByField.php diff --git a/src/Plugin/Condition/NodeReferencedByField.php b/src/Plugin/Condition/NodeReferencedByField.php new file mode 100644 index 00000000..15de47c6 --- /dev/null +++ b/src/Plugin/Condition/NodeReferencedByField.php @@ -0,0 +1,150 @@ +entityTypeManager = $entity_type_manager; + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return parent::defaultConfiguration() + [ + 'reference_field' => 'field_member_of', + ]; + } + + /** + * {@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') + ); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $field_map = \Drupal::service('entity_field.manager')->getFieldMapByFieldType('entity_reference'); + $node_fields = array_keys($field_map['node']); + $options = array_combine($node_fields, $node_fields); + $form['reference_field'] = [ + '#type' => 'select', + '#title' => t('Field to check for reference to this node'), + '#options' => $options, + '#default_value' => $this->configuration['reference_field'], + '#required' => TRUE, + '#description' => t("Machine name of field that should be checked for references to this node."), + ]; + + return parent::buildConfigurationForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $this->configuration['reference_field'] = $form_state->getValue('reference_field'); + parent::submitConfigurationForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function evaluate() { + $node = $this->getContextValue('node'); + if (!$node) { + return FALSE; + } + return $this->evaluateEntity($node); + } + + /** + * Evaluates if an entity is referenced in the configured node field. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity to evaluate. + * + * @return bool + * TRUE if entity is referenced.. + */ + protected function evaluateEntity(EntityInterface $entity) { + $reference_field = $this->configuration['reference_field']; + $config = FieldStorageConfig::loadByName('node', $reference_field); + if ($config) { + $id_count = \Drupal::entityQuery('node') + ->condition($reference_field, $entity->id()) + ->count() + ->execute(); + return ($id_count > 0); + } + } + + /** + * {@inheritdoc} + */ + public function summary() { + if (!empty($this->configuration['negate'])) { + return $this->t('The node is not referenced in the field @field.', ['@field' => $this->configuration['reference_field']]); + } + else { + return $this->t('The node is referenced in the field @field.', ['@field' => $this->configuration['reference_field']]); + } + } + +}