You are here

protected function EntityShareUuidLinkEnhancer::doUndoTransform in Entity Share 8.3

File

src/Plugin/jsonapi/FieldEnhancer/EntityShareUuidLinkEnhancer.php, line 42

Class

EntityShareUuidLinkEnhancer
Use UUID for internal link field value and add URL for import.

Namespace

Drupal\entity_share\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();

        // Add URL for import.
        $route_name = sprintf('jsonapi.%s--%s.individual', $entity_type, $entity
          ->bundle());
        try {
          $content_url = Url::fromRoute($route_name, [
            'entity' => $entity
              ->uuid(),
          ])
            ->setOption('language', $this->languageManager
            ->getCurrentLanguage())
            ->setOption('absolute', TRUE);
          $data['content_entity_href'] = $content_url
            ->toString();
        } catch (\Exception $exception) {

          // Do nothing.
        }
      }
      else {
        $data = [
          'uri' => '',
          'title' => '',
          'options' => [],
        ];
      }
    }
  }
  return $data;
}