You are here

public function LinkitFilter::process in Linkit 8.5

Performs the filter processing.

Parameters

string $text: The text string to be filtered.

string $langcode: The language code of the text to be filtered.

Return value

\Drupal\filter\FilterProcessResult The filtered text, wrapped in a FilterProcessResult object, and possibly with associated assets, cacheability metadata and placeholders.

Overrides FilterInterface::process

See also

\Drupal\filter\FilterProcessResult

File

src/Plugin/Filter/LinkitFilter.php, line 80

Class

LinkitFilter
Provides a Linkit filter.

Namespace

Drupal\linkit\Plugin\Filter

Code

public function process($text, $langcode) {
  $result = new FilterProcessResult($text);
  if (strpos($text, 'data-entity-type') !== FALSE && strpos($text, 'data-entity-uuid') !== FALSE) {
    $dom = Html::load($text);
    $xpath = new \DOMXPath($dom);
    foreach ($xpath
      ->query('//a[@data-entity-type and @data-entity-uuid]') as $element) {

      /** @var \DOMElement $element */
      try {

        // Load the appropriate translation of the linked entity.
        $entity_type = $element
          ->getAttribute('data-entity-type');
        $uuid = $element
          ->getAttribute('data-entity-uuid');

        // Skip empty attributes to prevent loading of non-existing
        // content type.
        if ($entity_type === '' || $uuid === '') {
          continue;
        }

        // Make the substitution optional, for backwards compatibility,
        // maintaining the previous hard-coded direct file link assumptions,
        // for content created before the substitution feature.
        if (!($substitution_type = $element
          ->getAttribute('data-entity-substitution'))) {
          $substitution_type = $entity_type === 'file' ? 'file' : SubstitutionManagerInterface::DEFAULT_SUBSTITUTION;
        }
        $entity = $this->entityRepository
          ->loadEntityByUuid($entity_type, $uuid);
        if ($entity) {
          $entity = $this->entityRepository
            ->getTranslationFromContext($entity, $langcode);

          /** @var \Drupal\Core\GeneratedUrl $url */
          $url = $this->substitutionManager
            ->createInstance($substitution_type)
            ->getUrl($entity);

          // Parse link href as url, extract query and fragment from it.
          $href_url = parse_url($element
            ->getAttribute('href'));
          $anchor = empty($href_url["fragment"]) ? '' : '#' . $href_url["fragment"];
          $query = empty($href_url["query"]) ? '' : '?' . $href_url["query"];
          $element
            ->setAttribute('href', $url
            ->getGeneratedUrl() . $query . $anchor);

          // Set the appropriate title attribute.
          if ($this->settings['title'] && !$element
            ->getAttribute('title')) {
            $access = $entity
              ->access('view', NULL, TRUE);
            if (!$access
              ->isForbidden()) {
              $element
                ->setAttribute('title', $entity
                ->label());
            }

            // Cache the linked entity access for the current user.
            $result
              ->addCacheableDependency($access);
          }

          // The processed text now depends on:
          $result
            ->addCacheableDependency($url)
            ->addCacheableDependency($entity);
        }
      } catch (\Exception $e) {
        watchdog_exception('linkit_filter', $e);
      }
    }
    $result
      ->setProcessedText(Html::serialize($dom));
  }
  return $result;
}