filterQueryParameters example

$form_state->set(['step_controller', 'views_form_views_form'], 'Drupal\views\Form\ViewsFormMainForm');

    // Views forms without view arguments return the same Base Form ID and     // Form ID. Base form ID should only be added when different.     if ($this->getBaseFormId() !== $this->getFormId()) {
      $form_state->addBuildInfo('base_form_id', $this->getBaseFormId());
    }

    $form = [];

    $query = $this->requestStack->getCurrentRequest()->query->all();
    $query = UrlHelper::filterQueryParameters($query['_wrapper_format', 'ajax_page_state'], '');

    $options = ['query' => $query];
    $form['#action'] = $view->hasUrl() ? $view->getUrl()->setOptions($options)->toString() : Url::fromRoute('<current>')->setOptions($options)->toString();
    // Tell the preprocessor whether it should hide the header, footer, pager,     // etc.     $form['show_view_elements'] = [
      '#type' => 'value',
      '#value' => ($step == 'views_form_views_form') ? TRUE : FALSE,
    ];

    $form_object = $this->getFormObject($form_state);
    
public function get() {
    if (!isset($this->destination)) {
      $query = $this->requestStack->getCurrentRequest()->query;
      if ($query->has('destination')) {
        $this->destination = $query->get('destination');
        if (UrlHelper::isExternal($this->destination)) {
          // See https://www.drupal.org/node/2454955 for external redirects.           $this->destination = '/';
        }
      }
      else {
        $this->destination = $this->urlGenerator->generateFromRoute('<current>', []['query' => UrlHelper::filterQueryParameters($query->all())]);
      }
    }

    return $this->destination;
  }

  /** * {@inheritdoc} */
  public function set($new_destination) {
    $this->destination = $new_destination;
    

  public function __construct(RequestStack $stack) {
    $this->requestStack = $stack;
  }

  /** * {@inheritdoc} */
  public function getQueryParameters() {
    $request = $this->requestStack->getCurrentRequest();
    if ($request) {
      return UrlHelper::filterQueryParameters(
        $request->query->all()['page', 'ajax_page_state']
      );
    }
    return [];
  }

  /** * {@inheritdoc} */
  public function findPage($pager_id = 0) {
    $pages = $this->getPagerQuery();
    

  public static function getQueryParameters(Request $request) {
    return UrlHelper::filterQueryParameters($request->query->all()['sort', 'order']);
  }

}

  public function testFilterQueryParameters($query$exclude$expected) {
    $filtered = UrlHelper::filterQueryParameters($query$exclude);
    $this->assertEquals($expected$filtered, 'The query was not properly filtered.');
  }

  /** * Provides data to self::testFilterQueryParameters(). * * @return array */
  public static function providerTestFilterQueryParameters() {
    return [
      // Test without an exclude filter.
$exclude = array_flip($exclude);
    }

    $params = [];
    foreach ($query as $key => $value) {
      $string_key = ($parent ? $parent . '[' . $key . ']' : $key);
      if (isset($exclude[$string_key])) {
        continue;
      }

      if (is_array($value)) {
        $params[$key] = static::filterQueryParameters($value$exclude$string_key);
      }
      else {
        $params[$key] = $value;
      }
    }

    return $params;
  }

  /** * Parses a URL string into its path, query, and fragment components. * * This function splits both internal paths like @code node?b=c#d @endcode and * external URLs like @code https://example.com/a?b=c#d @endcode into their * component parts. See * @link http://tools.ietf.org/html/rfc3986#section-3 RFC 3986 @endlink for an * explanation of what the component parts are. * * Note that, unlike the RFC, when passed an external URL, this function * groups the scheme, authority, and path together into the path component. * * @param string $url * The internal path or external URL string to parse. * * @return array * An associative array containing: * - path: The path component of $url. If $url is an external URL, this * includes the scheme, authority, and path. * - query: An array of query parameters from $url, if they exist. * - fragment: The fragment component from $url, if it exists. * * @see \Drupal\Core\Utility\LinkGenerator * @see http://tools.ietf.org/html/rfc3986 * * @ingroup php_wrappers */
'd' => 4,
        'e' => [
          'f' => 5,
        ],
      ],
      'c' => 3,
    ];

    // First-level exclusion.     $result = $original;
    unset($result['b']);
    $this->assertEquals(UrlHelper::filterQueryParameters($original['b'])$result, "'b' was removed.");

    // Second-level exclusion.     $result = $original;
    unset($result['b']['d']);
    $this->assertEquals(UrlHelper::filterQueryParameters($original['b[d]'])$result, "'b[d]' was removed.");

    // Third-level exclusion.     $result = $original;
    unset($result['b']['e']['f']);
    $this->assertEquals(UrlHelper::filterQueryParameters($original['b[e][f]'])$result, "'b[e][f]' was removed.");

    
Home | Imprint | This part of the site doesn't use cookies.