diff --git a/src/Plugin/Condition/EntityBundle.php b/src/Plugin/Condition/EntityBundle.php new file mode 100644 index 00000000..5ec1a19c --- /dev/null +++ b/src/Plugin/Condition/EntityBundle.php @@ -0,0 +1,96 @@ +getBundleInfo($content_entity); + foreach ($bundles as $bundle => $bundle_properties) { + $options[$bundle] = $this->t('@bundle (@type)', [ + '@bundle' => $bundle_properties['label'], + '@type' => $content_entity, + ]); + } + } + + $form['bundles'] = [ + '#title' => $this->t('Bundles'), + '#type' => 'checkboxes', + '#options' => $options, + '#default_value' => $this->configuration['bundles'], + ]; + + return parent::buildConfigurationForm($form, $form_state);; + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $this->configuration['bundles'] = array_filter($form_state->getValue('bundles')); + parent::submitConfigurationForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function evaluate() { + foreach (\Drupal::routeMatch()->getParameters()->keys() as $type) { + if ($this->getContext($type)->hasContextValue()) { + $entity = $this->getContextValue($type); + if (!empty($this->configuration['bundles'][$entity->bundle()])) { + return !$this->isNegated(); + } + } + } + return $this->isNegated(); + } + + /** + * {@inheritdoc} + */ + public function summary() { + if (empty($this->configuration['bundles'])) { + return $this->t('No bundles are selected.'); + } + + return $this->t( + 'Entity bundle in the list: @bundles', + [ + '@bundles' => implode(', ', $this->configuration['field']), + ] + ); + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return array_merge( + ['bundles' => []], + parent::defaultConfiguration() + ); + } + +} diff --git a/src/Plugin/ContextReaction/JsonldTypeAlterReaction.php b/src/Plugin/ContextReaction/JsonldTypeAlterReaction.php new file mode 100644 index 00000000..e130a68e --- /dev/null +++ b/src/Plugin/ContextReaction/JsonldTypeAlterReaction.php @@ -0,0 +1,92 @@ +t('Alter JSON-LD Type context reaction.'); + } + + /** + * {@inheritdoc} + */ + public function execute(EntityInterface $entity = NULL, array &$normalized = NULL, array $context = NULL) { + $config = $this->getConfiguration(); + // Use a pre-configured field as the source of the additional @type. + if (($entity->hasField($config['source_field'])) && + (!empty($entity->get($config['source_field'])->getValue()))) { + if (isset($normalized['@graph']) && is_array($normalized['@graph'])) { + foreach ($normalized['@graph'] as &$graph) { + foreach ($entity->get($config['source_field'])->getValue() as $type) { + // If the configured field is using an entity reference, + // we will see if it uses the core config's field_external_uri. + if (array_key_exists('target_id', $type)) { + $target_type = $entity->get($config['source_field'])->getFieldDefinition()->getSetting('target_type'); + $referenced_entity = \Drupal::entityTypeManager()->getStorage($target_type)->load($type['target_id']); + if ($referenced_entity->hasField('field_external_uri') && + !empty($referenced_entity->get('field_external_uri')->getValue())) { + foreach ($referenced_entity->get('field_external_uri')->getValue() as $value) { + $graph['@type'][] = $value['uri']; + } + } + } + else { + $graph['@type'][] = NormalizerBase::escapePrefix($type['value'], $context['namespaces']); + } + } + } + } + } + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $options = []; + $fieldsArray = \Drupal::service('entity_field.manager')->getFieldMap(); + foreach ($fieldsArray as $entity_type => $entity_fields) { + foreach ($entity_fields as $field => $field_properties) { + $options[$field] = $this->t('@field (@bundles)', [ + '@field' => $field, + '@bundles' => implode(', ', array_keys($field_properties['bundles'])), + ]); + } + } + + $config = $this->getConfiguration(); + $form['source_field'] = [ + '#type' => 'select', + '#title' => $this->t('Source Field'), + '#options' => $options, + '#description' => $this->t("Select the field containing the type predicates."), + '#default_value' => isset($config['source_field']) ? $config['source_field'] : '', + ]; + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $this->setConfiguration(['source_field' => $form_state->getValue('source_field')]); + } + +}