You are here

protected function UuidLinkEnhancer::doUndoTransform in JSON:API Extras 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/jsonapi/FieldEnhancer/UuidLinkEnhancer.php \Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer\UuidLinkEnhancer::doUndoTransform()

File

src/Plugin/jsonapi/FieldEnhancer/UuidLinkEnhancer.php, line 52

Class

UuidLinkEnhancer
Use UUID for internal link field value.

Namespace

Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer

Code

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'] = 'entity:' . $entity_type . '/' . $entity
          ->bundle() . '/' . $entity
          ->uuid();
      }
      else {
        $data = [
          'uri' => '',
          'title' => '',
          'options' => [],
        ];
      }
    }
  }
  return $data;
}