Previously, a module providing a filter would implement
hook_filter_info(), and then define one of several callbacks:
Instead of an info hook and several callbacks, filters have been converted to annotated plugins.
The callback functions become methods.
The data from the info hooks are now in the annotation.
Filters use the annotation class of Drupal\filter\Annotation\Filter, and implement Drupal\filter\Plugin\FilterInterface, filters have to be in the Plugin/Filter namespace.
To implement a filter in a module create a class in {module}/src/Plugin/Filter/{FilterName}.php and extend FilterBase (which implements FilterInterface).
Drupal 7
function filter_filter_info() {
$filters['filter_html'] = array(
'title' => t('Limit allowed HTML tags'),
'process callback' => '_filter_html',
'settings callback' => '_filter_html_settings',
'default settings' => array(
'allowed_html' => '<a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>',
'filter_html_help' => 1,
'filter_html_nofollow' => 0,
),
'tips callback' => '_filter_html_tips',
'weight' => -10,
);
return $filters;
}
/**
* Settings callback.
*/
function _filter_html_settings($form, &$form_state, $filter, $format, $defaults) {
// ...
}
/**
* Process callback.
*/
function _filter_html($text, $filter) {
// ...
}
/**
* Tips callback.
*/
function _filter_html_tips($filter, $format, $long = FALSE) {
// ...
}
Drupal 8
/**
* @file
* Contains \Drupal\filter\Plugin\Filter\FilterHtml.
*/
namespace Drupal\filter\Plugin\Filter;
use Drupal\filter\Annotation\Filter;
use Drupal\Core\Annotation\Translation;
use Drupal\filter\Plugin\FilterBase;
/**
* Provides a filter to limit allowed HTML tags.
*
* @Filter(
* id = "filter_html",
* title = @Translation("Limit allowed HTML tags"),
* type = Drupal\filter\Plugin\FilterInterface::TYPE_HTML_RESTRICTOR,
* settings = {
* "allowed_html" = "<a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <h4> <h5> <h6>",
* "filter_html_help" = 1,
* "filter_html_nofollow" = 0
* },
* weight = -10
* )
*/
class FilterHtml extends FilterBase {
public function settingsForm(array $form, array &$form_state) {
// ...
}
public function process($text, $langcode, $cache, $cache_id) {
// ...
}
public function tips($long = FALSE) {
// ...
}
}
Comments
Just in case someone hit the
Just in case someone hit the snag I did, your filter plugin won't be discovered if you don't place it in a Plugin/Filter subfolder. I spent an hour before I discovered the filter has to be in lib/Drupal/{modulename}/Plugin/Filter folder otherwise it won't be detected.
Type of filter shoold be like
Type of filter shoold be like type = Drupal\filter\Plugin\FilterInterface::TYPE_MARKUP_LANGUAGE or etc.
Set default value for multiselect setting (e.g. checkboxes)
I just came across a case where I'd like to set default values for a setting of a type "checkboxes" which means array. I tried
but that doesn't work. Is that documented somewhere?
EDIT:
I finally found out how to do it. The syntax above is OK, but schema.yml is REQUIRED (in contrast to scalar settings):
https://drupal.stackexchange.com/questions/239322/how-can-i-save-multipl...
http://www.DROWL.de || Professionelle Drupal Lösungen aus Ostwestfalen-Lippe (OWL)
http://www.webks.de || webks: websolutions kept simple - Webbasierte Lösungen die einfach überzeugen!
http://www.drupal-theming.com || Individuelle Responsive Themes