Create a simple module for Drupal 8 - Step 3 - Create Dynamic Page/ Block

Submitted by editor on Sun, 11/15/2015 - 19:29
Question

How to create a dynamically generated, non cached page or a block ?

In addition to the Step 2, Just add
    $form['#cache']['disabled'] = TRUE;
    $form['#cache']['max_age'] = 0;

 

    public function home() {
        $element = array(
            '#cache'=> array('max_age'=> 0),
            '#markup' => 'Hello. This is my First Page',
        );
        return $element;
    }

 

For a Block, You must use like this:

    /**
     * {@inheritdoc}
     */
    public function build() {
        $output = array();
        $output[]['#cache']['max-age'] = 0; // No cache
        $output[] = [ '#markup' => 'Time : ' . date("H:i:s"),];
       return $output;
  }

Comments

Nelson (not verified)

Mon, 01/18/2016 - 18:38

It doesn't seem right to uninstall these modules. Their description states that they will cache the page but that this will not disturb dynamic pages. It seems that if it is necessary to disable them, it must be a bug.

There is a second issue here. I read somewhere else that one should use

$output['cache']['disabled']=True;

here you say

$output['#cache']['max-age']=0;

I notice the difference in the use of "#' and the different attributes. Which is the correct one, and why?

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.