Unverified Commit 0def9dc0 authored by Alex Pott's avatar Alex Pott
Browse files

task: #3572243 Deprecate several views functions

By: nicxvan
By: mstrelan
By: godotislate
By: joachim
By: catch
By: berdir
By: larowlan
By: alexpott
(cherry picked from commit 188f4662)
parent 134cf9f7
Loading
Loading
Loading
Loading
Loading
+40 −1
Original line number Diff line number Diff line
@@ -221,7 +221,7 @@ public function queryViewsAlter(AlterableInterface $query): void {
      }
    }
    // Replaces substitutions in filter criteria.
    _views_query_tag_alter_condition($query, $where, $substitutions);
    $this->viewsQueryTagAlterCondition($query, $where, $substitutions);
  }

  /**
@@ -259,4 +259,43 @@ public function blockPresave(BlockInterface $block): void {
    }
  }

  /**
   * Replaces the substitutions recursive foreach condition.
   */
  protected function viewsQueryTagAlterCondition(AlterableInterface $query, array &$conditions, array $substitutions): void {
    foreach ($conditions as $condition_id => &$condition) {
      if (is_numeric($condition_id)) {
        if (is_string($condition['field'])) {
          $condition['field'] = str_replace(array_keys($substitutions), array_values($substitutions), $condition['field']);
        }
        elseif (is_object($condition['field'])) {
          $sub_conditions = &$condition['field']->conditions();
          $this->viewsQueryTagAlterCondition($query, $sub_conditions, $substitutions);
        }
        // $condition['value'] is a subquery so alter the subquery recursive.
        // Therefore make sure to get the metadata of the main query.
        if (is_object($condition['value'])) {
          $subquery = $condition['value'];
          $subquery->addMetaData('views_substitutions', $query->getMetaData('views_substitutions'));
          \Drupal::moduleHandler()->invoke('views', 'query_views_alter', [$condition['value']]);
        }
        elseif (isset($condition['value'])) {
          // We can not use a simple str_replace() here because it always
          // returns a string and we have to keep the type of the condition
          // value intact.
          if (is_array($condition['value'])) {
            foreach ($condition['value'] as &$value) {
              if (is_string($value)) {
                $value = str_replace(array_keys($substitutions), array_values($substitutions), $value);
              }
            }
          }
          elseif (is_string($condition['value'])) {
            $condition['value'] = str_replace(array_keys($substitutions), array_values($substitutions), $condition['value']);
          }
        }
      }
    }
  }

}
+16 −1
Original line number Diff line number Diff line
@@ -345,7 +345,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
      '#title' => $this->t('Query Tags'),
      '#description' => $this->t('If set, these tags will be appended to the query and can be used to identify the query in a module. This can be helpful for altering queries.'),
      '#default_value' => implode(', ', $this->options['query_tags']),
      '#element_validate' => ['views_element_validate_tags'],
      '#element_validate' => [[static::class, 'elementValidateTags']],
    ];
  }

@@ -1908,4 +1908,19 @@ public function getDateFormat($field, $format, $string_date = FALSE) {
    return $this->dateSql->getDateFormat($field, $format);
  }

  /**
   * Validation callback for query tags.
   *
   * @internal
   */
  public static function elementValidateTags(array &$element, FormStateInterface $form_state): void {
    $values = array_map('trim', explode(',', $element['#value']));
    foreach ($values as $value) {
      if (preg_match("/[^a-z_]/", $value)) {
        $form_state->setError($element, t('The query tags may only contain lower-case alphabetical characters and underscores.'));
        return;
      }
    }
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -303,7 +303,7 @@ protected function leftQuery($options) {
  /**
   * Recursive helper to add a namespace to conditions.
   *
   * Similar to _views_query_tag_alter_condition().
   * Similar to ViewsHooks::viewsQueryTagAlterCondition().
   *
   * (Though why is the condition we get in a simple query 3 levels deep???)
   */
+9 −5
Original line number Diff line number Diff line
@@ -548,7 +548,7 @@ public function save() {
   */
  public function setArguments(array $args) {
    // The array keys of the arguments will be incorrect if set by
    // views_embed_view() or \Drupal\views\ViewExecutable:preview().
    // \Drupal\views\ViewExecutable:preview().
    $this->args = array_values($args);
  }

@@ -1699,7 +1699,7 @@ public function executeDisplay($display_id = NULL, $args = []) {
   * This function does not do any access checks on the view. It is the
   * responsibility of the caller to check $view->access() or implement other
   * access logic. To render the view normally with access checks, use
   * views_embed_view() instead.
   * '#type' => 'view' render elements instead.
   *
   * @return array|null
   *   A renderable array containing the view output or NULL if the display ID
@@ -1728,8 +1728,10 @@ public function preview($display_id = NULL, $args = []) {
   *   An array of arguments from the URL that can be used by the view.
   */
  public function preExecute($args = []) {
    $this->old_view[] = views_get_current_view();
    views_set_current_view($this);
    // @todo remove when views_set|get_current_view is removed.
    // https://www.drupal.org/project/drupal/issues/3572671
    $this->old_view[] = $GLOBALS['_current_view'] ?? NULL;
    $GLOBALS['_current_view'] = $this;
    $display_id = $this->current_display;

    // Prepare the view with the information we have, but only if we were
@@ -1759,7 +1761,9 @@ public function postExecute() {
      $old_view = array_pop($this->old_view);
    }

    views_set_current_view($old_view ?? FALSE);
    // @todo remove when views_set|get_current_view is removed.
    // https://www.drupal.org/project/drupal/issues/3572671
    $GLOBALS['_current_view'] = $old_view ?? FALSE;
  }

  /**
+38 −0
Original line number Diff line number Diff line
@@ -559,4 +559,42 @@ protected static function t($string, array $args = [], array $options = []) {
    return static::$translationManager->translate($string, $args, $options);
  }

  /**
   * Get the result of a view.
   *
   * @param string $name
   *   The name of the view to retrieve the data from.
   * @param string|null $display_id
   *   The display ID. On the edit page for the view in question, you'll find a
   *   list of displays at the left side of the control area. "Default" will be
   *   at the top of that list. Hover your cursor over the name of the display
   *   you want to use. A URL will appear in the status bar of your browser.
   *   This is usually at the bottom of the window, in the chrome. Everything
   *   after #views-tab- is the display ID, e.g. page_1.
   * @param mixed ...$args
   *   Any additional parameters will be passed as arguments.
   *
   * @return array
   *   An array containing an object for each view item.
   */
  public static function getViewResult(string $name, string|null $display_id = NULL, mixed ...$args): array {
    $view = static::getView($name);
    if (is_object($view)) {
      if (is_array($args)) {
        $view->setArguments($args);
      }
      if (is_string($display_id)) {
        $view->setDisplay($display_id);
      }
      else {
        $view->initDisplay();
      }
      $view->preExecute();
      $view->execute();
      return $view->result;
    }

    return [];
  }

}
Loading