summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorflorenttorregrosa2017-11-09 22:42:01 +0100
committerMateu Aguiló Bosch2017-11-09 22:42:01 +0100
commit6ba06da89badc2d23da1ec1d25f343ea41f185d1 (patch)
tree37ebfb8ea12e3e9058b2a97810c03d687ae585d5
parent05f20db96e2317a05faeaab73192f39106a3672c (diff)
feat(FieldEnhancer): Transform link field value to use UUID (#2883437 by Grimreaper)
-rw-r--r--src/Plugin/ResourceFieldEnhancerBase.php14
-rw-r--r--src/Plugin/jsonapi/FieldEnhancer/UuidLinkEnhancer.php110
2 files changed, 124 insertions, 0 deletions
diff --git a/src/Plugin/ResourceFieldEnhancerBase.php b/src/Plugin/ResourceFieldEnhancerBase.php
index 1fa7683..fa053d6 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 [];
@@ -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 0000000..c76be9e
--- /dev/null
+++ b/src/Plugin/jsonapi/FieldEnhancer/UuidLinkEnhancer.php
@@ -0,0 +1,110 @@
+<?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 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}
+ */
+ 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',
+ ];
+ }
+
+}