diff --git a/src/Plugin/ResourceFieldEnhancerBase.php b/src/Plugin/ResourceFieldEnhancerBase.php index 1fa76832eebbce58a5bd9c220cf4db0438a57564..fa053d667a6547ace596d67fa4b849d771d3f982 100644 --- a/src/Plugin/ResourceFieldEnhancerBase.php +++ b/src/Plugin/ResourceFieldEnhancerBase.php @@ -23,6 +23,13 @@ abstract class ResourceFieldEnhancerBase extends PluginBase implements ResourceF */ protected $configuration; + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return []; + } + /** * {@inheritdoc} */ @@ -48,4 +55,11 @@ abstract class ResourceFieldEnhancerBase extends PluginBase implements ResourceF return $this->configuration; } + /** + * {@inheritdoc} + */ + public function getSettingsForm(array $resource_field_info) { + return []; + } + } diff --git a/src/Plugin/jsonapi/FieldEnhancer/UuidLinkEnhancer.php b/src/Plugin/jsonapi/FieldEnhancer/UuidLinkEnhancer.php new file mode 100644 index 0000000000000000000000000000000000000000..c76be9e44857d2ec023ea623101341a0ef0fbd6d --- /dev/null +++ b/src/Plugin/jsonapi/FieldEnhancer/UuidLinkEnhancer.php @@ -0,0 +1,110 @@ +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} + */ + public function postProcess($value) { + 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_id = $parsed_uri[2]; + $entity = $this->entityTypeManager->getStorage($entity_type)->load($entity_id); + if (!is_null($entity)) { + $value['uri'] = 'entity:' . $entity_type . '/' . $entity->bundle() . '/' . $entity->uuid(); + } + // Remove the value. + else { + $value = [ + 'uri' => '', + 'title' => '', + 'options' => [], + ]; + } + } + } + + return $value; + } + + /** + * {@inheritdoc} + */ + public function prepareForInput($value) { + 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 getJsonSchema() { + return [ + 'type' => 'object', + ]; + } + +}