StripTags.php in Tamper 8
File
src/Plugin/Tamper/StripTags.php
View source
<?php
namespace Drupal\tamper\Plugin\Tamper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tamper\Exception\TamperException;
use Drupal\tamper\TamperableItemInterface;
use Drupal\tamper\TamperBase;
class StripTags extends TamperBase {
const SETTING_ALLOWED_TAGS = 'allowed_tags';
public function defaultConfiguration() {
$config = parent::defaultConfiguration();
$config[self::SETTING_ALLOWED_TAGS] = '';
return $config;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form[self::SETTING_ALLOWED_TAGS] = [
'#type' => 'textarea',
'#title' => $this
->t('Allowed tags'),
'#default_value' => $this
->getSetting(self::SETTING_ALLOWED_TAGS),
'#description' => $this
->t('A list of allowed tags such as %a%b', [
'%a' => '<a>',
'%b' => '<em>',
]),
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$allowed_tags = trim($form_state
->getValue(self::SETTING_ALLOWED_TAGS));
$this
->setConfiguration([
self::SETTING_ALLOWED_TAGS => $allowed_tags,
]);
}
public function tamper($data, TamperableItemInterface $item = NULL) {
if (!is_string($data)) {
throw new TamperException('Input should be a string.');
}
$allowed_tags = $this
->getSetting(self::SETTING_ALLOWED_TAGS);
return strip_tags($data, $allowed_tags);
}
}
Classes
Name |
Description |
StripTags |
Plugin implementation for stripping tags. |