diff --git a/config/schema/islandora.schema.yml b/config/schema/islandora.schema.yml index ade716c3..7b89a25c 100644 --- a/config/schema/islandora.schema.yml +++ b/config/schema/islandora.schema.yml @@ -138,6 +138,16 @@ condition.plugin.content_entity_type: sequence: type: string +condition.plugin.node_had_namespace: + type: condition.plugin + mapping: + namespace: + type: text + label: 'Namespace' + pid_field: + type: ignore + label: 'PID field' + field.formatter.settings.islandora_image: type: mapping label: 'Image field display format settings' diff --git a/islandora.module b/islandora.module index 3a20e6b2..183adabc 100644 --- a/islandora.module +++ b/islandora.module @@ -343,6 +343,7 @@ function islandora_form_block_form_alter(&$form, FormStateInterface $form_state, // 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']); diff --git a/src/Plugin/Condition/NodeHadNamespace.php b/src/Plugin/Condition/NodeHadNamespace.php new file mode 100644 index 00000000..eb0f43d4 --- /dev/null +++ b/src/Plugin/Condition/NodeHadNamespace.php @@ -0,0 +1,187 @@ +utils = $utils; + $this->entityTypeManager = $entity_type_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('islandora.utils'), + $container->get('entity_type.manager') + ); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form['namespace'] = [ + '#type' => 'textfield', + '#title' => $this->t('Islandora 7.x Namespaces'), + '#description' => $this->t('Comma-delimited list of 7.x PID namespaces, including the trailing colon (e.g., "islandora:,ir:").'), + '#default_value' => $this->configuration['namespace'], + '#maxlength' => 256, + ]; + $field_map = \Drupal::service('entity_field.manager')->getFieldMapByFieldType('string'); + $node_fields = array_keys($field_map['node']); + $options = array_combine($node_fields, $node_fields); + $form['pid_field'] = [ + '#type' => 'select', + '#title' => t('Field that contains the PID'), + '#options' => $options, + '#default_value' => $this->configuration['pid_field'], + '#required' => TRUE, + '#description' => t("Machine name of the field that contains the PID."), + ]; + + return parent::buildConfigurationForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $this->configuration['namespace'] = NULL; + $namespace = $form_state->getValue('namespace'); + if (!empty($namespace)) { + if ($namespace) { + $this->configuration['namespace'] = $namespace; + } + } + $this->configuration['pid_field'] = $form_state->getValue('pid_field'); + parent::submitConfigurationForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function evaluate() { + if (empty($this->configuration['namespace']) && !$this->isNegated()) { + return TRUE; + } + + $node = $this->getContextValue('node'); + if (!$node) { + return FALSE; + } + return $this->evaluateEntity($node); + } + + /** + * Evaluates if the value of field_pid with a registered 7.x namespace. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity to evalute. + * + * @return bool + * TRUE if entity has the specified namespace, otherwise FALSE. + */ + protected function evaluateEntity(EntityInterface $entity) { + if ($entity->hasField('field_pid')) { + $pid_field = $this->configuration['pid_field']; + $pid_value = $entity->get($pid_field)->getValue(); + $pid = $pid_value[0]['value']; + $namespace = strtok($pid, ':') . ':'; + $registered_namespaces = explode(',', $this->configuration['namespace']); + foreach ($registered_namespaces as &$registered_namespace) { + $registered_namespace = trim($registered_namespace); + if (in_array($namespace, $registered_namespaces)) { + return $this->isNegated() ? FALSE : TRUE; + } + } + } + + return $this->isNegated() ? TRUE : FALSE; + } + + /** + * {@inheritdoc} + */ + public function summary() { + if (!empty($this->configuration['negate'])) { + return $this->t('The node does not have a value in its PID field with the namespace @namespace.', ['@namespace' => $this->configuration['namespace']]); + } + else { + return $this->t('The node has a value in its PID field with the namespace @namespace.', ['@namespace' => $this->configuration['namespace']]); + } + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return array_merge( + ['namespace' => '', 'pid_field' => 'field_pid'], + parent::defaultConfiguration() + ); + } + +}