From b00d179928d08ee3ae04b68140985568826504cb Mon Sep 17 00:00:00 2001 From: florenttorregrosa Date: Fri, 2 Jun 2017 15:58:16 +0200 Subject: [PATCH] Issue #2883437 by Grimreaper: Transform link field value to use UUID --- src/Plugin/ResourceFieldEnhancerBase.php | 28 ++++++++ .../jsonapi/FieldEnhancer/LinkFieldEnhancer.php | 75 ++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/Plugin/jsonapi/FieldEnhancer/LinkFieldEnhancer.php diff --git a/src/Plugin/ResourceFieldEnhancerBase.php b/src/Plugin/ResourceFieldEnhancerBase.php index 1fa7683..3034e07 100644 --- a/src/Plugin/ResourceFieldEnhancerBase.php +++ b/src/Plugin/ResourceFieldEnhancerBase.php @@ -26,6 +26,13 @@ abstract class ResourceFieldEnhancerBase extends PluginBase implements ResourceF /** * {@inheritdoc} */ + public function defaultConfiguration() { + return []; + } + + /** + * {@inheritdoc} + */ public function calculateDependencies() { // TODO: This should have a dependency on the resource_config entity. return []; @@ -43,9 +50,30 @@ abstract class ResourceFieldEnhancerBase extends PluginBase implements ResourceF /** * {@inheritdoc} */ + public function postProcess($value) { + return $value; + } + + /** + * {@inheritdoc} + */ + public function prepareForInput($value) { + return $value; + } + + /** + * {@inheritdoc} + */ public function setConfiguration(array $configuration) { $this->configuration = $configuration + $this->defaultConfiguration(); return $this->configuration; } + /** + * {@inheritdoc} + */ + public function getSettingsForm(array $resource_field_info) { + return []; + } + } diff --git a/src/Plugin/jsonapi/FieldEnhancer/LinkFieldEnhancer.php b/src/Plugin/jsonapi/FieldEnhancer/LinkFieldEnhancer.php new file mode 100644 index 0000000..72c476d --- /dev/null +++ b/src/Plugin/jsonapi/FieldEnhancer/LinkFieldEnhancer.php @@ -0,0 +1,75 @@ +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); + $value['uri'] = 'entity:' . $entity_type . '/' . $entity->uuid(); + } + } + + return $value; + } + + /** + * {@inheritdoc} + */ + public function getJsonSchema() { + return [ + 'type' => 'array', + ]; + } + +} -- 1.9.1