clean install

This commit is contained in:
2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 changed files with 3555714 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
<?php
namespace Drupal\jsonapi_extras\Plugin;
/**
* Base class for date and time based resourceFieldEnhancer plugins.
*/
abstract class DateTimeEnhancerBase extends ResourceFieldEnhancerBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'dateTimeFormat' => \DateTime::ISO8601,
];
}
/**
* {@inheritdoc}
*/
public function getOutputJsonSchema() {
return [
'type' => 'string',
];
}
/**
* {@inheritdoc}
*/
public function getSettingsForm(array $resource_field_info) {
$settings = empty($resource_field_info['enhancer']['settings'])
? $this->getConfiguration()
: $resource_field_info['enhancer']['settings'];
return [
'dateTimeFormat' => [
'#type' => 'textfield',
'#title' => $this->t('Format'),
'#description' => $this->t('Use a valid date format.'),
'#default_value' => $settings['dateTimeFormat'],
],
];
}
}

View File

@@ -0,0 +1,96 @@
<?php
namespace Drupal\jsonapi_extras\Plugin;
use Drupal\Core\Plugin\PluginBase;
use JsonSchema\Constraints\Constraint;
use JsonSchema\Validator;
use Shaper\DataAdaptor\DataAdaptorTransformerTrait;
use Shaper\DataAdaptor\DataAdaptorValidatorTrait;
use Shaper\Validator\AcceptValidator;
use Shaper\Validator\JsonSchemaValidator;
/**
* Common base class for resourceFieldEnhancer plugins.
*
* @see \Drupal\jsonapi_extras\Annotation\ResourceFieldEnhancer
* @see \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager
* @see \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerInterface
* @see plugin_api
*
* @ingroup third_party
*/
abstract class ResourceFieldEnhancerBase extends PluginBase implements ResourceFieldEnhancerInterface {
use DataAdaptorValidatorTrait;
use DataAdaptorTransformerTrait;
/**
* Holds the plugin configuration.
*
* @var array
*/
protected $configuration;
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [];
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
// @todo This should have a dependency on the resource_config entity.
return [];
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return $this->configuration
? $this->configuration
: $this->setConfiguration([]);
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = $configuration + $this->defaultConfiguration();
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function getSettingsForm(array $resource_field_info) {
return [];
}
/**
* {@inheritdoc}
*/
public function getInternalValidator() {
return new AcceptValidator();
}
/**
* {@inheritdoc}
*/
public function getInputValidator() {
// @todo Implement a getInputJsonSchema method.
return new AcceptValidator();
}
/**
* {@inheritdoc}
*/
public function getOutputValidator() {
return new JsonSchemaValidator($this->getOutputJsonSchema(), new Validator(), Constraint::CHECK_MODE_TYPE_CAST);
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Drupal\jsonapi_extras\Plugin;
use Shaper\DataAdaptor\ReversibleTransformationInterface;
use Shaper\DataAdaptor\ReversibleTransformationValidationInterface;
use Shaper\Transformation\TransformationInterface;
use Shaper\Transformation\TransformationValidationInterface;
/**
* Provides an interface defining a ResourceFieldEnhancer entity.
*/
interface ResourceFieldEnhancerInterface extends TransformationInterface, ReversibleTransformationInterface, TransformationValidationInterface, ReversibleTransformationValidationInterface {
/**
* Get the JSON Schema for the new output.
*
* @return array
* An structured array representing the JSON Schema of the new output.
*/
public function getOutputJsonSchema();
/**
* Get a form element to render the settings.
*
* @param array $resource_field_info
* The resource field info.
*
* @return array
* The form element array.
*/
public function getSettingsForm(array $resource_field_info);
}

View File

@@ -0,0 +1,61 @@
<?php
namespace Drupal\jsonapi_extras\Plugin;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
/**
* Manages discovery and instantiation of resourceFieldEnhancer plugins.
*/
class ResourceFieldEnhancerManager extends DefaultPluginManager {
/**
* Constructs a new ResourceFieldEnhancerManager.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct(
'Plugin/jsonapi/FieldEnhancer',
$namespaces,
$module_handler,
'Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerInterface',
'Drupal\jsonapi_extras\Annotation\ResourceFieldEnhancer'
);
$this->alterInfo('resource_field_enhancer_info');
$this->setCacheBackend($cache_backend, 'resource_field_enhancer_plugins');
}
/**
* {@inheritdoc}
*/
protected function alterDefinitions(&$definitions) {
// Loop through all definitions.
foreach ($definitions as $definition_key => $definition_info) {
// Check to see if dependencies key is set.
if (!empty($definition_info['dependencies'])) {
$definition_dependencies = $definition_info['dependencies'];
// Loop through dependencies to confirm if enabled.
foreach ($definition_dependencies as $dependency) {
// If dependency is not enabled removed from list of definitions.
if (!$this->moduleHandler->moduleExists($dependency)) {
unset($definitions[$definition_key]);
continue;
}
}
}
}
parent::alterDefinitions($definitions);
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Drupal\jsonapi_extras\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
/**
* The constraint object.
*
* @Constraint(
* id = "jsonapi_extras__duplicate_field",
* label = @Translation("Duplicate field", context = "Validation")
* )
*/
class DuplicateFieldConstraint extends Constraint {
/**
* The error message for the constraint.
*
* @var string
*/
public $message = 'The override must be unique.';
}

View File

@@ -0,0 +1,90 @@
<?php
namespace Drupal\jsonapi_extras\Plugin\Validation\Constraint;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* The validator.
*/
class DuplicateFieldConstraintValidator extends ConstraintValidator {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* DuplicateFieldConstraintValidator constructor.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager = NULL) {
$this->entityTypeManager = $entityTypeManager ?: \Drupal::entityTypeManager();
}
/**
* {@inheritdoc}
*/
public function validate($entity_data, Constraint $constraint) {
$resourceFields = $entity_data['resourceFields'];
$overrides = [];
// Get the field values.
foreach ($resourceFields as $field => $data) {
// Only get the overridden fields.
if ($data['fieldName'] != $data['publicName']) {
// Store the publicName for comparison.
$overrides[$field] = $data['publicName'];
}
}
// Compare the overrides and find any duplicate values.
$deduped_overrides = array_unique($overrides);
$dupes = array_diff_assoc($overrides, $deduped_overrides);
// Set an error if there are duplicates.
if ($dupes) {
foreach ($dupes as $field => $value) {
$this->context->buildViolation($constraint->message)
->atPath("resourceFields.$field.publicName")
->addViolation();
}
}
// Now compare the overrides with the default names to validate no dupes
// exist.
foreach ($overrides as $field => $override) {
if (array_key_exists($override, $resourceFields)) {
$this->context->buildViolation($constraint->message)
->atPath("resourceFields.$field.publicName")
->addViolation();
}
}
// Validate URL and resource type.
$resource_types = $this->entityTypeManager
->getStorage('jsonapi_resource_config')
->loadByProperties(['disabled' => FALSE]);
foreach ($resource_types as $id => $resource_type) {
if ($entity_data['id'] == $id) {
continue;
}
if ($resource_type->get('resourceType') == $entity_data['resourceType']) {
$this->context->buildViolation(
'There is already resource (@name) with this resource type.',
['@name' => $resource_type->id()]
)
->atPath('resourceType')
->addViolation();
}
if ($resource_type->get('path') == $entity_data['path']) {
$this->context->buildViolation('There is already resource (@name) with this path.', ['@name' => $resource_type->id()])
->atPath('resourceType')
->addViolation();
}
}
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\jsonapi_extras\Plugin\DateTimeEnhancerBase;
use Shaper\Util\Context;
/**
* Perform additional manipulations to timestamp fields.
*
* @ResourceFieldEnhancer(
* id = "date_time",
* label = @Translation("Date Time (Timestamp field)"),
* description = @Translation("Formats a date based the configured date format for timestamp fields."),
* dependencies = {"datetime"}
* )
*/
class DateTimeEnhancer extends DateTimeEnhancerBase {
/**
* {@inheritdoc}
*/
protected function doUndoTransform($data, Context $context) {
$date = new \DateTime();
$date->setTimestamp(is_int($data)
? $data
: (new DrupalDateTime($data))->getTimestamp()
);
$configuration = $this->getConfiguration();
return $date->format($configuration['dateTimeFormat']);
}
/**
* {@inheritdoc}
*/
protected function doTransform($data, Context $context) {
$date = new \DateTime($data);
return (int) $date->format('U');
}
}

View File

@@ -0,0 +1,84 @@
<?php
namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\jsonapi_extras\Plugin\DateTimeEnhancerBase;
use Shaper\Util\Context;
/**
* Perform additional manipulations to datetime fields.
*
* @ResourceFieldEnhancer(
* id = "date_time_from_string",
* label = @Translation("Date Time (Date Time field)"),
* description = @Translation("Formats a date based the configured date format for date fields."),
* dependencies = {"datetime"}
* )
*/
class DateTimeFromStringEnhancer extends DateTimeEnhancerBase {
/**
* {@inheritdoc}
*/
protected function doUndoTransform($data, Context $context) {
$configuration = $this->getConfiguration();
$reformat = function ($input) use ($configuration) {
$storage_timezone = new \DateTimezone(DateTimeItemInterface::STORAGE_TIMEZONE);
$date = new \DateTime($input, $storage_timezone);
$output_timezone = new \DateTimezone(date_default_timezone_get());
$date->setTimezone($output_timezone);
$output = $date->format($configuration['dateTimeFormat']);
return $output;
};
$result = is_array($data) ? array_map($reformat, $data) : $reformat($data);
return $result;
}
/**
* {@inheritdoc}
*/
protected function doTransform($data, Context $context) {
$reformat = function ($input) {
$date = new \DateTime($input);
// Adjust the date for storage.
$storage_timezone = new \DateTimezone(DateTimeItemInterface::STORAGE_TIMEZONE);
$date->setTimezone($storage_timezone);
$output = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
return $output;
};
$result = is_array($data) ? array_map($reformat, $data) : $reformat($data);
return $result;
}
/**
* {@inheritdoc}
*/
public function getOutputJsonSchema() {
$baseType = parent::getOutputJsonSchema();
return [
"anyOf" => [
$baseType,
["type" => "array", "items" => $baseType],
[
"type" => "object",
"properties" => [
"value" => $baseType,
"end_value" => $baseType,
],
],
],
];
}
}

View File

@@ -0,0 +1,94 @@
<?php
namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase;
use Shaper\Util\Context;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Perform additional manipulations to JSON fields.
*
* @ResourceFieldEnhancer(
* id = "json",
* label = @Translation("JSON Field"),
* description = @Translation("Render JSON Field has real json")
* )
*/
class JSONFieldEnhancer extends ResourceFieldEnhancerBase implements ContainerFactoryPluginInterface {
/**
* The serialization json.
*
* @var Drupal\Component\serialization\Json
*/
protected $encoder;
/**
* Constructs a new JSONFieldEnhancer.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Component\Serialization\Json $encoder
* The serialization json.
*/
public function __construct(array $configuration, string $plugin_id, $plugin_definition, Json $encoder) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->encoder = $encoder;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('serialization.json'));
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [];
}
/**
* {@inheritdoc}
*/
public function doUndoTransform($data, Context $context) {
return $this->encoder->decode($data);
}
/**
* {@inheritdoc}
*/
protected function doTransform($data, Context $context) {
return $this->encoder->encode($data);
}
/**
* {@inheritdoc}
*/
public function getOutputJsonSchema() {
return [
'oneOf' => [
['type' => 'object'],
['type' => 'array'],
['type' => 'null'],
],
];
}
/**
* {@inheritdoc}
*/
public function getSettingsForm(array $resource_field_info) {
return [];
}
}

View File

@@ -0,0 +1,70 @@
<?php
namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase;
use Drupal\options\Plugin\Field\FieldType\ListItemBase;
use Shaper\Util\Context;
/**
* Perform additional manipulations to list fields.
*
* @ResourceFieldEnhancer(
* id = "list",
* label = @Translation("List Field"),
* description = @Translation("Formats a list field based on labels and values.")
* )
*/
class ListFieldEnhancer extends ResourceFieldEnhancerBase {
/**
* {@inheritDoc}
*/
protected function doTransform($data, Context $context) {
return is_array($data) ? array_column($data, 'value') : $data;
}
/**
* {@inheritDoc}
*/
protected function doUndoTransform($data, Context $context) {
$field_context = $context->offsetGet('field_item_object');
assert($field_context instanceof ListItemBase);
$options = $field_context->getPossibleOptions();
$reformat = static function ($input) use ($options) {
return [
'value' => $input,
'label' => $options[(string) $input] ?? '',
];
};
return is_array($data) ? array_map($reformat, $data) : $reformat($data);
}
/**
* {@inheritDoc}
*/
public function getOutputJsonSchema(): array {
return [
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => [
'value' => [
'anyOf' => [
['type' => 'string'],
['type' => 'number'],
['type' => 'null'],
],
],
'label' => [
'anOf' => [
['type' => 'string'],
['type' => 'null'],
],
],
],
],
];
}
}

View File

@@ -0,0 +1,90 @@
<?php
namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase;
use Shaper\Util\Context;
/**
* Perform additional manipulations to date fields.
*
* @ResourceFieldEnhancer(
* id = "nested",
* label = @Translation("Single Nested Property"),
* description = @Translation("Extracts or wraps nested properties from an object.")
* )
*/
class SingleNestedEnhancer extends ResourceFieldEnhancerBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'path' => 'value',
];
}
/**
* {@inheritdoc}
*/
protected function doUndoTransform($data, Context $context) {
$output = $data;
$configuration = $this->getConfiguration();
$path = $configuration['path'];
$path_parts = explode('.', $path);
// Start drilling down until there are no more path parts.
while ($output && ($path_part = array_shift($path_parts))) {
$output = empty($output[$path_part])
? NULL
: $output[$path_part];
}
return $output;
}
/**
* {@inheritdoc}
*/
protected function doTransform($data, Context $context) {
$input = $data;
$configuration = $this->getConfiguration();
$path = $configuration['path'];
$path_parts = explode('.', $path);
// Start wrapping up until there are no more path parts.
while ($path_part = array_pop($path_parts)) {
$input = [$path_part => $input];
}
return $input;
}
/**
* {@inheritdoc}
*/
public function getOutputJsonSchema() {
return [
'oneOf' => [
['type' => 'string'],
['type' => 'null'],
],
];
}
/**
* {@inheritdoc}
*/
public function getSettingsForm(array $resource_field_info) {
$settings = empty($resource_field_info['enhancer']['settings'])
? $this->getConfiguration()
: $resource_field_info['enhancer']['settings'];
return [
'path' => [
'#type' => 'textfield',
'#title' => $this->t('Path'),
'#description' => $this->t('A dot separated path to extract the sub-property.'),
'#default_value' => $settings['path'],
],
];
}
}

View File

@@ -0,0 +1,162 @@
<?php
namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase;
use Shaper\Util\Context;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Add URL aliases to links.
*
* @ResourceFieldEnhancer(
* id = "url_link",
* label = @Translation("URL for link (link field only)"),
* description = @Translation("Use Url for link fields.")
* )
*/
class UrlLinkEnhancer extends ResourceFieldEnhancerBase implements ContainerFactoryPluginInterface {
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The logger service.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* Constructs UrlLinkEnhancer.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger service.
*/
public function __construct(
array $configuration,
$plugin_id,
array $plugin_definition,
LanguageManagerInterface $language_manager,
LoggerChannelFactoryInterface $logger_factory,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->languageManager = $language_manager;
$this->logger = $logger_factory->get('jsonapi_extras');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('language_manager'),
$container->get('logger.factory')
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'absolute_url' => 0,
];
}
/**
* {@inheritDoc}
*/
public function getSettingsForm(array $resource_field_info) {
$settings = empty($resource_field_info['enhancer']['settings'])
? $this->getConfiguration()
: $resource_field_info['enhancer']['settings'];
$form = parent::getSettingsForm($resource_field_info);
$form['absolute_url'] = [
'#type' => 'checkbox',
'#title' => $this->t('Get Absolute Urls'),
'#default_value' => $settings['absolute_url'],
];
return $form;
}
/**
* {@inheritdoc}
*/
protected function doUndoTransform($data, Context $context) {
if (isset($data['uri'])) {
try {
$url = Url::fromUri($data['uri'], ['language' => $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)]);
// Use absolute urls if configured.
$configuration = $this->getConfiguration();
if ($configuration['absolute_url']) {
$url->setAbsolute(TRUE);
}
$data['url'] = $url->toString();
}
catch (\Exception $e) {
$this->logger->error('Failed to create a URL from uri @uri. Error: @error', [
'@uri' => $data['uri'],
'@error' => $e->getMessage(),
]);
}
}
return $data;
}
/**
* {@inheritdoc}
*/
protected function doTransform($value, Context $context) {
}
/**
* {@inheritdoc}
*/
public function getOutputJsonSchema() {
return [
'type' => 'object',
'properties' => [
'uri' => ['type' => 'string'],
'title' => [
'anyOf' => [
['type' => 'null'],
['type' => 'string'],
],
],
'options' => [
'anyOf' => [
['type' => 'array'],
['type' => 'object'],
],
],
'url' => ['type' => 'string'],
],
];
}
}

View File

@@ -0,0 +1,111 @@
<?php
namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase;
use Shaper\Util\Context;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Use UUID for internal link field value.
*
* @ResourceFieldEnhancer(
* id = "uuid_link",
* label = @Translation("UUID for link (link field only)"),
* description = @Translation("Use UUID for internal link field.")
* )
*/
class UuidLinkEnhancer extends ResourceFieldEnhancerBase implements ContainerFactoryPluginInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
protected function doUndoTransform($data, Context $context) {
if (isset($data['uri'])) {
// Check if it is a link to an entity.
preg_match("/entity:(.*)\/(.*)/", $data['uri'], $parsed_uri);
if (!empty($parsed_uri)) {
$entity_type = $parsed_uri[1];
$entity_id = $parsed_uri[2];
$entity = $this->entityTypeManager->getStorage($entity_type)->load($entity_id);
if (!is_null($entity)) {
$data['uri'] = 'entity:' . $entity_type . '/' . $entity->bundle() . '/' . $entity->uuid();
}
// Remove the value.
else {
$data = [
'uri' => '',
'title' => '',
'options' => [],
];
}
}
}
return $data;
}
/**
* {@inheritdoc}
*/
protected function doTransform($value, Context $context) {
if (isset($value['uri'])) {
// Check if it is a link to an entity.
preg_match("/entity:(.*)\/(.*)\/(.*)/", $value['uri'], $parsed_uri);
if (!empty($parsed_uri)) {
$entity_type = $parsed_uri[1];
$entity_uuid = $parsed_uri[3];
$entities = $this->entityTypeManager->getStorage($entity_type)->loadByProperties(['uuid' => $entity_uuid]);
if (!empty($entities)) {
$entity = array_shift($entities);
$value['uri'] = 'entity:' . $entity_type . '/' . $entity->id();
}
else {
// If the entity has not been imported yet we unset the field value.
$value = [];
}
}
}
return $value;
}
/**
* {@inheritdoc}
*/
public function getOutputJsonSchema() {
return [
'type' => 'object',
];
}
}