Skip to content

Commit

Permalink
Add remove tags processor
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Nov 15, 2023
1 parent 806d6ad commit f54a767
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
78 changes: 78 additions & 0 deletions src/Plugin/search_api/processor/RemoveTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Drupal\stanford_profile_helper\Plugin\search_api\processor;

use Drupal\Core\Form\FormStateInterface;
use Drupal\search_api\Processor\FieldsProcessorPluginBase;

/**
* @SearchApiProcessor(
* id = "remove_tags",
* label = @Translation("Remove Specific HTML Tags"),
* description = @Translation("Similiar to 'strip_tags', but choose only
* which tags to strip from fields."), stages = {
* "preprocess_index" = 0,
* }
* )
*/
class RemoveTags extends FieldsProcessorPluginBase {

/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
$config = parent::defaultConfiguration();
$config['all_fields'] = TRUE;
$config += [
'tags' => [
'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);
}

}
4 changes: 3 additions & 1 deletion stanford_profile_helper.module
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}

Expand Down

0 comments on commit f54a767

Please sign in to comment.