Drupal is a registered trademark of Dries Buytaert
Protected Pages 3.0.0 Major update available for module protected_pages (3.0.0). Commerce Core 3.3.8 Minor update available for module commerce (3.3.8). Search API HTML Element Filter 1.0.7 Minor update available for module search_api_html_element_filter (1.0.7). Layout Builder Reorder 2.0.1 Minor update available for module layout_builder_reorder (2.0.1). Ban 1.1.0 Minor update available for module ban (1.1.0). Field Formatter Range 2.0.0 Major update available for module field_formatter_range (2.0.0). Field Formatter Range 8.x-1.8 Minor update available for module field_formatter_range (8.x-1.8). Varbase Media Header 9.2.1 Minor update available for module varbase_media_header (9.2.1). Search API Solr 4.3.11 Module search_api_solr updated after 14 months of inactivity (4.3.11). Provus Mega Menu Module provus_mega_menu crossed 1,000 active installs.

This module provides a configurable block that displays random items from a selected content type. You can choose which content type and fields to display, and control whether the block's content is cached and for how long.

This Drupal module provides a configurable block that displays a random item from a selected content type. It is designed to be flexible, performant, and standards-compliant.

FEATURES

  • Content Selection: Choose any Content Type (Bundle) to source items from.
  • Field Selection: Select specifically which fields to display (e.g., Title, Body, Image).
  • Randomization: Fetches random published items.
  • AJAX Loading: Uses a "Signed Configuration" AJAX pattern to bypass Drupal's Internal Page Cache, allowing the block to refresh even on fully cached pages.
  • Caching Options:
    • No Cache: Random item on every request.
    • Time-based (TTL): Cache the random selection for a specific duration (e.g., "Scripture of the Day").
    • Uses Drupal's Cache API to ensure high performance for the AJAX endpoint.
  • Item Count: Configure how many random items to show.

REQUIREMENTS

  • Drupal 10.x or 11.x

INSTALLATION

Install as you would normally install a contributed Drupal module. Visit: https://www.drupal.org/node/1897420 for further information.

CONFIGURATION

  1. Go to Structure > Block layout.
  2. Click Place block in the desired region.
  3. Search for Random Content Block.
  4. Configure the settings:
    • Content Type: Select the source content type. The form will refresh to show available fields.
    • Fields to Display: Check the fields you want to appear in the block.
    • Item Count: Number of items to display.
    • Cache Duration (seconds): Enter how long the random choice should stick in seconds. 0 means no cache.
  5. Click Save block.

Support development on this module

A lot of hard work and dedication has gone into developing this module. If you find it helpful and would like to support ongoing development, consider buying me a coffee! Your support helps ensure that I can continue enhancing and maintaining this module for everyone. Thank you for your generosity!


Architecture

This module implements the Signed Configuration AJAX Pattern to solve the "Page Cache Paradox" (where static page caching prevents dynamic blocks from updating for anonymous users).

  • Block (RandomContentBlock): Renders a secure placeholder. It generates a set of configuration parameters signed with a cryptographic HMAC to prevent tampering.
  • Controller (RandomContentController): A stateless endpoint that validates the cryptographic signature and renders the content fragment.
  • Service (RandomContentFetcher): Handles the logic of querying content types, fields, and random nodes.
  • JavaScript (random-content-block.js): A lightweight loader that fetches the signed URL and injects the content into the placeholder.

Testing

This module includes Kernel and Functional tests.

To run the tests:

phpunit modules/custom/random_content_block

Troubleshooting: Caching Issues

Standard Drupal blocks often suffer from the Internal Page Cache limitation, where the max-age of a block is ignored and the entire page is cached permanently for anonymous users.

Drupal core's Internal Page Cache module ignores max-age bubbled metadata. To ensure the Cache-Control header correctly reflects the configured "Cache Duration" even when the Internal Page Cache is active, it is recommended to install the page_cache_max_age_bubbling module.

Random Content Block uses AJAX to retrieve the block content because it seems that blocks in regions outside of the main content have a different bubbling strategy and won't bubble to the top even when using the above module. By retrieving with AJAX, the request is handled in isolation and gets the proper max-age and expires time.

Advanced Caching

If you are using external caches (Varnish, Cloudflare, etc.), ensure they are configured to respect the Cache-Control: max-age header returned by the AJAX endpoint (/random-content-block/render).

References:

Activity

Total releases
1
First release
Jan 2026
Latest release
5 months ago
Releases (12 mo)
1 ▲ from 0
Maintenance
Active

Releases

Version Type Release date
1.0.x-dev Dev Jan 26, 2026