Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

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

By editor , 15 November, 2015
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;
  }

Tags

  • Drupal 8
  • Module
  • Cache
  • Add new comment
Create a simple module for Drupal 8 - Step 2 - Create Simple Page
Drupal 8 Tags based cache system
Cache Drupal 8 Render Arrays
Insert a view on a custom page or block
Disable cache for a specific page

Comments1

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Nelson (not verified)

10 years 10 months ago

Uninstalling Internal Dynamic Page Cache

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?

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer