Add override for #markup in preRender

Created on 23 August 2023, about 1 year ago
Updated 3 October 2024, about 1 month ago

This is a simple alteration that would allow the Loading... text to be configurable via the render element.

In AjaxPlaceholder.php change the #markup element to get it's value from the render element.

public static function preRender(array $element) {
    // Set the callback data in the tempstore and use a temporary hash to
    // retrieve it.
    $tempstore = \Drupal::service('tempstore.shared')->get('ajax_placeholder');
    $hash = md5(Json::encode($element['#callback']));
    $tempstore->set($hash, [
      'url' => \Drupal::request()->getRequestUri(),
      'callback' => $element['#callback'],
    ]);
    return [
      'container' => [
        '#type' => 'container',
        '#callback' => '',
        '#attributes' => [
          'class' => 'js-ajax-placeholder',
          'data-hash' => $hash,
        ],
        'content' => [
          '#markup' => $element['#markup'] ?: 'Loading...',
        ],
        '#attached' => [
          'library' => ['ajax_placeholder/ajax'],
        ],
      ],
    ];
  }

Then in the render element it can be defined with a custom loading message image.

$build['awesome'] = [
  '#type' => 'ajax_placeholder',
  '#callback' => ['mymodule.service:callback', [$arg1, $arg2]],
  '#markup' => 'My custom loading message ....',
];
✨ Feature request
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States RustedBucket

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024