3 namespace Drupal\pathologic\Plugin\Filter;
5 use Drupal\filter\FilterProcessResult;
6 use Drupal\filter\Plugin\FilterBase;
7 use Drupal\Component\Utility\Crypt;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\pathologic\PathologicSettingsCommon;
13 * Attempts to correct broken paths in content.
15 * We give the filter a weight of 50 in the annotation below because in almost
16 * all cases Pathologic should be the last filter in the filter list. Is it
17 * possible to put a comment inside an annotation? Man, annotations are such a
21 * id = "filter_pathologic",
22 * title = @Translation("Correct URLs with Pathologic"),
23 * type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
25 * "settings_source" = "global",
26 * "local_settings" = {
27 * "protocol_style" = "full",
34 class FilterPathologic extends FilterBase {
39 public function settingsForm(array $form, FormStateInterface $form_state) {
42 '#markup' => $this->t('In most cases, Pathologic should be the <em>last</em> filter in the “Filter processing order” list.'),
45 $form['settings_source'] = [
47 '#title' => $this->t('Settings source'),
48 '#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()]),
50 '#default_value' => $this->settings['settings_source'],
52 'global' => $this->t('Use global Pathologic settings'),
53 'local' => $this->t('Use custom settings for this text format'),
56 // Fields in fieldsets areā¦ awkward to implement.
57 // @see https://www.drupal.org/node/2378437
58 $form['local_settings'] = [
59 '#type' => 'fieldset',
60 '#title' => $this->t('Custom settings for this text format'),
62 '#collapsible' => FALSE,
63 '#collapsed' => FALSE,
64 '#description' => $this->t('These settings are ignored if “Use global Pathologic settings” is selected above.'),
65 // @todo Fix the #states magic (or see if it's a core D8 bug)
68 ':input[name="filters[filter_pathologic][settings][settings_source]"]' => ['value' => 'local'],
73 $common = new PathologicSettingsCommon();
74 $form['local_settings'] += $common->commonSettingsForm($this->settings['local_settings']);
82 public function process($text, $langcode) {
83 $settings = $this->settings;
84 if ($settings['settings_source'] === 'global') {
85 $config = \Drupal::config('pathologic.settings');
86 $settings['protocol_style'] = $config->get('protocol_style');
87 $settings['local_paths'] = $config->get('local_paths');
90 $settings = $settings['local_settings'];
92 // @todo Move code from .module file to inside here.
93 return new FilterProcessResult(_pathologic_filter($text, $settings, Crypt::hashBase64(serialize($settings))));