You are here

function views_break_phrase in Views (for Drupal 7) 7.3

Same name and namespace in other branches
  1. 6.3 includes/handlers.inc \views_break_phrase()
  2. 6.2 includes/handlers.inc \views_break_phrase()

Break x,y,z and x+y+z into an array. Numeric only.

Parameters

string $str: The string to parse.

object $handler: The handler object to use as a base. If not specified one will be created.

Return value

$handler The new handler object.

9 calls to views_break_phrase()
ViewsHandlersTest::test_views_break_phrase in tests/views_handlers.test
Tests views_break_phrase function.
views_handler_argument_many_to_one::query in handlers/views_handler_argument_many_to_one.inc
Set up the query for this argument.
views_handler_argument_many_to_one::title in handlers/views_handler_argument_many_to_one.inc
Get the title this argument will assign the view, given the argument.
views_handler_argument_numeric::query in handlers/views_handler_argument_numeric.inc
Set up the query for this argument.
views_handler_argument_numeric::title in handlers/views_handler_argument_numeric.inc
Get the title this argument will assign the view, given the argument.

... See full list

File

includes/handlers.inc, line 1200
Defines the various handler objects to help build and display views.

Code

function views_break_phrase($str, &$handler = NULL) {
  if (!$handler) {
    $handler = new stdClass();
  }

  // Set up defaults.
  if (!isset($handler->value)) {
    $handler->value = array();
  }
  if (!isset($handler->operator)) {
    $handler->operator = 'or';
  }
  if (empty($str)) {
    return $handler;
  }
  if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str)) {

    // The '+' character in a query string may be parsed as ' '.
    $handler->operator = 'or';
    $handler->value = preg_split('/[+ ]/', $str);
  }
  elseif (preg_match('/^([0-9]+,)*[0-9]+$/', $str)) {
    $handler->operator = 'and';
    $handler->value = explode(',', $str);
  }

  // Keep an 'error' value if invalid strings were given.
  if (!empty($str) && (empty($handler->value) || !is_array($handler->value))) {
    $handler->value = array(
      -1,
    );
    return $handler;
  }

  // Doubly ensure that all values are numeric only.
  foreach ($handler->value as $id => $value) {
    $handler->value[$id] = intval($value);
  }
  return $handler;
}