Add override for #markup in preRender

Created on 23 August 2023, 10 months 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

Comments & Activities

Production build 0.69.0 2024