Skip to content

Commit

Permalink
Add Drupal 11 support (#83)
Browse files Browse the repository at this point in the history
* Add Drupal 11 support
* Update build-3.x.yml
* verbose no longer support in phpunit
* Remove drupal/core dependency
* Drop <=10.2 support
* compatible with Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize
* Remove installSchema per https://www.drupal.org/node/3349345
* Add Drupal 11 getSupportedTypes on normalizers
  • Loading branch information
joecorall authored Oct 30, 2024
1 parent d72011f commit 8db4c55
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 60 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/build-3.x.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ jobs:
strategy:
matrix:
php-versions: ["8.1", "8.2", "8.3"]
drupal-version: ["10.2.x", "10.3.x", "10.4.x-dev"]

drupal-version: ["10.3.x", "10.4.x-dev", "11.0.x"]
exclude:
- drupal-version: "11.0.x"
php-versions: "8.1"
- drupal-version: "11.0.x"
php-versions: "8.2"
env:
DRUPAL_VERSION: ${{ matrix.drupal-version }}
SCRIPT_DIR: ${{ github.workspace }}/islandora_ci
Expand Down Expand Up @@ -108,4 +112,4 @@ jobs:
- name: PHPUNIT tests
run: |
cd $DRUPAL_DIR/web/core
$DRUPAL_DIR/vendor/bin/phpunit --verbose --debug
$DRUPAL_DIR/vendor/bin/phpunit --debug
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
"islandora/jsonld": "self.version"
},
"require" : {
"drupal/core": "^10",
"drupal/hal": "^1||^2",
"drupal/rdf": "^2.1"
"drupal/rdf": "^3.0@beta"
},
"require-dev": {
"phpunit/phpunit": "^8",
Expand Down
2 changes: 1 addition & 1 deletion jsonld.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: 'JSON-LD'
type: module
description: 'Serializes entities to JSON-LD / LDP for the Islandora Project'
package: Web services
core_version_requirement: ^10
core_version_requirement: ^10.3 || ^11
dependencies:
- drupal:serialization
- hal:hal
Expand Down
14 changes: 7 additions & 7 deletions src/ContextGenerator/JsonldContextGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Drupal\jsonld\ContextGenerator;

use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\rdf\RdfMappingInterface;
use Drupal\rdf\Entity\RdfMapping;
use Drupal\rdf\RdfMappingInterface;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -206,7 +206,7 @@ public function getFieldsRdf(RdfMappingInterface $rdfMapping, $field_name, Field

// Now we start overriding from config entity defined mappings.
// Assume all non defined mapping types as "property".
$reltype = isset($fieldRDFMapping['mapping_type']) ? $fieldRDFMapping['mapping_type'] : 'property';
$reltype = $fieldRDFMapping['mapping_type'] ?? 'property';

if (isset($fieldRDFMapping['datatype']) && ($reltype == 'property')) {
$termDefinition = ['@type' => $fieldRDFMapping['datatype']];
Expand Down Expand Up @@ -277,7 +277,7 @@ protected function writeCache(RdfMappingInterface $rdfMapping, $cid) {
* And array with the entity type and the bundle id
*/
protected function entityBundleIdsSplitter($ids) {
list($entity_type_id, $bundle_id) = explode(".", $ids, 2);
[$entity_type_id, $bundle_id] = explode(".", $ids, 2);
return ['entityTypeId' => $entity_type_id, 'bundleId' => $bundle_id];
}

Expand All @@ -297,7 +297,7 @@ protected function entityBundleIdsSplitter($ids) {
*/
protected function parseCompactedIri($iri) {
// As naive as it gets.
list($prefix, $rest) = array_pad(explode(":", $iri, 2), 2, '');
[$prefix, $rest] = array_pad(explode(":", $iri, 2), 2, '');
if ((substr($rest, 0, 2) == "//") || ($prefix == $iri)) {
// Means this was never a compacted IRI.
return ['prefix' => NULL, 'term' => $iri];
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/JsonldContextController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Drupal\jsonld\Controller;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableJsonResponse;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Controller\ControllerBase;
use Drupal\jsonld\ContextGenerator\JsonldContextGeneratorInterface;
use Drupal\rdf\Entity\RdfMapping;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\rdf\Entity\RdfMapping;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableMetadata;

/**
* Controller for retrieving jsonld contexts.
Expand Down
24 changes: 18 additions & 6 deletions src/Normalizer/ContentEntityNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\jsonld\Normalizer;

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\hal\LinkManager\LinkManagerInterface;
Expand Down Expand Up @@ -63,10 +64,12 @@ class ContentEntityNormalizer extends NormalizerBase {
* @param \Drupal\Jsonld\Utils\JsonldNormalizerUtilsInterface $normalizer_utils
* The json-ld normalizer utilities.
*/
public function __construct(LinkManagerInterface $link_manager,
EntityTypeManagerInterface $entity_manager,
ModuleHandlerInterface $module_handler,
JsonldNormalizerUtilsInterface $normalizer_utils) {
public function __construct(
LinkManagerInterface $link_manager,
EntityTypeManagerInterface $entity_manager,
ModuleHandlerInterface $module_handler,
JsonldNormalizerUtilsInterface $normalizer_utils,
) {

$this->linkManager = $link_manager;
$this->entityManager = $entity_manager;
Expand All @@ -77,7 +80,7 @@ public function __construct(LinkManagerInterface $link_manager,
/**
* {@inheritdoc}
*/
public function normalize($entity, $format = NULL, array $context = []) {
public function normalize($entity, $format = NULL, array $context = []): array|bool|string|int|float|null|\ArrayObject {

// We need to make sure that this only runs for JSON-LD.
// @todo check $format before going RDF crazy
Expand Down Expand Up @@ -195,7 +198,7 @@ public function normalize($entity, $format = NULL, array $context = []) {
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = []) {
public function denormalize(mixed $data, string $class, ?string $format = NULL, array $context = []) : mixed {

// Get type, necessary for determining which bundle to create.
if (!isset($data['_links']['type'])) {
Expand Down Expand Up @@ -304,4 +307,13 @@ protected function getTypedDataIds(array $types, array $context = []) {
return $typed_data_ids;
}

/**
* {@inheritdoc}
*/
public function getSupportedTypes(?string $format): array {
return [
ContentEntityInterface::class => TRUE,
];
}

}
27 changes: 19 additions & 8 deletions src/Normalizer/EntityReferenceItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\jsonld\Normalizer;

use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\hal\LinkManager\LinkManagerInterface;
use Drupal\jsonld\ContextGenerator\JsonldContextGeneratorInterface;
use Drupal\serialization\EntityResolver\EntityResolverInterface;
Expand Down Expand Up @@ -44,9 +45,11 @@ class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidR
* @param \Drupal\jsonld\ContextGenerator\JsonldContextGeneratorInterface $jsonld_context
* The Json-Ld context service.
*/
public function __construct(LinkManagerInterface $link_manager,
EntityResolverInterface $entity_Resolver,
JsonldContextGeneratorInterface $jsonld_context) {
public function __construct(
LinkManagerInterface $link_manager,
EntityResolverInterface $entity_Resolver,
JsonldContextGeneratorInterface $jsonld_context,
) {
parent::__construct($jsonld_context);
$this->linkManager = $link_manager;
$this->entityResolver = $entity_Resolver;
Expand All @@ -55,7 +58,7 @@ public function __construct(LinkManagerInterface $link_manager,
/**
* {@inheritdoc}
*/
public function normalize($field_item, $format = NULL, array $context = []) {
public function normalize($field_item, $format = NULL, array $context = []): array|bool|string|int|float|null|\ArrayObject {

/** @var \Drupal\Core\Field\FieldItemInterface $field_item */
$target_entity = $field_item->get('entity')->getValue();
Expand All @@ -68,7 +71,7 @@ public function normalize($field_item, $format = NULL, array $context = []) {
// If the parent entity passed in a langcode, unset it before normalizing
// the target entity. Otherwise, untranslatable fields of the target entity
// will include the langcode.
$langcode = isset($context['langcode']) ? $context['langcode'] : NULL;
$langcode = $context['langcode'] ?? NULL;
unset($context['langcode']);
// Limiting to uuid makes sure that we only get one child from base entity
// if not we could end traversing forever since there is no way
Expand All @@ -90,15 +93,14 @@ public function normalize($field_item, $format = NULL, array $context = []) {
$field_item->getParent()
->getName()
);
$field_keys = isset($field_mappings['properties']) ?
$field_mappings['properties'] :
$field_keys = $field_mappings['properties'] ??
[$field_item->getParent()->getName()];

// Value in this case is the target entity, so if a callback exists
// it should work against that.
if (!empty($field_mappings['datatype_callback'])) {
$callback = $field_mappings['datatype_callback']['callable'];
$arguments = isset($field_mappings['datatype_callback']['arguments']) ? $field_mappings['datatype_callback']['arguments'] : NULL;
$arguments = $field_mappings['datatype_callback']['arguments'] ?? NULL;
$transformed_value = call_user_func($callback, $target_entity, $arguments);
// If the config says it is an @id, we'll save it as an @id.
if (!empty($field_mappings['datatype']) && $field_mappings['datatype'] == '@id') {
Expand Down Expand Up @@ -178,4 +180,13 @@ public function getUuid($data) {
}
}

/**
* {@inheritdoc}
*/
public function getSupportedTypes(?string $format): array {
return [
EntityReferenceItem::class => TRUE,
];
}

}
19 changes: 14 additions & 5 deletions src/Normalizer/FieldItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(JsonldContextGeneratorInterface $jsonld_context) {
/**
* {@inheritdoc}
*/
public function normalize($field_item, $format = NULL, array $context = []) {
public function normalize($field_item, $format = NULL, array $context = []): array|bool|string|int|float|null|\ArrayObject {

// @todo Understand Drupal complex fields to RDF mapping
// Fields can be complex, with multiple subfields
Expand All @@ -57,14 +57,14 @@ public function normalize($field_item, $format = NULL, array $context = []) {
}
else {
// Set the uri here, and then convert it to '@id' later on.
$values_clean['@value'] = isset($values['value']) ? $values['value'] : $values['uri'];
$values_clean['@value'] = $values['value'] ?? $values['uri'];
if (isset($context['current_entity_rdf_mapping'])) {
// So why i am passing the whole rdf mapping object and not
// only the predicate? Well because i hope i will be able
// to MAP to RDF also sub fields of a complex field someday
// and somehow.
$field_mappings = $context['current_entity_rdf_mapping']->getPreparedFieldMapping($field->getName());
$field_keys = isset($field_mappings['properties']) ? $field_mappings['properties'] : [$field->getName()];
$field_keys = $field_mappings['properties'] ?? [$field->getName()];

if (!empty($field_mappings['datatype'])) {
$values_clean['@type'] = $field_mappings['datatype'];
Expand All @@ -76,7 +76,7 @@ public function normalize($field_item, $format = NULL, array $context = []) {
// For now this is a dirty solution.
if (!empty($field_mappings['datatype_callback'])) {
$callback = $field_mappings['datatype_callback']['callable'];
$arguments = isset($field_mappings['datatype_callback']['arguments']) ? $field_mappings['datatype_callback']['arguments'] : NULL;
$arguments = $field_mappings['datatype_callback']['arguments'] ?? NULL;
$values_clean['@value'] = call_user_func($callback, $values, $arguments);
}
$field_context = $this->jsonldContextgenerator->getFieldsRdf(
Expand Down Expand Up @@ -138,7 +138,7 @@ public function normalize($field_item, $format = NULL, array $context = []) {
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = []) {
public function denormalize(mixed $data, string $class, ?string $format = NULL, array $context = []) : mixed {

if (!isset($context['target_instance'])) {
throw new InvalidArgumentException('$context[\'target_instance\'] must be set to denormalize with the FieldItemNormalizer');
Expand Down Expand Up @@ -210,4 +210,13 @@ protected function createTranslatedInstance(FieldItemInterface $item, $langcode)
return $entity_translation->get($field_name)->appendItem();
}

/**
* {@inheritdoc}
*/
public function getSupportedTypes(?string $format): array {
return [
FieldItemInterface::class => TRUE,
];
}

}
13 changes: 11 additions & 2 deletions src/Normalizer/FieldNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class FieldNormalizer extends NormalizerBase {
/**
* {@inheritdoc}
*/
public function normalize($field, $format = NULL, array $context = []) {
public function normalize($field, $format = NULL, array $context = []): array|bool|string|int|float|null|\ArrayObject {

$normalized_field_items = [];

Expand Down Expand Up @@ -62,7 +62,7 @@ public function normalize($field, $format = NULL, array $context = []) {
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = []) {
public function denormalize(mixed $data, string $class, ?string $format = NULL, array $context = []) : mixed {

if (!isset($context['target_instance'])) {
throw new InvalidArgumentException('$context[\'target_instance\'] must be set to denormalize with the FieldNormalizer');
Expand Down Expand Up @@ -110,4 +110,13 @@ protected function normalizeFieldItems(FieldItemListInterface $field, $format, a
return $normalized_field_items;
}

/**
* {@inheritdoc}
*/
public function getSupportedTypes(?string $format): array {
return [
FieldItemListInterface::class => TRUE,
];
}

}
28 changes: 20 additions & 8 deletions src/Normalizer/FileEntityNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\file\FileInterface;
use Drupal\hal\LinkManager\LinkManagerInterface;
use Drupal\jsonld\Utils\JsonldNormalizerUtilsInterface;
use GuzzleHttp\ClientInterface;
Expand Down Expand Up @@ -51,12 +52,14 @@ class FileEntityNormalizer extends ContentEntityNormalizer {
* @param \Drupal\jsonld\Utils\JsonldNormalizerUtilsInterface $normalizer_utils
* The json-ld normalizer utils.
*/
public function __construct(EntityTypeManagerInterface $entity_manager,
ClientInterface $http_client,
LinkManagerInterface $link_manager,
ModuleHandlerInterface $module_handler,
FileSystemInterface $file_system,
JsonldNormalizerUtilsInterface $normalizer_utils) {
public function __construct(
EntityTypeManagerInterface $entity_manager,
ClientInterface $http_client,
LinkManagerInterface $link_manager,
ModuleHandlerInterface $module_handler,
FileSystemInterface $file_system,
JsonldNormalizerUtilsInterface $normalizer_utils,
) {

parent::__construct($link_manager, $entity_manager, $module_handler, $normalizer_utils);

Expand All @@ -67,7 +70,7 @@ public function __construct(EntityTypeManagerInterface $entity_manager,
/**
* {@inheritdoc}
*/
public function normalize($entity, $format = NULL, array $context = []) {
public function normalize($entity, $format = NULL, array $context = []): array|bool|string|int|float|null|\ArrayObject {

$data = parent::normalize($entity, $format, $context);
// Replace the file url with a full url for the file.
Expand All @@ -79,7 +82,7 @@ public function normalize($entity, $format = NULL, array $context = []) {
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = []) {
public function denormalize(mixed $data, string $class, ?string $format = NULL, array $context = []) : mixed {

$file_data = (string) $this->httpClient->get($data['uri'][0]['value'])->getBody();

Expand All @@ -89,4 +92,13 @@ public function denormalize($data, $class, $format = NULL, array $context = [])
return $this->entityManager->getStorage('file')->create($data);
}

/**
* {@inheritdoc}
*/
public function getSupportedTypes(?string $format): array {
return [
FileInterface::class => TRUE,
];
}

}
Loading

0 comments on commit 8db4c55

Please sign in to comment.