You are here

class AliasLinkEnhancer in Gatsby Live Preview & Incremental Builds 8

Same name and namespace in other branches
  1. 2.0.x modules/gatsby_extras/src/Plugin/jsonapi/FieldEnhancer/AliasLinkEnhancer.php \Drupal\gatsby_extras\Plugin\jsonapi\FieldEnhancer\AliasLinkEnhancer

Use UUID for internal link field value.

Plugin annotation


@ResourceFieldEnhancer(
  id = "alias_link",
  label = @Translation("Alias for link (link field only)"),
  description = @Translation("Use alias for internal link field.")
)

Hierarchy

Expanded class hierarchy of AliasLinkEnhancer

File

modules/gatsby_extras/src/Plugin/jsonapi/FieldEnhancer/AliasLinkEnhancer.php, line 20

Namespace

Drupal\gatsby_extras\Plugin\jsonapi\FieldEnhancer
View source
class AliasLinkEnhancer 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_uuid'] = 'entity:' . $entity_type . '/' . $entity
            ->bundle() . '/' . $entity
            ->uuid();
          $data['uri_alias'] = $entity
            ->toUrl()
            ->toString();
        }
        else {
          $data = [
            'uri' => '',
            'uri_uuid' => '',
            'uri_alias' => '',
            '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)) {

          /** @var \Drupal\Core\Entity\EntityInterface $entity */
          $entity = array_shift($entities);
          $value['uri_uuid'] = 'entity:' . $entity_type . '/' . $entity
            ->id();
          $value['uri_alias'] = $entity
            ->toUrl()
            ->toString();
        }
        else {

          // If the entity has not been imported yet we unset the field value.
          $value = [];
        }
      }
    }
    return $value;
  }

  /**
   * {@inheritdoc}
   */
  public function getOutputJsonSchema() {
    return [
      'type' => 'object',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AliasLinkEnhancer::$entityTypeManager protected property The entity type manager.
AliasLinkEnhancer::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
AliasLinkEnhancer::doTransform protected function
AliasLinkEnhancer::doUndoTransform protected function
AliasLinkEnhancer::getOutputJsonSchema public function
AliasLinkEnhancer::__construct public function