You are here

public function UuidLinkEnhancer::postProcess in JSON:API Extras 8

Apply the last transformations to the output value of a single field.

Parameters

mixed $value: The value to be processed after being prepared for output.

Return value

mixed The value after being post processed.

Overrides ResourceFieldEnhancerInterface::postProcess

File

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

Class

UuidLinkEnhancer
Use UUID for internal link field value.

Namespace

Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer

Code

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();
      }
      else {
        $value = [
          'uri' => '',
          'title' => '',
          'options' => [],
        ];
      }
    }
  }
  return $value;
}