From 7de7a08ac199483674fe66fdcdc3f5dec0e104f8 Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Thu, 19 Nov 2020 23:20:15 -0800 Subject: [PATCH 1/4] 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']]); + } + } + +} From 4b1103e05f89c024c545b8d050d4559170b357ee Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Mon, 30 Nov 2020 13:35:02 -0800 Subject: [PATCH 2/4] Update and rename NodeReferencedByField.php to NodeReferencedByNode.php --- ...eferencedByField.php => NodeReferencedByNode.php} | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) rename src/Plugin/Condition/{NodeReferencedByField.php => NodeReferencedByNode.php} (87%) diff --git a/src/Plugin/Condition/NodeReferencedByField.php b/src/Plugin/Condition/NodeReferencedByNode.php similarity index 87% rename from src/Plugin/Condition/NodeReferencedByField.php rename to src/Plugin/Condition/NodeReferencedByNode.php index 15de47c6..c086358a 100644 --- a/src/Plugin/Condition/NodeReferencedByField.php +++ b/src/Plugin/Condition/NodeReferencedByNode.php @@ -11,17 +11,17 @@ use Drupal\field\Entity\FieldStorageConfig; use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Condition to detect if is a node is referenced by a configured field. + * Condition to detect if is a node is referenced by another node using the configured field. * * @Condition( - * id = "node_referenced_by_field", - * label = @Translation("Node referenced by field"), + * id = "node_referenced_by_node", + * label = @Translation("Node is referenced by other nodes"), * context = { * "node" = @ContextDefinition("entity:node", required = TRUE , label = @Translation("node")) * } * ) */ -class NodeReferencedByField extends ConditionPluginBase implements ContainerFactoryPluginInterface { +class NodeReferencedByNode extends ConditionPluginBase implements ContainerFactoryPluginInterface { /** * Node storage. @@ -140,10 +140,10 @@ class NodeReferencedByField extends ConditionPluginBase implements ContainerFact */ 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']]); + return $this->t('The node is not referenced in another node\'s field `@field`.', ['@field' => $this->configuration['reference_field']]); } else { - return $this->t('The node is referenced in the field @field.', ['@field' => $this->configuration['reference_field']]); + return $this->t('The node is referenced in another node\'s field `@field`.', ['@field' => $this->configuration['reference_field']]); } } From 69326432ec16b2803aac5bf925ae8031a84bd993 Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Tue, 1 Dec 2020 13:24:31 -0800 Subject: [PATCH 3/4] coding style --- src/Plugin/Condition/NodeReferencedByNode.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Plugin/Condition/NodeReferencedByNode.php b/src/Plugin/Condition/NodeReferencedByNode.php index c086358a..acb03dfa 100644 --- a/src/Plugin/Condition/NodeReferencedByNode.php +++ b/src/Plugin/Condition/NodeReferencedByNode.php @@ -11,7 +11,7 @@ use Drupal\field\Entity\FieldStorageConfig; use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Condition to detect if is a node is referenced by another node using the configured field. + * Condition for a node referenced by another node using the configured field. * * @Condition( * id = "node_referenced_by_node", @@ -140,10 +140,10 @@ class NodeReferencedByNode extends ConditionPluginBase implements ContainerFacto */ public function summary() { if (!empty($this->configuration['negate'])) { - return $this->t('The node is not referenced in another node\'s field `@field`.', ['@field' => $this->configuration['reference_field']]); + return $this->t("The node is not referenced in another node's field `@field`.", ['@field' => $this->configuration['reference_field']]); } else { - return $this->t('The node is referenced in another node\'s field `@field`.', ['@field' => $this->configuration['reference_field']]); + return $this->t("The node is referenced in another node's field `@field`.", ['@field' => $this->configuration['reference_field']]); } } From 1b3338e47ebf6680e534b797b0e60bcf27b0d90c Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Wed, 2 Dec 2020 12:17:39 -0800 Subject: [PATCH 4/4] hide node_referenced_by_node in block placement UI ... also sort them. --- islandora.module | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/islandora.module b/islandora.module index 88db562d..85771ee3 100644 --- a/islandora.module +++ b/islandora.module @@ -414,16 +414,17 @@ function islandora_form_block_form_alter(&$form, FormStateInterface $form_state, // /admin/structure/context instead if you want to use these conditions // to alter block layout. unset($form['visibility']['content_entity_type']); - unset($form['visibility']['parent_node_has_term']); - unset($form['visibility']['node_had_namespace']); - unset($form['visibility']['media_has_term']); unset($form['visibility']['file_uses_filesystem']); - unset($form['visibility']['node_has_term']); - unset($form['visibility']['node_has_parent']); - unset($form['visibility']['media_uses_filesystem']); unset($form['visibility']['media_has_mimetype']); - unset($form['visibility']['node_is_islandora_object']); + unset($form['visibility']['media_has_term']); unset($form['visibility']['media_is_islandora_media']); + unset($form['visibility']['media_uses_filesystem']); + unset($form['visibility']['node_had_namespace']); + unset($form['visibility']['node_has_parent']); + unset($form['visibility']['node_has_term']); + unset($form['visibility']['node_is_islandora_object']); + unset($form['visibility']['node_referenced_by_node']); + unset($form['visibility']['parent_node_has_term']); } /**