String.php

Definition of Drupal\views\Plugin\views\filter\String.

Namespace

Drupal\views\Plugin\views\filter

File

drupal/core/modules/views/lib/Drupal/views/Plugin/views/filter/String.php
View source
<?php

/**
 * @file
 * Definition of Drupal\views\Plugin\views\filter\String.
 */
namespace Drupal\views\Plugin\views\filter;

use Drupal\Core\Database\Database;
use Drupal\Component\Annotation\PluginID;

/**
 * Basic textfield filter to handle string filtering commands
 * including equality, like, not like, etc.
 *
 * @ingroup views_filter_handlers
 *
 * @PluginID("string")
 */
class String extends FilterPluginBase {

  // exposed filter options
  var $always_multiple = TRUE;
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['expose']['contains']['required'] = array(
      'default' => FALSE,
      'bool' => TRUE,
    );
    return $options;
  }

  /**
   * This kind of construct makes it relatively easy for a child class
   * to add or remove functionality by overriding this function and
   * adding/removing items from this array.
   */
  function operators() {
    $operators = array(
      '=' => array(
        'title' => t('Is equal to'),
        'short' => t('='),
        'method' => 'opEqual',
        'values' => 1,
      ),
      '!=' => array(
        'title' => t('Is not equal to'),
        'short' => t('!='),
        'method' => 'opEqual',
        'values' => 1,
      ),
      'contains' => array(
        'title' => t('Contains'),
        'short' => t('contains'),
        'method' => 'opContains',
        'values' => 1,
      ),
      'word' => array(
        'title' => t('Contains any word'),
        'short' => t('has word'),
        'method' => 'opContainsWord',
        'values' => 1,
      ),
      'allwords' => array(
        'title' => t('Contains all words'),
        'short' => t('has all'),
        'method' => 'opContainsWord',
        'values' => 1,
      ),
      'starts' => array(
        'title' => t('Starts with'),
        'short' => t('begins'),
        'method' => 'opStartsWith',
        'values' => 1,
      ),
      'not_starts' => array(
        'title' => t('Does not start with'),
        'short' => t('not_begins'),
        'method' => 'opNotStartsWith',
        'values' => 1,
      ),
      'ends' => array(
        'title' => t('Ends with'),
        'short' => t('ends'),
        'method' => 'opEndsWith',
        'values' => 1,
      ),
      'not_ends' => array(
        'title' => t('Does not end with'),
        'short' => t('not_ends'),
        'method' => 'opNotEnds',
        'values' => 1,
      ),
      'not' => array(
        'title' => t('Does not contain'),
        'short' => t('!has'),
        'method' => 'opNotLike',
        'values' => 1,
      ),
      'shorterthan' => array(
        'title' => t('Length is shorter than'),
        'short' => t('shorter than'),
        'method' => 'opShorterThan',
        'values' => 1,
      ),
      'longerthan' => array(
        'title' => t('Length is longer than'),
        'short' => t('longer than'),
        'method' => 'opLongerThan',
        'values' => 1,
      ),
    );

    // if the definition allows for the empty operator, add it.
    if (!empty($this->definition['allow empty'])) {
      $operators += array(
        'empty' => array(
          'title' => t('Is empty (NULL)'),
          'method' => 'opEmpty',
          'short' => t('empty'),
          'values' => 0,
        ),
        'not empty' => array(
          'title' => t('Is not empty (NOT NULL)'),
          'method' => 'opEmpty',
          'short' => t('not empty'),
          'values' => 0,
        ),
      );
    }

    // Add regexp support for MySQL.
    if (Database::getConnection()
      ->databaseType() == 'mysql') {
      $operators += array(
        'regular_expression' => array(
          'title' => t('Regular expression'),
          'short' => t('regex'),
          'method' => 'opRegex',
          'values' => 1,
        ),
      );
    }
    return $operators;
  }

  /**
   * Build strings from the operators() for 'select' options
   */
  public function operatorOptions($which = 'title') {
    $options = array();
    foreach ($this
      ->operators() as $id => $info) {
      $options[$id] = $info[$which];
    }
    return $options;
  }
  public function adminSummary() {
    if ($this
      ->isAGroup()) {
      return t('grouped');
    }
    if (!empty($this->options['exposed'])) {
      return t('exposed');
    }
    $options = $this
      ->operatorOptions('short');
    $output = '';
    if (!empty($options[$this->operator])) {
      $output = check_plain($options[$this->operator]);
    }
    if (in_array($this->operator, $this
      ->operatorValues(1))) {
      $output .= ' ' . check_plain($this->value);
    }
    return $output;
  }
  protected function operatorValues($values = 1) {
    $options = array();
    foreach ($this
      ->operators() as $id => $info) {
      if (isset($info['values']) && $info['values'] == $values) {
        $options[] = $id;
      }
    }
    return $options;
  }

  /**
   * Provide a simple textfield for equality
   */
  protected function valueForm(&$form, &$form_state) {

    // We have to make some choices when creating this as an exposed
    // filter form. For example, if the operator is locked and thus
    // not rendered, we can't render dependencies; instead we only
    // render the form items we need.
    $which = 'all';
    if (!empty($form['operator'])) {
      $source = ':input[name="options[operator]"]';
    }
    if (!empty($form_state['exposed'])) {
      $identifier = $this->options['expose']['identifier'];
      if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {

        // exposed and locked.
        $which = in_array($this->operator, $this
          ->operatorValues(1)) ? 'value' : 'none';
      }
      else {
        $source = ':input[name="' . $this->options['expose']['operator_id'] . '"]';
      }
    }
    if ($which == 'all' || $which == 'value') {
      $form['value'] = array(
        '#type' => 'textfield',
        '#title' => t('Value'),
        '#size' => 30,
        '#default_value' => $this->value,
      );
      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
        $form_state['input'][$identifier] = $this->value;
      }
      if ($which == 'all') {

        // Setup #states for all operators with one value.
        foreach ($this
          ->operatorValues(1) as $operator) {
          $form['value']['#states']['visible'][] = array(
            $source => array(
              'value' => $operator,
            ),
          );
        }
      }
    }
    if (!isset($form['value'])) {

      // Ensure there is something in the 'value'.
      $form['value'] = array(
        '#type' => 'value',
        '#value' => NULL,
      );
    }
  }
  function operator() {
    return $this->operator == '=' ? 'LIKE' : 'NOT LIKE';
  }

  /**
   * Add this filter to the query.
   *
   * Due to the nature of fapi, the value and the operator have an unintended
   * level of indirection. You will find them in $this->operator
   * and $this->value respectively.
   */
  public function query() {
    $this
      ->ensureMyTable();
    $field = "{$this->tableAlias}.{$this->realField}";
    $info = $this
      ->operators();
    if (!empty($info[$this->operator]['method'])) {
      $this
        ->{$info[$this->operator]['method']}($field);
    }
  }
  public function opEqual($field) {
    $this->query
      ->addWhere($this->options['group'], $field, $this->value, $this
      ->operator());
  }
  protected function opContains($field) {
    $this->query
      ->addWhere($this->options['group'], $field, '%' . db_like($this->value) . '%', 'LIKE');
  }
  protected function opContainsWord($field) {
    $where = $this->operator == 'word' ? db_or() : db_and();

    // Don't filter on empty strings.
    if (empty($this->value)) {
      return;
    }
    preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' ' . $this->value, $matches, PREG_SET_ORDER);
    foreach ($matches as $match) {
      $phrase = FALSE;

      // Strip off phrase quotes
      if ($match[2][0] == '"') {
        $match[2] = substr($match[2], 1, -1);
        $phrase = TRUE;
      }
      $words = trim($match[2], ',?!();:-');
      $words = $phrase ? array(
        $words,
      ) : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
      foreach ($words as $word) {
        $placeholder = $this
          ->placeholder();
        $where
          ->condition($field, '%' . db_like(trim($word, " ,!?")) . '%', 'LIKE');
      }
    }
    if (!$where) {
      return;
    }

    // previously this was a call_user_func_array but that's unnecessary
    // as views will unpack an array that is a single arg.
    $this->query
      ->addWhere($this->options['group'], $where);
  }
  protected function opStartsWith($field) {
    $this->query
      ->addWhere($this->options['group'], $field, db_like($this->value) . '%', 'LIKE');
  }
  protected function opNotStartsWith($field) {
    $this->query
      ->addWhere($this->options['group'], $field, db_like($this->value) . '%', 'NOT LIKE');
  }
  protected function opEndsWith($field) {
    $this->query
      ->addWhere($this->options['group'], $field, '%' . db_like($this->value), 'LIKE');
  }
  protected function opNotEnds($field) {
    $this->query
      ->addWhere($this->options['group'], $field, '%' . db_like($this->value), 'NOT LIKE');
  }
  protected function opNotLike($field) {
    $this->query
      ->addWhere($this->options['group'], $field, '%' . db_like($this->value) . '%', 'NOT LIKE');
  }
  protected function opShorterThan($field) {
    $placeholder = $this
      ->placeholder();
    $this->query
      ->add_where_expression($this->options['group'], "LENGTH({$field}) < {$placeholder}", array(
      $placeholder => $this->value,
    ));
  }
  protected function opLongerThan($field) {
    $placeholder = $this
      ->placeholder();
    $this->query
      ->add_where_expression($this->options['group'], "LENGTH({$field}) > {$placeholder}", array(
      $placeholder => $this->value,
    ));
  }
  protected function opRegex($field) {
    $this->query
      ->addWhere($this->options['group'], $field, $this->value, 'RLIKE');
  }
  protected function opEmpty($field) {
    if ($this->operator == 'empty') {
      $operator = "IS NULL";
    }
    else {
      $operator = "IS NOT NULL";
    }
    $this->query
      ->addWhere($this->options['group'], $field, NULL, $operator);
  }

}

Classes

Namesort descending Description
String Basic textfield filter to handle string filtering commands including equality, like, not like, etc.