View source
<?php
namespace Drupal\yamlform\Element;
use Drupal\Core\Render\Element\RenderElement;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
class YamlFormMessage extends RenderElement {
const STORAGE_NONE = '';
const STORAGE_LOCAL = 'local';
const STORAGE_SESSION = 'session';
const STORAGE_USER = 'user';
const STORAGE_STATE = 'state';
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,
'preRenderYamlFormMessage',
],
],
'#theme_wrappers' => [
'yamlform_message',
],
];
}
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';
if (\Drupal::currentUser()
->isAnonymous() && in_array($message_storage, [
self::STORAGE_USER,
self::STORAGE_STATE,
])) {
$message_storage = '';
}
$messages = [];
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,
];
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;
}
}
}
$messages[] = !is_array($message_message) ? [
'#markup' => $message_message,
] : $message_message;
foreach (Element::children($element) as $key) {
$messages[] = $element[$key];
unset($element[$key]);
}
$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;
}
public static function isClosed($storage, $id) {
$account = \Drupal::currentUser();
$namespace = 'yamlform.element.message';
switch ($storage) {
case self::STORAGE_STATE:
$state = \Drupal::service('state');
$values = $state
->get($namespace, []);
return isset($values[$id]) ? TRUE : FALSE;
case self::STORAGE_USER:
$user_data = \Drupal::service('user.data');
$values = $user_data
->get('yamlform', $account
->id(), $namespace) ?: [];
return isset($values[$id]) ? TRUE : FALSE;
}
return FALSE;
}
public static function setClosed($storage, $id) {
$account = \Drupal::currentUser();
$namespace = 'yamlform.element.message';
switch ($storage) {
case self::STORAGE_STATE:
$state = \Drupal::service('state');
$values = $state
->get($namespace, []);
$values[$id] = TRUE;
$state
->set($namespace, $values);
break;
case self::STORAGE_USER:
$user_data = \Drupal::service('user.data');
$values = $user_data
->get('yamlform', $account
->id(), $namespace) ?: [];
$values[$id] = TRUE;
$user_data
->set('yamlform', $account
->id(), $namespace, $values);
}
}
public static function resetClosed($storage, $id) {
$account = \Drupal::currentUser();
$namespace = 'yamlform.element.message';
switch ($storage) {
case self::STORAGE_STATE:
$state = \Drupal::service('state');
$values = $state
->get($namespace, []);
unset($values[$id]);
$state
->set($namespace, $values);
break;
case self::STORAGE_USER:
$user_data = \Drupal::service('user.data');
$values = $user_data
->get('yamlform', $account
->id(), $namespace) ?: [];
unset($values[$id]);
$user_data
->set('yamlform', $account
->id(), $namespace, $values);
}
}
}