0

I have a preprocess function for a paragraph that has a variable which contains a render array. I cannot seem to find a way to disable the caching of this variable when rendered on a page, it is cached with the page regardless of what i set its max-age to, really confused and have taken to just adding #cache => ['max-age' => 0] everywhere i can think of to no avail.

Example

/**
 * Implements hook_preprocess_paragraph().
 */
function hook_preprocess_paragraph__my_paragraph(&$vars) {
  $vars['rotator'] = [
    '#cache' => [
      'max-age' => 0
    ],
    'placeholder' => [
      '#cache' => [
        'max-age' => 0
      ],
      '#lazy_builder' => [
        'my_module.controller:myMethod',
        ['test', 4],
      ],
      '#create_placeholder' => TRUE,
    ],
  ];
}


public function myMethod($view_mode, $qty = 4) {

    $nodes = $this->loadSomeNodes($qty);
    if (!empty($nodes)) {
      $build = entity_view_multiple($nodes, $view_mode, null, TRUE);
      $build['#cache']['max-age'] = 0;
      return $build;
    }
    return [];
}

and then in my paragraphs--my-paragraph.html.twig file i have

 {{ rotator }}

I need the nodes to not be cached on the page.

  • 1
    Is this for anonymous users? It might be related to the accepted answer here that the page is getting cached as a whole for anon users even though you have content with a max-age of 0 drupal.stackexchange.com/questions/237777/… – Leigh Oct 17 '18 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.