class WebformElementAttributes in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Element/WebformElementAttributes.php \Drupal\webform\Element\WebformElementAttributes
Provides a webform element for element attributes.
Plugin annotation
@FormElement("webform_element_attributes");
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\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\webform\Element\WebformElementAttributes
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of WebformElementAttributes
11 #type uses of WebformElementAttributes
- Range::form in src/
Plugin/ WebformElement/ Range.php - Gets the actual configuration webform array to be built.
- WebformActions::form in src/
Plugin/ WebformElement/ WebformActions.php - Gets the actual configuration webform array to be built.
- WebformElementBase::form in src/
Plugin/ WebformElementBase.php - Gets the actual configuration webform array to be built.
- WebformEntityReferenceLinkFormatter::settingsForm in src/
Plugin/ Field/ FieldFormatter/ WebformEntityReferenceLinkFormatter.php - Returns a form to configure settings for the formatter.
- WebformEntitySettingsConfirmationForm::form in src/
EntitySettings/ WebformEntitySettingsConfirmationForm.php - Gets the actual form array to be built.
File
- src/
Element/ WebformElementAttributes.php, line 16
Namespace
Drupal\webform\ElementView source
class WebformElementAttributes extends FormElement {
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#process' => [
[
$class,
'processWebformElementAttributes',
],
],
'#pre_render' => [
[
$class,
'preRenderWebformElementAttributes',
],
],
'#theme_wrappers' => [
'container',
],
'#classes' => '',
];
}
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
$element += [
'#default_value' => [],
];
$element['#default_value'] += [
'class' => [],
'style' => '',
];
return NULL;
}
/**
* Processes element attributes.
*/
public static function processWebformElementAttributes(&$element, FormStateInterface $form_state, &$complete_form) {
$element['#tree'] = TRUE;
// Determine what type of HTML element the attributes are being applied to.
$type = t('element');
$types = [
preg_quote(t('webform')),
preg_quote(t('link')),
preg_quote(t('button')),
];
if (preg_match('/\\b(' . implode('|', $types) . ')\\b/i', $element['#title'], $match)) {
$type = $match[1];
}
$t_args = [
'@title' => $element['#title'],
'@type' => mb_strtolower($type),
];
// Class.
$element['#classes'] = trim($element['#classes']);
if ($element['#classes']) {
$classes = preg_split('/\\r?\\n/', $element['#classes']);
$element['class'] = [
'#type' => 'webform_select_other',
'#title' => t('@title CSS classes', $t_args),
'#description' => t("Apply classes to the @type. Select 'custom…' to enter custom classes.", $t_args),
'#multiple' => TRUE,
'#options' => [
WebformSelectOther::OTHER_OPTION => t('custom…'),
] + array_combine($classes, $classes),
'#other__placeholder' => t('Enter custom classes…'),
'#other__option_delimiter' => ' ',
'#attributes' => [
'class' => [
'js-' . $element['#id'] . '-attributes-style',
],
],
'#select2' => TRUE,
'#default_value' => $element['#default_value']['class'],
];
WebformElementHelper::process($element['class']);
}
else {
$element['class'] = [
'#type' => 'textfield',
'#title' => t('@title CSS classes', $t_args),
'#description' => t("Apply classes to the @type.", $t_args),
'#default_value' => implode(' ', $element['#default_value']['class']),
];
}
// Custom options.
$element['custom'] = [
'#type' => 'texfield',
'#placeholder' => t('Enter custom classes…'),
'#states' => [
'visible' => [
'select.js-' . $element['#id'] . '-attributes-style' => [
'value' => '_custom_',
],
],
],
'#error_no_message' => TRUE,
'#default_value' => '',
];
// Style.
$element['style'] = [
'#type' => 'textfield',
'#title' => t('@title CSS style', $t_args),
'#description' => t('Apply custom styles to the @type.', $t_args),
'#default_value' => $element['#default_value']['style'],
];
// Attributes.
$attributes = $element['#default_value'];
unset($attributes['class'], $attributes['style']);
$element['attributes'] = [
'#type' => 'webform_codemirror',
'#mode' => 'yaml',
'#title' => t('@title custom attributes (YAML)', $t_args),
'#description' => t('Enter additional attributes to be added the @type.', $t_args),
'#attributes__access' => !\Drupal::moduleHandler()
->moduleExists('webform_ui') || \Drupal::currentUser()
->hasPermission('edit webform source'),
'#default_value' => WebformYaml::encode($attributes),
];
// Apply custom properties. Typically used for descriptions.
foreach ($element as $key => $value) {
if (strpos($key, '__') !== FALSE) {
list($element_key, $property_key) = explode('__', ltrim($key, '#'));
$element[$element_key]["#{$property_key}"] = $value;
}
}
// Add validate callback.
$element += [
'#element_validate' => [],
];
array_unshift($element['#element_validate'], [
get_called_class(),
'validateWebformElementAttributes',
]);
return $element;
}
/**
* Validates element attributes.
*/
public static function validateWebformElementAttributes(&$element, FormStateInterface $form_state, &$complete_form) {
$values = $element['#value'];
$attributes = [];
if ($values['class']) {
if (isset($element['class']['select'])) {
$class = $element['class']['select']['#value'];
$class_other = $element['class']['other']['#value'];
if (isset($class[WebformSelectOther::OTHER_OPTION])) {
unset($class[WebformSelectOther::OTHER_OPTION]);
if ($class_other) {
$class_other = preg_split('/[ ]+/', $class_other);
$class += array_combine($class_other, $class_other);
}
}
if ($class) {
$attributes['class'] = array_values($class);
}
}
else {
$attributes['class'] = preg_split('/[ ]+/', $values['class']);
}
}
if ($values['style']) {
$attributes['style'] = $values['style'];
}
if (!empty($values['attributes'])) {
$attributes += Yaml::decode($values['attributes']);
}
$form_state
->setValueForElement($element['class'], NULL);
$form_state
->setValueForElement($element['style'], NULL);
$form_state
->setValueForElement($element['attributes'], NULL);
$element['#value'] = $attributes;
$form_state
->setValueForElement($element, $attributes);
}
/**
* Prepares a #type 'webform_element_attributes' render element.
*
* @param array $element
* An associative array containing the properties of the element.
*
* @return array
* The $element.
*/
public static function preRenderWebformElementAttributes($element) {
static::setAttributes($element, [
'webform-element-attributes',
]);
return $element;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormElement:: |
public static | function | Adds autocomplete functionality to elements. | |
FormElement:: |
public static | function | #process callback for #pattern form element property. | |
FormElement:: |
public static | function | #element_validate callback for #pattern form element property. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
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:: |
2 |
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. | 98 |
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. | 4 |
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. | |
WebformElementAttributes:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
WebformElementAttributes:: |
public static | function | Prepares a #type 'webform_element_attributes' render element. | |
WebformElementAttributes:: |
public static | function | Processes element attributes. | |
WebformElementAttributes:: |
public static | function | Validates element attributes. | |
WebformElementAttributes:: |
public static | function |
Determines how user input is mapped to an element's #value property. Overrides FormElement:: |