You are here

public function SiteAlertBlock::build in Site Alert 8

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/SiteAlertBlock.php, line 97

Class

SiteAlertBlock
Implements SiteAlertBlock class.

Namespace

Drupal\site_alert\Plugin\Block

Code

public function build() {
  $build = [];
  $metadata = new CacheableMetadata();

  // Due to a bug in Drupal core the cache max age is ignored when the core
  // page_cache module is enabled. This bug prevents any cached pages from
  // being invalidated in time for a scheduled alert to be displayed. We can
  // work around this by injecting the JS code in the page and letting it
  // retrieve the first alert on page load. This means that even if the page
  // is cached it will poll the server for any available alerts and display
  // them. The drawback is that this will always cause a second request to be
  // sent which increases server load, so only do this if any alerts are
  // actually scheduled.
  // @todo Remove this workaround when the core bug is fixed.
  // @see https://www.drupal.org/project/site_alert/issues/3121988

  /** @var \Drupal\site_alert\SiteAlertStorage $storage */
  $storage = $this->entityTypeManager
    ->getStorage('site_alert');
  $scheduled_alerts_present = $storage
    ->getCacheMaxAge() !== CacheBackendInterface::CACHE_PERMANENT;
  $workaround_needed = $scheduled_alerts_present && $this->moduleHandler
    ->moduleExists('page_cache');
  $alerts = $this->getAlerts
    ->getActiveAlerts();
  foreach ($alerts as $alert) {
    $metadata
      ->addCacheableDependency($alert);

    // Avoid displaying the alert if we need to work around the core bug. This
    // prevents site alerts from briefly flashing in the page in case the bug
    // prevents a cached page that contains an alert from being invalidated.
    if ($workaround_needed) {
      continue;
    }
    $build[] = [
      '#theme' => 'site_alert',
      '#alert' => [
        'severity' => $alert
          ->getSeverity(),
        'label' => $alert
          ->getLabel(),
        'message' => [
          '#type' => 'markup',
          '#markup' => $alert
            ->getMessage(),
        ],
      ],
    ];
  }

  // Attach the JS code to refresh the site alerts when a timeout is
  // configured.
  $timeout = $this
    ->getConfiguration()['timeout'];
  if ($timeout > 0 || $workaround_needed) {
    $build['#attached'] = [
      'library' => [
        'site_alert/drupal.site_alert',
      ],
      'drupalSettings' => [
        'siteAlert' => [
          'timeout' => $timeout,
          'workaround_needed' => $workaround_needed,
        ],
      ],
    ];
  }
  if (!empty($build)) {
    $build['#prefix'] = '<div class="site-alert" aria-live="polite">';
    $build['#suffix'] = '</div>';
  }
  $metadata
    ->applyTo($build);
  return $build;
}