From f54a7676c5d9b49ff45e08753ee658ac07ac96a7 Mon Sep 17 00:00:00 2001 From: Mike Decker Date: Tue, 14 Nov 2023 22:18:55 -0800 Subject: [PATCH] Add remove tags processor --- .../search_api/processor/RemoveTags.php | 78 +++++++++++++++++++ stanford_profile_helper.module | 4 +- 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/Plugin/search_api/processor/RemoveTags.php diff --git a/src/Plugin/search_api/processor/RemoveTags.php b/src/Plugin/search_api/processor/RemoveTags.php new file mode 100644 index 00000000..a3024e2e --- /dev/null +++ b/src/Plugin/search_api/processor/RemoveTags.php @@ -0,0 +1,78 @@ + [ + 'div', + 'span', + ], + ]; + return $config; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form = parent::buildConfigurationForm($form, $form_state); + $form['tags'] = [ + '#type' => 'textarea', + '#title' => $this->t('Tags to Remove'), + '#description' => $this->t('One tag per line'), + '#default_value' => implode("\n", $this->configuration['tags']), + ]; + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { + parent::validateConfigurationForm($form, $form_state); + $tags = explode("\n", $form_state->getValue('tags')); + foreach ($tags as &$tag) { + $tag = trim($tag); + if (!preg_match('/^(\w+)$/', $tag)) { + $form_state->setError($form['tags'], $this->t('Invalid tag.')); + return; + } + } + $form_state->setValue('tags', array_filter($tags)); + } + + /** + * {@inheritdoc} + */ + protected function processFieldValue(&$value, $type) { + $text = str_replace('><', '> <', $value); + $text = trim(preg_replace('//Uis', '', $text)); + + preg_match_all('/<(\w+)/', $text, $tags_matched); + $text_tags = $tags_matched[1]; + $keep_tags = array_diff($text_tags, $this->configuration['tags']); + $value = strip_tags($text, $keep_tags); + } + +} diff --git a/stanford_profile_helper.module b/stanford_profile_helper.module index 4898d1f1..4c20ea25 100644 --- a/stanford_profile_helper.module +++ b/stanford_profile_helper.module @@ -421,7 +421,9 @@ function stanford_profile_helper_help_section_info_alter(array &$info) { function stanford_profile_helper_preprocess_input__submit__paragraph_action(&$variables) { // Change the top banner field button from "Add @type" to "Add Top @type". if ($variables['element']['#name'] == 'su_page_banner_stanford_banner_add_more') { - $variables['attributes']['value'] = t('Add Top @type', $variables['attributes']['value']->getArguments()); + if (!is_string($variables['attributes']['value'])) { + $variables['attributes']['value'] = t('Add Top @type', $variables['attributes']['value']->getArguments()); + } } }