Verified Commit 1e89b0ca authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3421012 by sorlov, quietone, sakthi_dev, mstrelan, Wim Leers: Convert...

Issue #3421012 by sorlov, quietone, sakthi_dev, mstrelan, Wim Leers: Convert Filter plugin discovery to attributes

(cherry picked from commit 1d07b822)
parent a3a4d885
Loading
Loading
Loading
Loading
Loading
+8 −6
Original line number Diff line number Diff line
@@ -2,18 +2,20 @@

namespace Drupal\ckeditor5_incompatible_filter_test\Plugin\Filter;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\filter\Attribute\Filter;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\filter\Plugin\FilterInterface;

/**
 * Provides a filter incompatible with CKEditor 5.
 *
 * @Filter(
 *   id = "filter_incompatible",
 *   title = @Translation("A TYPE_MARKUP_LANGUAGE filter incompatible with CKEditor 5"),
 *   type = Drupal\filter\Plugin\FilterInterface::TYPE_MARKUP_LANGUAGE
 * )
 */
#[Filter(
  id: "filter_incompatible",
  title: new TranslatableMarkup("A TYPE_MARKUP_LANGUAGE filter incompatible with CKEditor 5"),
  type: FilterInterface::TYPE_MARKUP_LANGUAGE
)]
class FilterIsIncompatible extends FilterBase {

  /**
+9 −7
Original line number Diff line number Diff line
@@ -6,23 +6,25 @@
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\file\FileInterface;
use Drupal\filter\Attribute\Filter;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\filter\Plugin\FilterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a filter to track images uploaded via a Text Editor.
 *
 * Generates file URLs and associates the cache tags of referenced files.
 *
 * @Filter(
 *   id = "editor_file_reference",
 *   title = @Translation("Track images uploaded via a Text Editor"),
 *   description = @Translation("Ensures that the latest versions of images uploaded via a Text Editor are displayed, along with their dimensions."),
 *   type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_REVERSIBLE
 * )
 */
#[Filter(
  id: "editor_file_reference",
  title: new TranslatableMarkup("Track images uploaded via a Text Editor"),
  description: new TranslatableMarkup("Ensures that the latest versions of images uploaded via a Text Editor are displayed, along with their dimensions."),
  type: FilterInterface::TYPE_TRANSFORM_REVERSIBLE
)]
class EditorFileReference extends FilterBase implements ContainerFactoryPluginInterface {

  /**
+56 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\filter\Attribute;

use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines a filter attribute for plugin discovery.
 *
 * Plugin Namespace: Plugin\Filter
 *
 * For a working example, see \Drupal\filter\Plugin\Filter\FilterHtml
 *
 * @see \Drupal\filter\FilterPluginManager
 * @see \Drupal\filter\Plugin\FilterInterface
 * @see \Drupal\filter\Plugin\FilterBase
 * @see plugin_api
 */
#[\Attribute(\Attribute::TARGET_CLASS)]
class Filter extends Plugin {

  /**
   * Constructs a Filter attribute.
   *
   * @param string $id
   *   The plugin ID.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup $title
   *   The human-readable name of the filter. This is used as an administrative
   *   summary of what the filter does.
   * @param int $type
   *   The filter type. Values are defined in
   *   \Drupal\filter\Plugin\FilterInterface.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description
   *   (optional) Additional administrative information about the filter's
   *   behavior.
   * @param int $weight
   *   (optional) A default weight for the filter in new text formats.
   * @param bool $status
   *   (optional) Whether this filter is enabled or disabled by default.
   * @param array $settings
   *   (optional) The default settings for the filter.
   */
  public function __construct(
    public readonly string $id,
    public readonly TranslatableMarkup $title,
    public readonly int $type,
    public readonly ?TranslatableMarkup $description = NULL,
    public readonly int $weight = 0,
    public readonly bool $status = FALSE,
    public readonly array $settings = [],
  ) {}

}
+2 −1
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\filter\Attribute\Filter;

/**
 * Manages text processing filters.
@@ -30,7 +31,7 @@ class FilterPluginManager extends DefaultPluginManager implements FallbackPlugin
   *   The module handler to invoke the alter hook with.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
    parent::__construct('Plugin/Filter', $namespaces, $module_handler, 'Drupal\filter\Plugin\FilterInterface', 'Drupal\filter\Annotation\Filter');
    parent::__construct('Plugin/Filter', $namespaces, $module_handler, 'Drupal\filter\Plugin\FilterInterface', Filter::class, 'Drupal\filter\Annotation\Filter');
    $this->alterInfo('filter_info');
    $this->setCacheBackend($cache_backend, 'filter_plugins');
  }
+9 −7
Original line number Diff line number Diff line
@@ -3,19 +3,21 @@
namespace Drupal\filter\Plugin\Filter;

use Drupal\Component\Utility\Html;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\filter\Attribute\Filter;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\filter\Plugin\FilterInterface;

/**
 * Provides a filter to align elements.
 *
 * @Filter(
 *   id = "filter_align",
 *   title = @Translation("Align images"),
 *   description = @Translation("Uses a <code>data-align</code> attribute on <code>&lt;img&gt;</code> tags to align images."),
 *   type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_REVERSIBLE
 * )
 */
#[Filter(
  id: "filter_align",
  title: new TranslatableMarkup("Align images"),
  description: new TranslatableMarkup("Uses a <code>data-align</code> attribute on <code>&lt;img&gt;</code> tags to align images."),
  type: FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
)]
class FilterAlign extends FilterBase {

  /**
Loading