summaryrefslogtreecommitdiffstats
path: root/src/Plugin/IndexRecordContentTransformation/RemoveBeginningWords.php
blob: e773ba14611ac9326e00dcc6c3e36e47b400194b (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
<?php

namespace Drupal\views_natural_sort\Plugin\IndexRecordContentTransformation;

use Drupal\views_natural_sort\Plugin\IndexRecordContentTransformationBase as TransformationBase;

/**
 * @IndexRecordContentTransformation (
 *   id = "remove_beginning_words",
 *   label = @Translation("Remove Beginning Words")
 * )
 */
class RemoveBeginningWords extends TransformationBase {

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

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

}