You are here

protected function DefaultFormattedTextHandler::replaceEntityReferenceLinks in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x src/Plugin/cms_content_sync/field_handler/DefaultFormattedTextHandler.php \Drupal\cms_content_sync\Plugin\cms_content_sync\field_handler\DefaultFormattedTextHandler::replaceEntityReferenceLinks()
  2. 2.0.x src/Plugin/cms_content_sync/field_handler/DefaultFormattedTextHandler.php \Drupal\cms_content_sync\Plugin\cms_content_sync\field_handler\DefaultFormattedTextHandler::replaceEntityReferenceLinks()

Replace all "/node/..." links with their correct ID for the current site.

@todo If a new entity is added, we should scan the database for existing references to it that can now be resolved.

Parameters

$text:

Return value

string

1 call to DefaultFormattedTextHandler::replaceEntityReferenceLinks()
DefaultFormattedTextHandler::pull in src/Plugin/cms_content_sync/field_handler/DefaultFormattedTextHandler.php

File

src/Plugin/cms_content_sync/field_handler/DefaultFormattedTextHandler.php, line 167

Class

DefaultFormattedTextHandler
Providing a minimalistic implementation for any field type.

Namespace

Drupal\cms_content_sync\Plugin\cms_content_sync\field_handler

Code

protected function replaceEntityReferenceLinks($text) {
  $entity_repository = \Drupal::service('entity.repository');
  $replace_uri_callback = function ($matches) {
    $path = $matches[2];

    // PDF files can have a #page=... anchor attached that we want to keep.
    $anchor = null;
    if (false !== strpos($path, '#')) {
      list($path, $anchor) = explode('#', $path);
    }
    $parts = explode('/', $path);
    $file = null;
    $uri = null;
    for ($i = 0; $i < count($parts); ++$i) {
      $uri = 'public://' . urldecode(implode('/', array_slice($parts, $i)));

      /** @var FileInterface[] $files */
      $files = \Drupal::entityTypeManager()
        ->getStorage('file')
        ->loadByProperties([
        'uri' => $uri,
      ]);
      if (count($files)) {
        $file = reset($files);
        break;
      }
    }
    if (!$file) {
      \Drupal::logger('cms_content_sync')
        ->error('Failed to replace file URI in the formatted text as the file is missing on this site: @uri', [
        '@uri' => 'public://' . urldecode($path),
      ]);
      return $matches[1] . '"/404"';
    }
    $url = file_url_transform_relative(file_create_url($uri));
    if ($anchor) {
      $url .= '#' . $anchor;
    }
    return $matches[1] . '"' . $url . '"';
  };

  // Simple image embedding (default ckeditor + IMCE images)
  $text = preg_replace_callback('@(<img[^>]+src=)"/sites/[^/]+/files/([^"]+)"@', $replace_uri_callback, $text);

  // Other file embedding (IMCE files)
  $text = preg_replace_callback('@(<a[^>]+href=)"/sites/[^/]+/files/([^"]+)"@', $replace_uri_callback, $text);

  // Entity embedding (especially media)
  return preg_replace_callback('@data-entity-uuid="([0-9a-z-]+)" href="/node/([0-9]+)"@', function ($matches) use ($entity_repository) {
    $uuid = $matches[1];
    $id = $matches[2];
    try {
      $node = $entity_repository
        ->loadEntityByUuid('node', $uuid);
      if ($node) {
        $id = $node
          ->id();
      }
    } catch (\Exception $e) {
    }
    return 'data-entity-uuid="' . $uuid . '" href="/node/' . $id . '"';
  }, $text);
}