You are here

protected function EntityShareEmbeddedEntitiesEnhancer::addEntityJsonapiUrl in Entity Share 8.3

Helper function to add an entity JSON:API link to HTML attributes.

Parameters

array $matches: The link information.

Return value

string The replacement.

File

src/Plugin/jsonapi/FieldEnhancer/EntityShareEmbeddedEntitiesEnhancer.php, line 126

Class

EntityShareEmbeddedEntitiesEnhancer
Alter rich text exposed data to provide import URL for embedded entities.

Namespace

Drupal\entity_share\Plugin\jsonapi\FieldEnhancer

Code

protected function addEntityJsonapiUrl(array $matches) {
  $entity_type = $matches[2];
  $entity_uuid = $matches[3];
  $entities = $this->entityTypeManager
    ->getStorage($entity_type)
    ->loadByProperties([
    'uuid' => $entity_uuid,
  ]);
  if (!empty($entities)) {
    $entity = array_shift($entities);

    // 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);

      // For img tag.
      $closing_slash = $matches[4];
      return $matches[1] . ' data-entity-jsonapi-url="' . $content_url
        ->toString() . '"' . $closing_slash . '>';
    } catch (\Exception $exception) {

      // Do nothing.
    }
  }
  return $matches[0];
}