function HandlerBase::breakString

Same name in other branches
  1. 9 core/modules/views/src/Plugin/views/HandlerBase.php \Drupal\views\Plugin\views\HandlerBase::breakString()
  2. 8.9.x core/modules/views/src/Plugin/views/HandlerBase.php \Drupal\views\Plugin\views\HandlerBase::breakString()
  3. 10 core/modules/views/src/Plugin/views/HandlerBase.php \Drupal\views\Plugin\views\HandlerBase::breakString()

Overrides ViewsHandlerInterface::breakString

5 calls to HandlerBase::breakString()
ArgumentPluginBase::unpackArgumentValue in core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php
Splits an argument into value and operator properties on this instance.
HandlerTest::testBreakString in core/modules/views/tests/src/Functional/Handler/HandlerTest.php
Tests the breakString method.
IndexTidDepth::query in core/modules/taxonomy/src/Plugin/views/argument/IndexTidDepth.php
Set up the query for this argument.
NumericArgument::query in core/modules/views/src/Plugin/views/argument/NumericArgument.php
Set up the query for this argument.
NumericArgument::title in core/modules/views/src/Plugin/views/argument/NumericArgument.php
Get the title this argument will assign the view, given the argument.

File

core/modules/views/src/Plugin/views/HandlerBase.php, line 742

Class

HandlerBase
Base class for Views handler plugins.

Namespace

Drupal\views\Plugin\views

Code

public static function breakString($str, $force_int = FALSE) {
    $operator = NULL;
    $value = [];
    // Determine if the string has 'or' operators (plus signs) or 'and'
    // operators (commas) and split the string accordingly.
    if (preg_match('/^([\\w0-9-_\\.]+[+ ]+)+[\\w0-9-_\\.]+$/u', $str)) {
        // The '+' character in a query string may be parsed as ' '.
        $operator = 'or';
        $value = preg_split('/[+ ]/', $str);
    }
    elseif (preg_match('/^([\\w0-9-_\\.]+[, ]+)*[\\w0-9-_\\.]+$/u', $str)) {
        $operator = 'and';
        $value = explode(',', $str);
    }
    // Filter any empty matches (Like from '++' in a string) and reset the
    // array keys. 'strlen' is used as the filter callback so we do not lose
    // 0 values (would otherwise evaluate == FALSE).
    $value = array_values(array_filter($value, 'strlen'));
    if ($force_int) {
        $value = array_map('intval', $value);
    }
    return (object) [
        'value' => $value,
        'operator' => $operator,
    ];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.