You are here

class WebformMessage in Webform 6.x

Same name in this branch
  1. 6.x src/Element/WebformMessage.php \Drupal\webform\Element\WebformMessage
  2. 6.x src/Plugin/WebformElement/WebformMessage.php \Drupal\webform\Plugin\WebformElement\WebformMessage
Same name and namespace in other branches
  1. 8.5 src/Element/WebformMessage.php \Drupal\webform\Element\WebformMessage

Provides a render element for message.

Plugin annotation

@FormElement("webform_message");

Hierarchy

Expanded class hierarchy of WebformMessage

39 files declare their use of WebformMessage
Captcha.php in src/Plugin/WebformElement/Captcha.php
DateBase.php in src/Plugin/WebformElement/DateBase.php
EmailWebformHandler.php in src/Plugin/WebformHandler/EmailWebformHandler.php
OptionsLimitWebformHandler.php in modules/webform_options_limit/src/Plugin/WebformHandler/OptionsLimitWebformHandler.php
RemotePostWebformHandler.php in src/Plugin/WebformHandler/RemotePostWebformHandler.php

... See full list

93 #type uses of WebformMessage
Captcha::form in src/Plugin/WebformElement/Captcha.php
Gets the actual configuration webform array to be built.
DateBase::form in src/Plugin/WebformElement/DateBase.php
Gets the actual configuration webform array to be built.
DateTime::form in src/Plugin/WebformElement/DateTime.php
Gets the actual configuration webform array to be built.
DelimitedWebformExporter::buildConfigurationForm in src/Plugin/WebformExporter/DelimitedWebformExporter.php
Form constructor.
EmailWebformHandler::buildConfigurationForm in src/Plugin/WebformHandler/EmailWebformHandler.php
Form constructor.

... See full list

File

src/Element/WebformMessage.php, line 14

Namespace

Drupal\webform\Element
View source
class WebformMessage extends RenderElement {

  /**
   * Storage none.
   */
  const STORAGE_NONE = '';

  /**
   * Storage local.
   */
  const STORAGE_LOCAL = 'local';

  /**
   * Storage session.
   */
  const STORAGE_SESSION = 'session';

  /**
   * Storage user (data).
   */
  const STORAGE_USER = 'user';

  /**
   * Storage state (API).
   */
  const STORAGE_STATE = 'state';

  /**
   * Storage custom.
   *
   * @see hook_webform_message_custom()
   */
  const STORAGE_CUSTOM = 'custom';

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#message_type' => 'status',
      '#message_message' => '',
      '#message_close' => FALSE,
      '#message_close_effect' => 'slide',
      '#message_id' => '',
      '#message_storage' => '',
      '#status_headings' => [],
      '#pre_render' => [
        [
          $class,
          'preRenderWebformMessage',
        ],
      ],
      '#theme_wrappers' => [
        'webform_message',
      ],
    ];
  }

  /**
   * Create status message for rendering.
   *
   * @param array $element
   *   An associative array containing the properties and children of the
   *   element.
   *
   * @return array
   *   The modified element with status message.
   */
  public static function preRenderWebformMessage(array $element) {
    $message_type = $element['#message_type'];
    $message_close = $element['#message_close'];
    $message_close_effect = $element['#message_close_effect'];
    $message_id = $element['#message_id'];
    $message_storage = $element['#message_storage'];
    $message_message = $element['#message_message'];
    $element['#attributes']['class'][] = 'webform-message';
    $element['#attributes']['class'][] = 'js-webform-message';

    // Ignore 'user' and 'state' storage is current user is anonymous.
    if (\Drupal::currentUser()
      ->isAnonymous() && in_array($message_storage, [
      static::STORAGE_USER,
      static::STORAGE_STATE,
      static::STORAGE_CUSTOM,
    ])) {
      $message_storage = '';
    }

    // Build the messages render array.
    $messages = [];

    // Add close button as the first message.
    if ($message_close) {
      $element['#attributes']['data-message-close-effect'] = $message_close_effect;
      $element['#attributes']['class'][] = 'webform-message--close';
      $element['#attributes']['class'][] = 'js-webform-message--close';
      $close_attributes = [
        'aria-label' => t('close'),
        'class' => [
          'js-webform-message__link',
          'webform-message__link',
        ],
      ];
      if (in_array($message_storage, [
        static::STORAGE_USER,
        static::STORAGE_STATE,
        static::STORAGE_CUSTOM,
      ])) {
        $close_url = Url::fromRoute('webform.element.message.close', [
          'storage' => $message_storage,
          'id' => $message_id,
        ]);
      }
      else {
        $close_url = Url::fromRoute('<none>', [], [
          'fragment' => 'close',
        ]);
      }
      $messages[] = [
        '#type' => 'link',
        '#title' => '×',
        '#url' => $close_url,
        '#attributes' => $close_attributes,
      ];

      // Add close attributes and check is message is closed.
      if ($message_storage && $message_id) {
        $element['#attributes']['data-message-id'] = $message_id;
        $element['#attributes']['data-message-storage'] = $message_storage;
        $element['#attributes']['class'][] = 'js-webform-message--close-storage';
        if (static::isClosed($message_storage, $message_id)) {
          $element['#closed'] = TRUE;
        }
      }
    }

    // Add messages to container children.
    $messages[] = !is_array($message_message) ? [
      '#markup' => $message_message,
    ] : $message_message;
    foreach (Element::children($element) as $key) {
      $messages[] = $element[$key];
      unset($element[$key]);
    }

    // Add status messages as the message.
    $element['#message'] = [
      '#theme' => 'status_messages',
      '#message_list' => [
        $message_type => [
          $messages,
        ],
      ],
      '#status_headings' => $element['#status_headings'] + [
        'status' => t('Status message'),
        'error' => t('Error message'),
        'warning' => t('Warning message'),
      ],
    ];
    $element['#attached']['library'][] = 'webform/webform.element.message';
    return $element;
  }

  /****************************************************************************/

  // Manage closed functions.

  /****************************************************************************/

  /**
   * Is message closed via User Data, State API, or Custom.
   *
   * @param string $storage
   *   The storage mechanism to check if a message is closed.
   * @param string $id
   *   The ID of the message.
   *
   * @return bool
   *   TRUE if the message is closed.
   */
  public static function isClosed($storage, $id) {
    $account = \Drupal::currentUser();
    $namespace = 'webform.element.message';
    switch ($storage) {
      case static::STORAGE_STATE:

        /** @var \Drupal\Core\State\StateInterface $state */
        $state = \Drupal::service('state');
        $values = $state
          ->get($namespace, []);
        return isset($values[$id]) ? TRUE : FALSE;
      case static::STORAGE_USER:

        /** @var \Drupal\user\UserDataInterface $user_data */
        $user_data = \Drupal::service('user.data');
        $values = $user_data
          ->get('webform', $account
          ->id(), $namespace) ?: [];
        return isset($values[$id]) ? TRUE : FALSE;
      case static::STORAGE_CUSTOM:
        $result = \Drupal::moduleHandler()
          ->invokeAll('webform_message_custom', [
          'closed',
          $id,
        ]);
        return array_filter($result) ? TRUE : FALSE;
    }
    return FALSE;
  }

  /**
   * Set message closed via User Data, State API, or Custom.
   *
   * @param string $storage
   *   The storage mechanism save message closed.
   * @param string $id
   *   The ID of the message.
   *
   * @see \Drupal\webform\Controller\WebformElementController::close
   */
  public static function setClosed($storage, $id) {
    $account = \Drupal::currentUser();
    $namespace = 'webform.element.message';
    switch ($storage) {
      case static::STORAGE_STATE:

        /** @var \Drupal\Core\State\StateInterface $state */
        $state = \Drupal::service('state');
        $values = $state
          ->get($namespace, []);
        $values[$id] = TRUE;
        $state
          ->set($namespace, $values);
        break;
      case static::STORAGE_USER:

        /** @var \Drupal\user\UserDataInterface $user_data */
        $user_data = \Drupal::service('user.data');
        $values = $user_data
          ->get('webform', $account
          ->id(), $namespace) ?: [];
        $values[$id] = TRUE;
        $user_data
          ->set('webform', $account
          ->id(), $namespace, $values);
        break;
      case static::STORAGE_CUSTOM:
        \Drupal::moduleHandler()
          ->invokeAll('webform_message_custom', [
          'close',
          $id,
        ]);
        break;
    }
  }

  /**
   * Reset message closed via User Data, State API, or Custom.
   *
   * @param string $storage
   *   The storage mechanism save message closed.
   * @param string $id
   *   The ID of the message.
   *
   * @see \Drupal\webform\Controller\WebformElementController::close
   */
  public static function resetClosed($storage, $id) {
    $account = \Drupal::currentUser();
    $namespace = 'webform.element.message';
    switch ($storage) {
      case static::STORAGE_STATE:

        /** @var \Drupal\Core\State\StateInterface $state */
        $state = \Drupal::service('state');
        $values = $state
          ->get($namespace, []);
        unset($values[$id]);
        $state
          ->set($namespace, $values);
        break;
      case static::STORAGE_USER:

        /** @var \Drupal\user\UserDataInterface $user_data */
        $user_data = \Drupal::service('user.data');
        $values = $user_data
          ->get('webform', $account
          ->id(), $namespace) ?: [];
        unset($values[$id]);
        $user_data
          ->set('webform', $account
          ->id(), $namespace, $values);
        break;
      case static::STORAGE_CUSTOM:
        \Drupal::moduleHandler()
          ->invokeAll('webform_message_custom', [
          'reset',
          $id,
        ]);
        break;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 98
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.
WebformMessage::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
WebformMessage::isClosed public static function Is message closed via User Data, State API, or Custom.
WebformMessage::preRenderWebformMessage public static function Create status message for rendering.
WebformMessage::resetClosed public static function Reset message closed via User Data, State API, or Custom.
WebformMessage::setClosed public static function Set message closed via User Data, State API, or Custom.
WebformMessage::STORAGE_CUSTOM constant Storage custom.
WebformMessage::STORAGE_LOCAL constant Storage local.
WebformMessage::STORAGE_NONE constant Storage none.
WebformMessage::STORAGE_SESSION constant Storage session.
WebformMessage::STORAGE_STATE constant Storage state (API).
WebformMessage::STORAGE_USER constant Storage user (data).