You are here

function template_preprocess_better_messages_wrapper in Better Messages 8

Preprocess for 'better_messages_wrapper' theme hook.

File

./better_messages.module, line 84
Better messages module file.

Code

function template_preprocess_better_messages_wrapper(&$variables) {
  $settings = \Drupal::config('better_messages.settings');
  $cache = CacheableMetadata::createFromRenderArray($variables);
  $cache
    ->addCacheTags($settings
    ->getCacheTags());
  $cache
    ->addCacheContexts($settings
    ->getCacheContexts());
  $cache
    ->mergeCacheMaxAge($settings
    ->getCacheMaxAge());

  // Inject the messages from $variables into our "better_messages" context
  // since now we have crossed beyond the point where "current" messages are
  // retrievable via drupal_get_messages().
  \Drupal::service('better_messages.context')
    ->setMessages($variables['message_list']);

  /** @var \Drupal\Core\Condition\ConditionManager $condition_manager */
  $condition_manager = \Drupal::service('plugin.manager.condition');

  /** @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository */
  $context_repository = \Drupal::service('context.repository');

  /** @var \Drupal\Core\Plugin\Context\ContextHandlerInterface $context_handler */
  $context_handler = \Drupal::service('context.handler');
  $pass = TRUE;
  foreach ($settings
    ->get('visibility') as $condition_id => $condition) {
    $condition = $condition_manager
      ->createInstance($condition_id, $condition);
    if ($condition instanceof ContextAwarePluginInterface) {
      try {
        $contexts = $context_repository
          ->getRuntimeContexts(array_values($condition
          ->getContextMapping()));
        $context_handler
          ->applyContextMapping($condition, $contexts);
      } catch (ContextException $e) {

        // We couldn't initialize the condition, so it just cannot participate.
        continue;
      }
    }
    try {
      $pass = $condition
        ->execute();
    } catch (ContextException $e) {

      // Condition couldn't evaluate itself. Let's skip it.
      continue;
    }
    if ($condition instanceof CacheableDependencyInterface) {
      $cache
        ->addCacheTags($condition
        ->getCacheTags());
      $cache
        ->addCacheContexts($condition
        ->getCacheContexts());
      $cache
        ->mergeCacheMaxAge($condition
        ->getCacheMaxAge());
    }
    if (!$pass) {
      break;
    }
  }
  $cache
    ->applyTo($variables);
  if ($pass && !empty($variables['message_list'])) {
    $variables['attributes']['class'][] = 'better-messages-overlay';
    if (\Drupal::config('better_messages.settings')
      ->get('fixed')) {
      $variables['attributes']['class'][] = 'better-messages-position-fixed';
    }
    if (isset($variables['message_list']['error'])) {
      $variables['attributes']['class'][] = 'better-messages-has-errors';
    }
  }
}