You are here

public static function YamlFormMessage::preRenderYamlFormMessage in YAML Form 8

Create status message for rendering.

Parameters

array $element: An associative array containing the properties and children of the element.

Return value

array The modified element with status message.

File

src/Element/YamlFormMessage.php, line 71

Class

YamlFormMessage
Provides a render element for message.

Namespace

Drupal\yamlform\Element

Code

public static function preRenderYamlFormMessage($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'][] = 'yamlform-message';
  $element['#attributes']['class'][] = 'js-yamlform-message';

  // Ignore 'user' and 'state' storage is current user is anonymous.
  if (\Drupal::currentUser()
    ->isAnonymous() && in_array($message_storage, [
    self::STORAGE_USER,
    self::STORAGE_STATE,
  ])) {
    $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'][] = 'yamlform-message--close';
    $element['#attributes']['class'][] = 'js-yamlform-message--close';
    $close_attributes = [
      'aria-label' => t('close'),
      'class' => [
        'js-yamlform-message__link',
        'yamlform-message__link',
      ],
    ];
    if (in_array($message_storage, [
      'user',
      'state',
    ])) {
      $close_url = Url::fromRoute('yamlform.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-yamlform-message--close-storage';
      if (self::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'][] = 'yamlform/yamlform.element.message';
  return $element;
}