You are here

protected function AliasLinkEnhancer::doTransform 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::doTransform()

File

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

Class

AliasLinkEnhancer
Use UUID for internal link field value.

Namespace

Drupal\gatsby_extras\Plugin\jsonapi\FieldEnhancer

Code

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;
}