class YamlFormMessage in YAML Form 8
Same name in this branch
- 8 src/Element/YamlFormMessage.php \Drupal\yamlform\Element\YamlFormMessage
- 8 src/Plugin/YamlFormElement/YamlFormMessage.php \Drupal\yamlform\Plugin\YamlFormElement\YamlFormMessage
Provides a render element for message.
Plugin annotation
@FormElement("yamlform_message");
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\yamlform\Element\YamlFormMessage
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of YamlFormMessage
3 files declare their use of YamlFormMessage
- YamlFormElementController.php in src/
Controller/ YamlFormElementController.php - YamlFormHelpManager.php in src/
YamlFormHelpManager.php - YamlFormMessage.php in src/
Plugin/ YamlFormElement/ YamlFormMessage.php
9 #type uses of YamlFormMessage
- DateTime::form in src/
Plugin/ YamlFormElement/ DateTime.php - Gets the actual configuration form array to be built.
- DelimitedYamlFormExporter::buildConfigurationForm in src/
Plugin/ YamlFormExporter/ DelimitedYamlFormExporter.php - Form constructor.
- YamlFormAdminSettingsForm::buildForm in src/
Form/ YamlFormAdminSettingsForm.php - Form constructor.
- YamlFormCreditCard::getCompositeElements in src/
Element/ YamlFormCreditCard.php - Get a renderable array of form elements.
- YamlFormElementStates::processYamlFormStates in src/
Element/ YamlFormElementStates.php - Expand an email confirm field into two HTML5 email elements.
File
- src/
Element/ YamlFormMessage.php, line 14
Namespace
Drupal\yamlform\ElementView source
class YamlFormMessage 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';
/**
* {@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,
'preRenderYamlFormMessage',
],
],
'#theme_wrappers' => [
'yamlform_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 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;
}
/****************************************************************************/
// Manage closed functions.
/****************************************************************************/
/**
* Is message closed via User Data or State API.
*
* @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 = 'yamlform.element.message';
switch ($storage) {
case self::STORAGE_STATE:
/** @var \Drupal\Core\State\StateInterface $state */
$state = \Drupal::service('state');
$values = $state
->get($namespace, []);
return isset($values[$id]) ? TRUE : FALSE;
case self::STORAGE_USER:
/** @var \Drupal\user\UserDataInterface $user_data */
$user_data = \Drupal::service('user.data');
$values = $user_data
->get('yamlform', $account
->id(), $namespace) ?: [];
return isset($values[$id]) ? TRUE : FALSE;
}
return FALSE;
}
/**
* Set message closed via User Data or State API.
*
* @param string $storage
* The storage mechanism save message closed.
* @param string $id
* The ID of the message.
*
* @see \Drupal\yamlform\Controller\YamlFormElementController::close
*/
public static function setClosed($storage, $id) {
$account = \Drupal::currentUser();
$namespace = 'yamlform.element.message';
switch ($storage) {
case self::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 self::STORAGE_USER:
/** @var \Drupal\user\UserDataInterface $user_data */
$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);
}
}
/**
* Reset message closed via User Data or State API.
*
* @param string $storage
* The storage mechanism save message closed.
* @param string $id
* The ID of the message.
*
* @see \Drupal\yamlform\Controller\YamlFormElementController::close
*/
public static function resetClosed($storage, $id) {
$account = \Drupal::currentUser();
$namespace = 'yamlform.element.message';
switch ($storage) {
case self::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 self::STORAGE_USER:
/** @var \Drupal\user\UserDataInterface $user_data */
$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);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
RenderElement:: |
public static | function | Adds Ajax information about an element to communicate with JavaScript. | |
RenderElement:: |
public static | function | Adds members of this group as actual elements for rendering. | |
RenderElement:: |
public static | function | Form element processing handler for the #ajax form property. | 1 |
RenderElement:: |
public static | function | Arranges elements into groups. | |
RenderElement:: |
public static | function |
Sets a form element's class attribute. Overrides ElementInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
YamlFormMessage:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
YamlFormMessage:: |
public static | function | Is message closed via User Data or State API. | |
YamlFormMessage:: |
public static | function | Create status message for rendering. | |
YamlFormMessage:: |
public static | function | Reset message closed via User Data or State API. | |
YamlFormMessage:: |
public static | function | Set message closed via User Data or State API. | |
YamlFormMessage:: |
constant | Storage local. | ||
YamlFormMessage:: |
constant | Storage none. | ||
YamlFormMessage:: |
constant | Storage session. | ||
YamlFormMessage:: |
constant | Storage state (API). | ||
YamlFormMessage:: |
constant | Storage user (data). |