View source
<?php
namespace Drupal\pathologic\Plugin\Filter;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Form\FormStateInterface;
use Drupal\pathologic\PathologicSettingsCommon;
use Drupal\Core\Url;
class FilterPathologic extends FilterBase {
public function settingsForm(array $form, FormStateInterface $form_state) {
$form['reminder'] = [
'#type' => 'markup',
'#markup' => $this
->t('In most cases, Pathologic should be the <em>last</em> filter in the “Filter processing order” list.'),
'#weight' => 0,
];
$form['settings_source'] = [
'#type' => 'radios',
'#title' => $this
->t('Settings source'),
'#description' => $this
->t('Select whether Pathologic should use the <a href=":config">global Pathologic settings</a> or custom “local” settings when filtering text in this text format.', [
':config' => Url::fromRoute('pathologic.config_form')
->toString(),
]),
'#weight' => 10,
'#default_value' => $this->settings['settings_source'],
'#options' => [
'global' => $this
->t('Use global Pathologic settings'),
'local' => $this
->t('Use custom settings for this text format'),
],
];
$form['local_settings'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Custom settings for this text format'),
'#weight' => 20,
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#description' => $this
->t('These settings are ignored if “Use global Pathologic settings” is selected above.'),
'#states' => [
'visible' => [
':input[name="filters[filter_pathologic][settings][settings_source]"]' => [
'value' => 'local',
],
],
],
];
$common = new PathologicSettingsCommon();
$form['local_settings'] += $common
->commonSettingsForm($this->settings['local_settings']);
return $form;
}
public function process($text, $langcode) {
$settings = $this->settings;
if ($settings['settings_source'] === 'global') {
$config = \Drupal::config('pathologic.settings');
$settings['protocol_style'] = $config
->get('protocol_style');
$settings['local_paths'] = $config
->get('local_paths');
}
else {
$settings = $settings['local_settings'];
}
return new FilterProcessResult(_pathologic_filter($text, $settings, Crypt::hashBase64(serialize($settings))));
}
}