You are here

CKEditorAnchorLinkMatcher.php in CKEditor Anchor Link - For Drupal 8 and 9 3.0.x

File

src/Plugin/Linkit/Matcher/CKEditorAnchorLinkMatcher.php
View source
<?php

namespace Drupal\anchor_link\Plugin\Linkit\Matcher;

use Drupal\Component\Utility\Html;
use Drupal\linkit\MatcherBase;
use Drupal\linkit\Suggestion\DescriptionSuggestion;
use Drupal\linkit\Suggestion\SuggestionCollection;

/**
 * Provides specific linkit matchers for Anchor links.
 *
 * @Matcher(
 *   id = "ckeditor_anchor_link",
 *   label = @Translation("CKEditor Anchor link"),
 * )
 */
class CKEditorAnchorLinkMatcher extends MatcherBase {

  /**
   * {@inheritdoc}
   */
  public function execute($string) {
    $suggestions = new SuggestionCollection();
    $string = ltrim($string, '#');
    $suggestion = new DescriptionSuggestion();
    $suggestion
      ->setLabel($this
      ->t('#@anchor_link', [
      '@anchor_link' => $string,
    ]))
      ->setPath('#' . $string)
      ->setGroup($this
      ->t('Anchor links (within the same page)'));
    $suggestions
      ->addSuggestion($suggestion);
    return $suggestions;
  }

}

Classes

Namesort descending Description
CKEditorAnchorLinkMatcher Provides specific linkit matchers for Anchor links.