View source
<?php
namespace Drupal\markdown\Traits;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
trait FormTrait {
protected static $tokenModuleExists;
public static function addElementState(array &$element, $state, $name, array $conditions, array $parents = NULL) {
if (!isset($parents)) {
$parents = isset($element['#parents']) ? $element['#parents'] : [];
}
if ($selector = static::getElementSelector($name, $parents)) {
if (!isset($element['#states'][$state][$selector])) {
$element['#states'][$state][$selector] = [];
}
$element['#states'][$state][$selector] = NestedArray::mergeDeep($element['#states'][$state][$selector], $conditions);
}
}
public static function addDataAttribute(array &$element, $name, $value) {
static::addDataAttributes($element, [
$name => $value,
]);
}
public static function addDataAttributes(array &$element, array $data) {
$converter = new CamelCaseToSnakeCaseNameConverter();
foreach ($data as $name => $value) {
$name = str_replace('_', '-', $converter
->normalize(preg_replace('/^data-?/', '', $name)));
$element['#attributes']['data-' . $name] = is_string($value) || $value instanceof MarkupInterface ? (string) $value : Json::encode($value);
}
}
public static function createElement(array $element) {
if (isset($element['#attributes']['data']) && is_array($element['#attributes']['data'])) {
static::addDataAttributes($element, $element['#attributes']['data']);
unset($element['#attributes']['data']);
}
return $element;
}
public static function createInlineMessage(array $messages, $weight = -10) {
static $headings;
if (!$headings) {
$headings = [
'error' => t('Error message'),
'info' => t('Info message'),
'status' => t('Status message'),
'warning' => t('Warning message'),
];
}
return [
'#type' => 'item',
'#weight' => $weight,
'#theme' => 'status_messages',
'#message_list' => $messages,
'#status_headings' => $headings,
];
}
public static function getElementSelector($name, array $parents) {
if (strpos($name, ':input[name="') === 0) {
return $name;
}
$parents[] = $name;
$selector = array_shift($parents);
if ($parents) {
$selector .= '[' . implode('][', $parents) . ']';
}
return $selector ? ':input[name="' . $selector . '"]' : '';
}
public static function resetToDefault(array &$element, $name, $defaultValue, FormStateInterface $form_state) {
$selector = static::getElementSelector($name, $form_state
->createParents());
$reset = FormTrait::createElement([
'#type' => 'link',
'#title' => '↩️',
'#url' => Url::fromUserInput('#reset-default-value', [
'external' => TRUE,
]),
'#attributes' => [
'data' => [
'markdownElement' => 'reset',
'markdownId' => 'reset_' . $name,
'markdownTarget' => $selector,
'markdownDefaultValue' => $defaultValue,
],
'title' => t('Reset to default value'),
'style' => 'display: none; text-decoration: none;',
],
]);
$renderer = \Drupal::service('renderer');
$element['#attached']['library'][] = 'markdown/reset';
$element['#title'] = new FormattableMarkup('@title @reset', [
'@title' => $element['#title'],
'@reset' => $renderer
->renderPlain($reset),
]);
}
public static function createTokenBrowser(array $tokenTypes = [], $globalTypes = TRUE, $dialog = TRUE) {
if (!isset(static::$tokenModuleExists)) {
static::$tokenModuleExists = \Drupal::moduleHandler()
->moduleExists('token');
}
if (static::$tokenModuleExists) {
return [
'#type' => 'item',
'#input' => FALSE,
'#theme' => 'token_tree_link',
'#token_types' => $tokenTypes,
'#global_types' => $globalTypes,
'#dialog' => $dialog,
];
}
return [
'#type' => 'item',
'#input' => FALSE,
'#markup' => t('To browse available tokens, install the @token module.', [
'@token' => Link::fromTextAndUrl('Token', Url::fromUri('https://www.drupal.org/project/token', [
'attributes' => [
'target' => '_blank',
],
]))
->toString(),
]),
];
}
}