Skip to content

Instantly share code, notes, and snippets.

@superbiche
Last active March 7, 2019 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superbiche/13c35f3ba81b7641d14de994962d510b to your computer and use it in GitHub Desktop.
Save superbiche/13c35f3ba81b7641d14de994962d510b to your computer and use it in GitHub Desktop.
diff --git a/src/Plugin/jsonapi/FieldEnhancer/UrlLinkEnhancer.php b/src/Plugin/jsonapi/FieldEnhancer/UrlLinkEnhancer.php
new file mode 100644
index 0000000..b5b7d3d
--- /dev/null
+++ b/src/Plugin/jsonapi/FieldEnhancer/UrlLinkEnhancer.php
@@ -0,0 +1,101 @@
+<?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 URL for link field values.
+ *
+ * @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 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'])) {
+
+ // Transform internal links.
+ preg_match("/internal:(.*)/", $data['uri'], $parsed_uri);
+ if (!empty($parsed_uri)) {
+ $values = [
+ 'url' => $parsed_uri[1],
+ 'title' => $data['title']
+ ];
+ return $values += $data['options'];
+ }
+
+ 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);
+
+ $values = [
+ 'url' => $entity->toUrl()->toString(),
+ 'title' => $data['title']
+ ];
+ return $values += $data['options'];
+ }
+
+ $values = [
+ 'url' => $data['uri'],
+ 'title' => $data['title']
+ ];
+ return $values += $data['options'];
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function doTransform($value, Context $context) {
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getOutputJsonSchema() {
+ return [
+ 'type' => 'object',
+ ];
+ }
+
+}
diff --git a/src/Plugin/jsonapi/FieldEnhancer/VestaUrlLinkEnhancer.php b/src/Plugin/jsonapi/FieldEnhancer/VestaUrlLinkEnhancer.php
new file mode 100644
index 0000000..ecf8b8b
--- /dev/null
+++ b/src/Plugin/jsonapi/FieldEnhancer/VestaUrlLinkEnhancer.php
@@ -0,0 +1,126 @@
+<?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 Drupal\consumers\Negotiator;
+
+/**
+ * Use URL for link field values.
+ *
+ * @ResourceFieldEnhancer(
+ * id = "vesta_url_link",
+ * label = @Translation("Vesta URL for link (link field only)"),
+ * description = @Translation("Use Url for link fields (Vesta Site).")
+ * )
+ */
+class VestaUrlLinkEnhancer extends ResourceFieldEnhancerBase implements ContainerFactoryPluginInterface {
+
+ /**
+ * The entity type manager.
+ *
+ * @var \Drupal\Core\Entity\EntityTypeManagerInterface
+ */
+ protected $entityTypeManager;
+
+ /**
+ * @var \Drupal\consumers\Negotiator
+ */
+ protected $consumerNegotiator;
+
+ protected $site;
+
+ /**
+ * {@inheritdoc}
+ */
+ public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityTypeManagerInterface $entity_type_manager, Negotiator $consumer_negotiator) {
+ parent::__construct($configuration, $plugin_id, $plugin_definition);
+ $this->entityTypeManager = $entity_type_manager;
+ $this->consumerNegotiator = $consumer_negotiator;
+ }
+
+ public function getSite() {
+ if (!$this->site) {
+ // Fetch the site data.
+ $consumer = $this->consumerNegotiator->negotiateFromRequest();
+ $site = $consumer->vesta_site ? $consumer->vesta_site->entity : null;
+ if (!$site) {
+ throw new \InvalidArgumentException('No value for vesta_site found for this Consumer or no Consumer uuid found in request. Make sure you include either X-Consumer-ID header or _consumer_id query param in your request, and make sure every Consumer has a "Site" Group entity referenced.');
+ }
+ $this->site = $site;
+ }
+ return $this->site;
+ }
+
+ /**
+ * {@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'),
+ $container->get('consumer.negotiator')
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function doUndoTransform($data, Context $context) {
+ if (isset($data['uri'])) {
+
+ // Transform internal links.
+ preg_match("/internal:(.*)/", $data['uri'], $parsed_uri);
+ if (!empty($parsed_uri)) {
+ $values = [
+ 'url' => $parsed_uri[1],
+ 'title' => $data['title']
+ ];
+ return $values += $data['options'];
+ }
+
+ 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);
+ $path_manager = \Drupal::service('vesta_decoupled_path.path_manager');
+ $url = $entity->toUrl()->toString();
+
+ $values = [
+ 'url' => $path_manager->getVestaPathForNode($entity, $url),
+ 'title' => $data['title']
+ ];
+ return $values += $data['options'];
+ }
+
+ $values = [
+ 'url' => $data['uri'],
+ 'title' => $data['title']
+ ];
+ return $values += $data['options'];
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function doTransform($value, Context $context) {
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getOutputJsonSchema() {
+ return [
+ 'type' => 'object',
+ ];
+ }
+
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment