diff --git a/README.md b/README.md index 88b5cec..2e1480b 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,12 @@ This module adds a simple Drupal entity to JSON-LD classes. It depends on RDF module and existing fields to rdf properties mappings to do it's job. +## Configuration + +The JSON-LD normalizer adds a `?_format=jsonld` to all URIs by default. + +You can disable this via a checkbox in the Configuration -> Search and Metadata -> JsonLD form. + ## Maintainers Current maintainers: diff --git a/config/install/jsonld.settings.yml b/config/install/jsonld.settings.yml new file mode 100644 index 0000000..6deb979 --- /dev/null +++ b/config/install/jsonld.settings.yml @@ -0,0 +1 @@ +remove_jsonld_format: false diff --git a/config/schema/jsonld.schema.yml b/config/schema/jsonld.schema.yml new file mode 100644 index 0000000..1338fe0 --- /dev/null +++ b/config/schema/jsonld.schema.yml @@ -0,0 +1,7 @@ +jsonld.settings: + type: config_object + label: 'JSONLD Settings' + mapping: + remove_jsonld_format: + type: boolean + label: 'If checked, ?_format=jsonld will be stripped from the end of urls in JSONLD' diff --git a/jsonld.links.menu.yml b/jsonld.links.menu.yml new file mode 100644 index 0000000..e2ef0d7 --- /dev/null +++ b/jsonld.links.menu.yml @@ -0,0 +1,6 @@ +# Core configuration form +system.jsonld_settings: + title: 'JsonLD' + parent: system.admin_config_search + route_name: system.jsonld_settings + description: 'Configure Jsonld settings' diff --git a/jsonld.routing.yml b/jsonld.routing.yml index a61b2c6..fd16a26 100644 --- a/jsonld.routing.yml +++ b/jsonld.routing.yml @@ -5,3 +5,12 @@ jsonld.context: _controller: '\Drupal\jsonld\Controller\JsonldContextController::content' requirements: _permission: 'access content' + +# Core Jsonld configuration form +system.jsonld_settings: + path: '/admin/config/search/jsonld' + defaults: + _form: '\Drupal\jsonld\Form\JsonLdSettingsForm' + _title: 'JsonLD Settings' + requirements: + _permission: 'administer site configuration' diff --git a/jsonld.services.yml b/jsonld.services.yml index 0cfaf15..39da23b 100644 --- a/jsonld.services.yml +++ b/jsonld.services.yml @@ -17,10 +17,10 @@ services: class: Drupal\jsonld\Normalizer\FileEntityNormalizer tags: - { name: normalizer, priority: 20 } - arguments: ['@entity_type.manager', '@http_client', '@hal.link_manager', '@module_handler', '@file_system'] + arguments: ['@entity_type.manager', '@http_client', '@hal.link_manager', '@module_handler', '@file_system', '@config.factory'] serializer.normalizer.entity.jsonld: class: Drupal\jsonld\Normalizer\ContentEntityNormalizer - arguments: ['@hal.link_manager', '@entity_type.manager', '@module_handler'] + arguments: ['@hal.link_manager', '@entity_type.manager', '@module_handler', '@config.factory'] tags: - { name: normalizer, priority: 10 } serializer.encoder.jsonld: diff --git a/src/Form/JsonLdSettingsForm.php b/src/Form/JsonLdSettingsForm.php new file mode 100644 index 0000000..a070ea5 --- /dev/null +++ b/src/Form/JsonLdSettingsForm.php @@ -0,0 +1,61 @@ +config(self::CONFIG_NAME); + $form = [ + self::REMOVE_JSONLD_FORMAT => [ + '#type' => 'checkbox', + '#title' => $this->t('Remove jsonld parameter from @ids'), + '#description' => $this->t('This will alter any @id parameters to remove "?_format=jsonld"'), + '#default_value' => $config->get(self::REMOVE_JSONLD_FORMAT) ? $config->get(self::REMOVE_JSONLD_FORMAT) : FALSE, + ], + ]; + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $config = $this->configFactory->getEditable(self::CONFIG_NAME); + $config->set(self::REMOVE_JSONLD_FORMAT, $form_state->getValue(self::REMOVE_JSONLD_FORMAT)) + ->save(); + parent::submitForm($form, $form_state); + } + +} diff --git a/src/Normalizer/ContentEntityNormalizer.php b/src/Normalizer/ContentEntityNormalizer.php index 99315f9..8b56bd7 100644 --- a/src/Normalizer/ContentEntityNormalizer.php +++ b/src/Normalizer/ContentEntityNormalizer.php @@ -2,10 +2,12 @@ namespace Drupal\jsonld\Normalizer; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\hal\LinkManager\LinkManagerInterface; +use Drupal\jsonld\Form\JsonLdSettingsForm; use Symfony\Component\Serializer\Exception\UnexpectedValueException; /** @@ -43,6 +45,13 @@ class ContentEntityNormalizer extends NormalizerBase { */ protected $moduleHandler; + /** + * The configuration. + * + * @var \Drupal\Core\Config\ImmutableConfig + */ + protected $config; + /** * Constructs an ContentEntityNormalizer object. * @@ -52,12 +61,18 @@ class ContentEntityNormalizer extends NormalizerBase { * The entity manager. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The configuration factory. */ - public function __construct(LinkManagerInterface $link_manager, EntityTypeManagerInterface $entity_manager, ModuleHandlerInterface $module_handler) { + public function __construct(LinkManagerInterface $link_manager, + EntityTypeManagerInterface $entity_manager, + ModuleHandlerInterface $module_handler, + ConfigFactoryInterface $config_factory) { $this->linkManager = $link_manager; $this->entityManager = $entity_manager; $this->moduleHandler = $module_handler; + $this->config = $config_factory->get(JsonLdSettingsForm::CONFIG_NAME); } /** @@ -252,10 +267,16 @@ protected function getEntityUri(EntityInterface $entity) { // Some entity types don't provide a canonical link template, at least call // out to ->url(). if ($entity->isNew() || !$entity->hasLinkTemplate('canonical')) { - return $entity->toUrl('canonical', []); + if ($entity->getEntityTypeId() == 'file') { + return $entity->url(); + } + return ""; } $url = $entity->toUrl('canonical', ['absolute' => TRUE]); - return $url->setRouteParameter('_format', 'jsonld')->toString(); + if (!$this->config->get(JsonLdSettingsForm::REMOVE_JSONLD_FORMAT)) { + $url->setRouteParameter('_format', 'jsonld'); + } + return $url->toString(); } /** diff --git a/src/Normalizer/FileEntityNormalizer.php b/src/Normalizer/FileEntityNormalizer.php index 0fe5198..01057b7 100644 --- a/src/Normalizer/FileEntityNormalizer.php +++ b/src/Normalizer/FileEntityNormalizer.php @@ -2,6 +2,7 @@ namespace Drupal\jsonld\Normalizer; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\File\FileSystemInterface; @@ -47,14 +48,17 @@ class FileEntityNormalizer extends ContentEntityNormalizer { * The module handler. * @param \Drupal\Core\File\FileSystemInterface $file_system * The file system handler. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The configuration factory. */ public function __construct(EntityTypeManagerInterface $entity_manager, ClientInterface $http_client, LinkManagerInterface $link_manager, ModuleHandlerInterface $module_handler, - FileSystemInterface $file_system) { + FileSystemInterface $file_system, + ConfigFactoryInterface $config_factory) { - parent::__construct($link_manager, $entity_manager, $module_handler); + parent::__construct($link_manager, $entity_manager, $module_handler, $config_factory); $this->httpClient = $http_client; $this->fileSystem = $file_system; diff --git a/tests/src/Kernel/JsonldKernelTestBase.php b/tests/src/Kernel/JsonldKernelTestBase.php index 07c3936..76673cc 100644 --- a/tests/src/Kernel/JsonldKernelTestBase.php +++ b/tests/src/Kernel/JsonldKernelTestBase.php @@ -170,7 +170,7 @@ protected function setUp() { // Set up the mock serializer. $normalizers = [ - new ContentEntityNormalizer($link_manager, $entity_manager, \Drupal::moduleHandler()), + new ContentEntityNormalizer($link_manager, $entity_manager, \Drupal::moduleHandler(), \Drupal::service('config.factory')), new EntityReferenceItemNormalizer($link_manager, $chain_resolver, $jsonld_context_generator), new FieldItemNormalizer($jsonld_context_generator), new FieldNormalizer(), @@ -190,6 +190,9 @@ protected function setUp() { * * @return string * The entity URI. + * + * @throws \Drupal\Core\Entity\EntityMalformedException + * The toUrl() call fails. */ protected function getEntityUri(EntityInterface $entity) { @@ -207,6 +210,11 @@ protected function getEntityUri(EntityInterface $entity) { * * @return array * with [ the entity, the normalized array ]. + * + * @throws \Drupal\Core\Entity\EntityStorageException + * Problem saving the entity. + * @throws \Exception + * Problem creating a DateTime. */ protected function generateTestEntity() { $target_entity = EntityTest::create([