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

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;
  }

 

Big thanks to this blog author

Latest blog

Web Analytics