summaryrefslogtreecommitdiffstats
path: root/core/modules/filter/lib/Drupal/filter/Plugin/FilterBase.php
blob: ab56c911613644955e04dc65f7aaf121b79a8f15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php

/**
 * @file
 * Contains \Drupal\filter\Plugin\Filter\FilterBase.
 */

namespace Drupal\filter\Plugin;

use Drupal\Component\Plugin\PluginBase;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\filter\FilterBag;

/**
 * Provides a base class for Filter plugins.
 */
abstract class FilterBase extends PluginBase implements FilterInterface {

  /**
   * The plugin ID of this filter.
   *
   * @var string
   */
  protected $plugin_id;

  /**
   * The name of the module that owns this filter.
   *
   * @var string
   */
  public $module;

  /**
   * A Boolean indicating whether this filter is enabled.
   *
   * @var bool
   */
  public $status = FALSE;

  /**
   * The weight of this filter compared to others in a filter collection.
   *
   * @see FilterBase::$filterBag
   *
   * @var int
   */
  public $weight = 0;

  /**
   * A Boolean indicating whether the text processed by this filter may be cached.
   *
   * @var bool
   */
  public $cache = TRUE;

  /**
   * An associative array containing the configured settings of this filter.
   *
   * @var array
   */
  public $settings = array();

  /**
   * A collection of all filters this filter participates in.
   *
   * @var \Drupal\filter\FilterBag
   */
  protected $bag;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, FilterBag $bag) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->module = $this->pluginDefinition['module'];
    $this->cache = $this->pluginDefinition['cache'];

    $this->setPluginConfiguration($configuration);

    $this->bag = $bag;
  }

  /**
   * {@inheritdoc}
   */
  public function setPluginConfiguration(array $configuration) {
    if (isset($configuration['status'])) {
      $this->status = (bool) $configuration['status'];
    }
    if (isset($configuration['weight'])) {
      $this->weight = (int) $configuration['weight'];
    }
    if (isset($configuration['settings'])) {
      $this->settings = (array) $configuration['settings'];
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function export() {
    return array(
      'module' => $this->pluginDefinition['module'],
      'status' => $this->status,
      'weight' => $this->weight,
      'settings' => $this->settings,
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getType() {
    return $this->pluginDefinition['type'];
  }

  /**
   * {@inheritdoc}
   */
  public function getLabel() {
    return $this->pluginDefinition['title'];
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this->pluginDefinition['description'];
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, array &$form_state) {
    // Implementations should work with and return $form. Returning an empty
    // array here allows the text format administration form to identify whether
    // the filter plugin has any settings form elements.
    return array();
  }

  /**
   * {@inheritdoc}
   */
  public function prepare($text, $langcode, $cache, $cache_id) {
    return $text;
  }

  /**
   * {@inheritdoc}
   */
  public function tips($long = FALSE) {
  }

}