I have a block plugin that contains a form that performs a get request and reloads the current page. Currently it's set to allow a user to submit a state from a drop down box to be used as a filter.

This issue I'm having is the results from the get request are cached. It works after clearing the cache but right after, once I submit again with a new value for the dropdown the results do not update on the page, even though I can clearly see that the get request has changed. Below is the code I currently have

public function build() {
    $request = \Drupal::request();
    /* GET Variables */
    $state = (empty($request->get('state')))?'':$request->get('state');

    $render = '
    <form method="GET" class="relief-network-form">
    <select class="state" name="state">
        <option value="">Pick One</option>
        <option value="AL">Alabama</option>
        <option value="AK">Alaska</option>
        <option value="AZ">Arizona</option>
        <option value="AR">Arkansas</option>

    <input type="submit" value="Filter">
    return array(
        'test'    => [
            '#type'  => 'html_tag',
            '#tag'   => 'p',
            '#value' => 'State:'.$state.'-',
        'form'       => [
            '#type'     => 'inline_template',
            '#template' => $render,

When submitting this form after the cache is cleared it displays the proper state in the p tag above the form, but afterwards it never changes. I have a feeling it has to do with using cache tags after reading the documentation here https://www.drupal.org/docs/8/api/render-api/cacheability-of-render-arrays but to be honest I'm having trouble making sense of it, and I'm not sure how I would apply the examples to the current situation. Is there a way to prevent the results of a GET request inside a block plugin from being cached?

1 Answer 1


You don't need cache tags, you need a cache context for the query key state:

$build['#cache']['contexts'][] = 'url.query_args:state';
  • I think that Block plugins add their cache contexts via a public method, getCacheContexts.
    – mradcliffe
    Jan 23, 2017 at 17:42
  • I think what I've seen how drupal handles cache metadata it should be placed in the render array. For example if you have a field that needs own cache metadata the field formatter builds a render array with the cache metadata placed in it. This field can be in a block, a controller or anywhere else.
    – 4uk4
    Jan 23, 2017 at 17:57
  • Nothing wrong with using the block method, both work. Especially if you have conditions and only sometimes return something, then using getCacheContexts() can be easier as it is very important to always return the relevant cache contexts as your block might otherwise be cached empty for every condition
    – Berdir
    Jan 23, 2017 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.