summaryrefslogtreecommitdiffstats
path: root/src/Plugin/IndexRecordContentTransformation/RemoveWords.php
blob: 290605101fa5597c931c3112ace18d7c597ff991 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php

namespace Drupal\views_natural_sort\Plugin\IndexRecordContentTransformation;

use Drupal\views_natural_sort\Plugin\IndexRecordContentTransformationBase as TransformationBase;

/**
 * @IndexRecordContentTransformation (
 *   id = "remove_words",
 *   label = @Translation("Remove Words")
 * )
 */
class RemoveWords extends TransformationBase {

  public function transform($string) {
    $words = $this->configuration['settings'];
    if (empty($words)) {
      return $string;
    }

    array_walk($words, 'preg_quote');
    return preg_replace(
      array(
        '/\s(' . implode('|', $words) . ')\s+/i',
        '/^(' . implode('|', $words) . ')\s+/i',
      ),
      array(
        ' ',
        '',
      ),
      $string
    );
  }

}