Skip to content

Commit

Permalink
Add configuration form to remove ?_format=jsonld from @ids (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
dannylamb authored and seth-shaw-unlv committed May 21, 2019
1 parent 89910a2 commit faa8301
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 8 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions config/install/jsonld.settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
remove_jsonld_format: false
7 changes: 7 additions & 0 deletions config/schema/jsonld.schema.yml
Original file line number Diff line number Diff line change
@@ -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'
6 changes: 6 additions & 0 deletions jsonld.links.menu.yml
Original file line number Diff line number Diff line change
@@ -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'
9 changes: 9 additions & 0 deletions jsonld.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
4 changes: 2 additions & 2 deletions jsonld.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
61 changes: 61 additions & 0 deletions src/Form/JsonLdSettingsForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Drupal\jsonld\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
* Class JsonLdSettingsForm.
*
* @package Drupal\jsonld\Form
*/
class JsonLdSettingsForm extends ConfigFormBase {

const CONFIG_NAME = 'jsonld.settings';

const REMOVE_JSONLD_FORMAT = 'remove_jsonld_format';

/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
self::CONFIG_NAME,
];
}

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'jsonld_admin_settings';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->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);
}

}
27 changes: 24 additions & 3 deletions src/Normalizer/ContentEntityNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -43,6 +45,13 @@ class ContentEntityNormalizer extends NormalizerBase {
*/
protected $moduleHandler;

/**
* The configuration.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;

/**
* Constructs an ContentEntityNormalizer object.
*
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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();
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Normalizer/FileEntityNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion tests/src/Kernel/JsonldKernelTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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) {

Expand All @@ -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([
Expand Down

0 comments on commit faa8301

Please sign in to comment.