diff --git a/config/schema/islandora.schema.yml b/config/schema/islandora.schema.yml index 9c2d33fe..ade716c3 100644 --- a/config/schema/islandora.schema.yml +++ b/config/schema/islandora.schema.yml @@ -83,6 +83,16 @@ condition.plugin.node_has_term: type: text label: 'Taxonomy Term URI' +condition.plugin.node_has_parent: + type: condition.plugin + mapping: + parent_nid: + type: integer + label: 'Parent node' + parent_reference_field: + type: string + label: 'Parent reference field' + condition.plugin.media_has_term: type: condition.plugin mapping: diff --git a/islandora.module b/islandora.module index 5d55da16..3a20e6b2 100644 --- a/islandora.module +++ b/islandora.module @@ -346,6 +346,7 @@ function islandora_form_block_form_alter(&$form, FormStateInterface $form_state, 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']); } diff --git a/src/Plugin/Condition/NodeHasParent.php b/src/Plugin/Condition/NodeHasParent.php new file mode 100644 index 00000000..df95c6ad --- /dev/null +++ b/src/Plugin/Condition/NodeHasParent.php @@ -0,0 +1,168 @@ +entityTypeManager = $entity_type_manager; + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return parent::defaultConfiguration() + [ + 'parent_reference_field' => 'field_member_of', + 'parent_nid' => '', + ]; + } + + /** + * {@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) { + $form['parent_nid'] = [ + '#type' => 'entity_autocomplete', + '#title' => t('Parent node'), + '#default_value' => $this->entityTypeManager->getStorage('node')->load($this->configuration['parent_nid']), + '#required' => TRUE, + '#description' => t("Can be a collection node or a compound object."), + '#target_type' => 'node', + ]; + $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['parent_reference_field'] = [ + '#type' => 'select', + '#title' => t('Field that contains reference to parents'), + '#options' => $options, + '#default_value' => $this->configuration['parent_reference_field'], + '#required' => TRUE, + '#description' => t("Machine name of field that contains references to parent node."), + ]; + + return parent::buildConfigurationForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $this->configuration['parent_nid'] = $form_state->getValue('parent_nid'); + $this->configuration['parent_reference_field'] = $form_state->getValue('parent_reference_field'); + parent::submitConfigurationForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function evaluate() { + if (empty($this->configuration['parent_nid']) && !$this->isNegated()) { + return TRUE; + } + + $node = $this->getContextValue('node'); + if (!$node) { + return FALSE; + } + return $this->evaluateEntity($node); + } + + /** + * Evaluates if an entity has the specified node as its parent. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity to evalute. + * + * @return bool + * TRUE if entity references the specified parent. + */ + protected function evaluateEntity(EntityInterface $entity) { + foreach ($entity->referencedEntities() as $referenced_entity) { + if ($entity->getEntityTypeID() == 'node' && $referenced_entity->getEntityTypeId() == 'node') { + $parent_reference_field = $this->configuration['parent_reference_field']; + $field = $entity->get($parent_reference_field); + if (!$field->isEmpty()) { + $nids = $field->getValue(); + foreach ($nids as $nid) { + if ($nid['target_id'] == $this->configuration['parent_nid']) { + return $this->isNegated() ? FALSE : TRUE; + } + } + } + } + } + } + + /** + * {@inheritdoc} + */ + public function summary() { + if (!empty($this->configuration['negate'])) { + return $this->t('The node does not have node @nid as its parent.', ['@nid' => $this->configuration['parent_nid']]); + } + else { + return $this->t('The node has node @nid as its parent.', ['@nid' => $this->configuration['parent_nid']]); + } + } + +}