Browse Source

Respecting jsonld module configuration for ?_format=jsonld (#134)

* Respecting jsonld module configuration for ?_format=jsonld
* Using 0.2.0 of jsonld
pull/729/head
dannylamb 6 years ago committed by Seth Shaw
parent
commit
6956a85d3c
  1. 2
      composer.json
  2. 56
      src/ContextReaction/NormalizerAlterReaction.php
  3. 2
      src/Plugin/ContextReaction/JsonldTypeAlterReaction.php
  4. 24
      src/Plugin/ContextReaction/MappingUriPredicateReaction.php

2
composer.json

@ -17,7 +17,7 @@
"require": { "require": {
"drupal/context": "^4.0", "drupal/context": "^4.0",
"drupal/search_api": "~1.8", "drupal/search_api": "~1.8",
"islandora/jsonld": "dev-8.x-1.x", "islandora/jsonld": "0.2.0",
"stomp-php/stomp-php": "4.*", "stomp-php/stomp-php": "4.*",
"drupal/jwt": "1.0.0-alpha6", "drupal/jwt": "1.0.0-alpha6",
"drupal/filehash": "^1.1", "drupal/filehash": "^1.1",

56
src/ContextReaction/NormalizerAlterReaction.php

@ -3,7 +3,11 @@
namespace Drupal\islandora\ContextReaction; namespace Drupal\islandora\ContextReaction;
use Drupal\context\ContextReactionPluginBase; use Drupal\context\ContextReactionPluginBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\jsonld\Form\JsonLdSettingsForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Base class to alter the normalizes Json-ld. * Base class to alter the normalizes Json-ld.
@ -12,10 +16,41 @@ use Drupal\Core\Entity\EntityInterface;
* *
* @package Drupal\islandora\ContextReaction * @package Drupal\islandora\ContextReaction
*/ */
abstract class NormalizerAlterReaction extends ContextReactionPluginBase { abstract class NormalizerAlterReaction extends ContextReactionPluginBase implements ContainerFactoryPluginInterface {
/** /**
* This reaction takes can alter the array of json-ld built from the entity. * The configuration.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $jsonldConfig;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration,
$plugin_id,
$plugin_definition,
ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->jsonldConfig = $config_factory->get(JsonLdSettingsForm::CONFIG_NAME);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('config.factory')
);
}
/**
* This reaction can alter the array of json-ld built from the entity.
* *
* @param \Drupal\Core\Entity\EntityInterface|null $entity * @param \Drupal\Core\Entity\EntityInterface|null $entity
* The entity we are normalizing. * The entity we are normalizing.
@ -26,4 +61,21 @@ abstract class NormalizerAlterReaction extends ContextReactionPluginBase {
*/ */
abstract public function execute(EntityInterface $entity = NULL, array &$normalized = NULL, array $context = NULL); abstract public function execute(EntityInterface $entity = NULL, array &$normalized = NULL, array $context = NULL);
/**
* Helper function to get the url for an entity that repsects jsonld config.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity.
*
* @return string
* The url.
*/
protected function getSubjectUrl(EntityInterface $entity) {
$url = $entity->toUrl('canonical', ['absolute' => TRUE]);
if (!$this->jsonldConfig->get(JsonLdSettingsForm::REMOVE_JSONLD_FORMAT)) {
$url->setRouteParameter('_format', 'jsonld');
}
return $url->toString();
}
} }

2
src/Plugin/ContextReaction/JsonldTypeAlterReaction.php

@ -43,7 +43,7 @@ class JsonldTypeAlterReaction extends NormalizerAlterReaction {
// Search for the entity in the graph. // Search for the entity in the graph.
foreach ($normalized['@graph'] as &$elem) { foreach ($normalized['@graph'] as &$elem) {
if ($elem['@id'] === $entity->toUrl()->setAbsolute()->toString() . '?_format=jsonld') { if ($elem['@id'] === $this->getSubjectUrl($entity)) {
foreach ($entity->get($config['source_field'])->getValue() as $type) { foreach ($entity->get($config['source_field'])->getValue() as $type) {
// If the configured field is using an entity reference, // If the configured field is using an entity reference,
// we will see if it uses the core config's field_external_uri. // we will see if it uses the core config's field_external_uri.

24
src/Plugin/ContextReaction/MappingUriPredicateReaction.php

@ -2,9 +2,9 @@
namespace Drupal\islandora\Plugin\ContextReaction; namespace Drupal\islandora\Plugin\ContextReaction;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\islandora\ContextReaction\NormalizerAlterReaction; use Drupal\islandora\ContextReaction\NormalizerAlterReaction;
use Drupal\islandora\MediaSource\MediaSourceService; use Drupal\islandora\MediaSource\MediaSourceService;
use Drupal\jsonld\Normalizer\NormalizerBase; use Drupal\jsonld\Normalizer\NormalizerBase;
@ -19,7 +19,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* label = @Translation("Map URI to predicate") * label = @Translation("Map URI to predicate")
* ) * )
*/ */
class MappingUriPredicateReaction extends NormalizerAlterReaction implements ContainerFactoryPluginInterface { class MappingUriPredicateReaction extends NormalizerAlterReaction {
const URI_PREDICATE = 'drupal_uri_predicate'; const URI_PREDICATE = 'drupal_uri_predicate';
@ -28,8 +28,18 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction implements Con
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function __construct(array $configuration, $plugin_id, $plugin_definition, MediaSourceService $media_source) { public function __construct(array $configuration,
parent::__construct($configuration, $plugin_id, $plugin_definition); $plugin_id,
$plugin_definition,
ConfigFactoryInterface $config_factory,
MediaSourceService $media_source) {
parent::__construct(
$configuration,
$plugin_id,
$plugin_definition,
$config_factory
);
$this->mediaSource = $media_source; $this->mediaSource = $media_source;
} }
@ -41,6 +51,7 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction implements Con
$configuration, $configuration,
$plugin_id, $plugin_id,
$plugin_definition, $plugin_definition,
$container->get('config.factory'),
$container->get('islandora.media_source_service') $container->get('islandora.media_source_service')
); );
} }
@ -59,10 +70,7 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction implements Con
$config = $this->getConfiguration(); $config = $this->getConfiguration();
$drupal_predicate = $config[self::URI_PREDICATE]; $drupal_predicate = $config[self::URI_PREDICATE];
if (!is_null($drupal_predicate) && !empty($drupal_predicate)) { if (!is_null($drupal_predicate) && !empty($drupal_predicate)) {
$url = $entity $url = $this->getSubjectUrl($entity);
->toUrl('canonical', ['absolute' => TRUE])
->setRouteParameter('_format', 'jsonld')
->toString();
if ($context['needs_jsonldcontext'] === FALSE) { if ($context['needs_jsonldcontext'] === FALSE) {
$drupal_predicate = NormalizerBase::escapePrefix($drupal_predicate, $context['namespaces']); $drupal_predicate = NormalizerBase::escapePrefix($drupal_predicate, $context['namespaces']);
} }

Loading…
Cancel
Save