Commit 0946b702 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #1792836 by damiankloip, alansaviolobo, Kars-T | tim.plunkett: Merge...

Issue #1792836 by damiankloip, alansaviolobo, Kars-T | tim.plunkett: Merge HandlerBase::breakPhrase() and HandlerBase::breakPhraseString() and clean up.
parent 6f9ceadb
Loading
Loading
Loading
Loading
+4 −12
Original line number Diff line number Diff line
@@ -73,21 +73,13 @@ public function query($group_by = FALSE) {
    $this->ensureMyTable();

    if (!empty($this->options['break_phrase'])) {
      $tids = new \stdClass();
      $tids->value = $this->argument;
      $tids = $this->breakPhrase($this->argument, $tids);
      if ($tids->value == array(-1)) {
      $break = static::breakString($this->argument);
      if ($break->value === array(-1)) {
        return FALSE;
      }

      if (count($tids->value) > 1) {
        $operator = 'IN';
      }
      else {
        $operator = '=';
      }

      $tids = $tids->value;
      $operator = (count($break->value) > 1) ? 'IN' : '=';
      $tids = $break->value;
    }
    else {
      $operator = "=";
+5 −0
Original line number Diff line number Diff line
@@ -50,6 +50,11 @@ public function testArgumentTitle() {
    $this->executeView($view, array($account->id() . ',0'));
    $this->assertEqual($view->getTitle(), $account->label() . ', ' . $anonymous);
    $view->destroy();

    $view->getDisplay()->getHandler('argument', 'uid')->options['break_phrase'] = TRUE;
    $this->executeView($view, array('0,' . $account->id()));
    $this->assertEqual($view->getTitle(), $anonymous . ', ' . $account->label());
    $view->destroy();
  }

}
+24 −100
Original line number Diff line number Diff line
@@ -772,117 +772,42 @@ public function getEntityType() {
  }

  /**
   * Breaks x,y,z and x+y+z into an array. Numeric only.
   * Breaks x,y,z and x+y+z into an array.
   *
   * @param string $str
   *   The string to parse.
   * @param \Drupal\views\Plugin\views\HandlerBase|null $handler
   *   The handler object to use as a base. If not specified one will
   *   be created.
   *   The string to split.
   * @param bool $force_int
   *   Enforce a numeric check.
   *
   * @return \Drupal\views\Plugin\views\HandlerBase|stdClass $handler
   *   The new handler object.
   * @return \stdClass
   *   A stdClass object containing value and operator properties.
   */
  public static function breakPhrase($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;
    }
  public static function breakString($str, $force_int = FALSE) {
    $operator = NULL;
    $value = array();

    if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str)) {
    // 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 ' '.
      $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;
  }

  /**
   * Breaks x,y,z and x+y+z into an array. Works for strings.
   *
   * @param string $str
   *   The string to parse.
   * @param \Drupal\views\Plugin\views\HandlerBase|null $handler
   *   The object to use as a base. If not specified one will
   *   be created.
   *
   * @return \Drupal\views\Plugin\views\HandlerBase|stdClass $handler
   *   The new handler object.
   */
  public static function breakPhraseString($str, &$handler = NULL) {
    if (!$handler) {
      $handler = new \stdClass();
    }

    // Set up defaults:
    if (!isset($handler->value)) {
      $handler->value = array();
      $operator = 'or';
      $value = preg_split('/[+ ]/', $str);
    }

    if (!isset($handler->operator)) {
      $handler->operator = 'or';
    }

    if ($str == '') {
      return $handler;
    }

    // Determine if the string has 'or' operators (plus signs) or 'and' operators
    // (commas) and split the string accordingly. If we have an 'and' operator,
    // spaces are treated as part of the word being split, but otherwise they are
    // treated the same as a plus sign.
    $or_wildcard = '[^\s+,]';
    $and_wildcard = '[^+,]';
    if (preg_match("/^({$or_wildcard}+[+ ])+{$or_wildcard}+$/", $str)) {
      $handler->operator = 'or';
      $handler->value = preg_split('/[+ ]/', $str);
    }
    elseif (preg_match("/^({$and_wildcard}+,)*{$and_wildcard}+$/", $str)) {
      $handler->operator = 'and';
      $handler->value = explode(',', $str);
    elseif (preg_match('/^([\w0-9-_]+[, ]+)*[\w0-9-_]+$/u', $str)) {
      $operator = 'and';
      $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;
    }
    // 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'));

    // Doubly ensure that all values are strings only.
    foreach ($handler->value as $id => $value) {
      $handler->value[$id] = (string) $value;
    if ($force_int) {
      $value = array_map('intval', $value);
    }

    return $handler;
    return (object) array('value' => $value, 'operator' => $operator);
  }

  /**
@@ -966,5 +891,4 @@ public function submitTemporaryForm($form, FormStateInterface $form_state) {
    // Write to cache
    $form_state['view']->cacheSet();
  }

}
+12 −0
Original line number Diff line number Diff line
@@ -1151,6 +1151,18 @@ public static function encodeValidatorId($id) {
  public static function decodeValidatorId($id) {
    return str_replace('---', ':', $id);
  }

  /**
   * Splits an argument into value and operator properties on this instance.
   *
   * @param bool $force_int
   *   Enforce that values should be numeric.
   */
  protected function unpackArgumentValue($force_int = FALSE) {
    $break = static::breakString($this->argument, $force_int);
    $this->value = $break->value;
    $this->operator = $break->operator;
  }
}

/**
+4 −7
Original line number Diff line number Diff line
@@ -120,12 +120,8 @@ public function query($group_by = FALSE) {
    }

    if (!empty($this->options['break_phrase'])) {
      if (!empty($this->definition['numeric'])) {
        $this->breakPhrase($this->argument, $this);
      }
      else {
        $this->breakPhraseString($this->argument, $this);
      }
      $force_int = !empty($this->definition['numeric']);
      $this->unpackArgumentValue($force_int);
    }
    else {
      $this->value = array($this->argument);
@@ -141,7 +137,8 @@ function title() {
    }

    if (!empty($this->options['break_phrase'])) {
      $this->breakPhrase($this->argument, $this);
      $force_int = !empty($this->definition['numeric']);
      $this->unpackArgumentValue($force_int);
    }
    else {
      $this->value = array($this->argument);
Loading