You are here

class MessagesContext in Better Messages 8

Provides current Drupal status messages as a context.

Hierarchy

Expanded class hierarchy of MessagesContext

1 string reference to 'MessagesContext'
better_messages.services.yml in ./better_messages.services.yml
better_messages.services.yml
1 service uses MessagesContext
better_messages.context in ./better_messages.services.yml
Drupal\better_messages\ContextProvider\MessagesContext

File

src/ContextProvider/MessagesContext.php, line 14

Namespace

Drupal\better_messages\ContextProvider
View source
class MessagesContext implements ContextProviderInterface {
  use StringTranslationTrait;

  /**
   * Current status messages.
   *
   * If this property is NULL, it means we haven't hit the rendering yet and
   * current messages can be queried from Drupal core. Otherwise the currently
   * rendered messages will be stored in here.
   *
   * @var array
   */
  protected $messages = NULL;

  /**
   * {@inheritdoc}
   */
  public function getRuntimeContexts(array $unqualified_context_ids) {
    $result = [];
    $context_definition = new ContextDefinition('map', NULL, FALSE);
    $context = new Context($context_definition, $this
      ->getMessages());
    $cacheability = new CacheableMetadata();

    // We cannot cache this context as literally a few lines below some message
    // might be added.
    $cacheability
      ->setCacheMaxAge(0);

    // Since messages are stored in session, we must vary by it.
    $cacheability
      ->addCacheContexts([
      'session',
    ]);
    $context
      ->addCacheableDependency($cacheability);
    $result['better_messages'] = $context;
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableContexts() {
    $context = new Context(new ContextDefinition('map', $this
      ->t('Current status messages')));
    return [
      'better_messages' => $context,
    ];
  }

  /**
   * Store currently rendered status messages.
   *
   * @param array $messages
   *   Messages array to store (supposedly it should be the currently rendered
   *   ones)
   */
  public function setMessages(array $messages) {
    $this->messages = $messages;
  }

  /**
   * Retrieve current status messages.
   *
   * @return array
   *   Array of current messages. It will either be a result of
   *   drupal_get_messages() or $this->messages should we already be beyond the
   *   phase of rendering status messages on the current page request
   */
  protected function getMessages() {
    return is_null($this->messages) ? drupal_get_messages(NULL, FALSE) : $this->messages;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MessagesContext::$messages protected property Current status messages.
MessagesContext::getAvailableContexts public function Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface::getAvailableContexts
MessagesContext::getMessages protected function Retrieve current status messages.
MessagesContext::getRuntimeContexts public function Gets runtime context values for the given context IDs. Overrides ContextProviderInterface::getRuntimeContexts
MessagesContext::setMessages public function Store currently rendered status messages.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.