diff -u b/src/Plugin/Tamper/KeywordFilter.php b/src/Plugin/Tamper/KeywordFilter.php --- b/src/Plugin/Tamper/KeywordFilter.php +++ b/src/Plugin/Tamper/KeywordFilter.php @@ -24,6 +24,8 @@ const SETTING_EXACT = 'exact'; const SETTING_CASE_SENSITIVE = 'case_sensitive'; const SETTING_INVERT = 'invert'; + const SETTING_WORD_LIST = 'word_list'; + const SETTING_REGEX = 'regex'; /** * {@inheritdoc} @@ -42,11 +44,39 @@ * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { - $form[self::SETTING_OPERATION] = [ - '#type' => 'select', - '#title' => $this->t('How to convert case'), - '#options' => $this->getOptions(), - '#default_value' => $this->getSetting(self::SETTING_OPERATION), + $form[self::SETTING_WORDS] = [ + '#type' => 'textarea', + '#title' => $this->t('Words or phrases to filter on'), + '#default_value' => $this->getSetting(self::SETTING_WORDS), + '#description' => $this->t('A list of words/phrases that need to appear in the text. Enter one value per line.'), + ]; + + $form[self::SETTING_WORD_BOUNDARIES] = [ + '#type' => 'checkbox', + '#title' => $this->t('Respect word boundaries'), + '#default_value' => $this->getSetting(self::SETTING_WORD_BOUNDARIES), + '#description' => $this->t('If checked, then "book" will match "book" but not "bookcase".'), + ]; + + $form[self::SETTING_EXACT] = [ + '#type' => 'checkbox', + '#title' => $this->t('Exact'), + '#default_value' => $this->getSetting(self::SETTING_EXACT), + '#description' => $this->t('If checked, then "book" will only match "book". This will override the "Respect word boundaries" setting above.'), + ]; + + $form[self::SETTING_CASE_SENSITIVE] = [ + '#type' => 'checkbox', + '#title' => $this->t('Case sensitive'), + '#default_value' => $this->getSetting(self::SETTING_CASE_SENSITIVE), + '#description' => $this->t('If checked, then "book" will match "book" but not "Book" or "BOOK".'), + ]; + + $form[self::SETTING_INVERT] = [ + '#type' => 'checkbox', + '#title' => $this->t('Invert filter'), + '#default_value' => $this->getSetting(self::SETTING_INVERT), + '#description' => $this->t('Inverting the filter will remove items with the specified text.'), ]; return $form; @@ -57,23 +87,62 @@ */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { parent::submitConfigurationForm($form, $form_state); - $this->setConfiguration([self::SETTING_OPERATION => $form_state->getValue(self::SETTING_OPERATION)]); + $this->setConfiguration([self::SETTING_WORDS => $form_state->getValue(self::SETTING_WORDS)]); + $this->setConfiguration([self::SETTING_WORD_BOUNDARIES => $form_state->getValue(self::SETTING_WORD_BOUNDARIES)]); + $this->setConfiguration([self::SETTING_EXACT => $form_state->getValue(self::SETTING_EXACT)]); + $this->setConfiguration([self::SETTING_CASE_SENSITIVE => $form_state->getValue(self::SETTING_CASE_SENSITIVE)]); + $this->setConfiguration([self::SETTING_INVERT => $form_state->getValue(self::SETTING_INVERT)]); + $this->setConfiguration([self::SETTING_WORD_LIST => $form_state->getValue(self::SETTING_WORD_LIST)]); + $this->setConfiguration([self::SETTING_REGEX => $form_state->getValue(self::SETTING_REGEX)]); } /** - * Get the case conversion options. - * - * @return array - * List of options, keyed by method on Drupal's unicode class. + * {@inheritdoc} */ - protected function getOptions() { - return [ - 'strtoupper' => $this->t('Convert to uppercase'), - 'strtolower' => $this->t('Convert to lowercase'), - 'ucfirst' => $this->t('Capitalize the first character'), - 'lcfirst' => $this->t('Convert the first character to lowercase'), - 'ucwords' => $this->t('Capitalize the first character of each word'), - ]; + public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { + if (strlen($form_state->getValue('phone_number')) < 3) { + $form_state->setErrorByName('phone_number', $this->t('The phone number is too short. Please enter a full phone number.')); + } + global $multibyte; + $is_multibyte = ($multibyte == UNICODE_MULTIBYTE) ? TRUE : FALSE; + + $form_state->setValue( + self::SETTING_WORD_LIST, + str_replace("\r", '', $form_state->getValue(self::SETTING_WORDS)) + ); + $word_list = explode("\n", $form_state->getValue(self::SETTING_WORDS)); + $word_list = array_map('trim', $word_list); + $form_state->setValue( + self::SETTING_WORD_LIST, + $word_list + ); + $form_state->setValue(self::SETTING_REGEX, FALSE); + + if (!empty($settings['exact']) || $settings['word_boundaries']) { + foreach ($settings['word_list'] as &$word) { + if (!empty($settings['exact'])) { + $word = '/^' . preg_quote($word, '/') . '$/u'; + } + elseif ($settings['word_boundaries']) { + // Word boundaries can only match a word with letters at the end. + if (!preg_match('/^\w(.*\w)?$/u', $word)) { + form_set_error('settings][words', t('Search text must begin and end with a letter, number, or underscore to use the %option option.', array('%option' => t('Respect word boundaries')))); + } + $word = '/\b' . preg_quote($word, '/') . '\b/u'; + } + if (!$settings['case_sensitive']) { + $word .= 'i'; + } + } + $settings['regex'] = TRUE; + } + + elseif (!$settings['word_boundaries'] && $settings['case_sensitive']) { + $settings['func'] = $is_multibyte ? 'mb_strpos' : 'strpos'; + } + elseif (!$settings['word_boundaries'] && !$settings['case_sensitive']) { + $settings['func'] = $is_multibyte ? 'mb_stripos' : 'stripos'; + } } /**