abstract class WebformCompositeBase in Webform 6.x
Same name in this branch
- 6.x src/Element/WebformCompositeBase.php \Drupal\webform\Element\WebformCompositeBase
- 6.x src/Plugin/WebformElement/WebformCompositeBase.php \Drupal\webform\Plugin\WebformElement\WebformCompositeBase
Same name and namespace in other branches
- 8.5 src/Element/WebformCompositeBase.php \Drupal\webform\Element\WebformCompositeBase
Provides an base composite webform element.
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\WebformCompositeBase implements WebformCompositeInterface uses WebformCompositeFormElementTrait
- 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 WebformCompositeBase
4 files declare their use of WebformCompositeBase
- WebformExampleComposite.php in modules/
webform_example_composite/ src/ Element/ WebformExampleComposite.php - WebformOptionsStorage.php in src/
WebformOptionsStorage.php - WebformTestComposite.php in tests/
modules/ webform_test_element/ src/ Element/ WebformTestComposite.php - WebformTestCompositeFile.php in tests/
modules/ webform_test_element/ src/ Element/ WebformTestCompositeFile.php
File
- src/
Element/ WebformCompositeBase.php, line 14
Namespace
Drupal\webform\ElementView source
abstract class WebformCompositeBase extends FormElement implements WebformCompositeInterface {
use WebformCompositeFormElementTrait;
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#access' => TRUE,
'#process' => [
[
$class,
'processWebformComposite',
],
[
$class,
'processAjaxForm',
],
],
'#pre_render' => [
[
$class,
'preRenderWebformCompositeFormElement',
],
],
'#title_display' => 'invisible',
'#required' => FALSE,
'#flexbox' => TRUE,
];
}
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
/** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
$element_manager = \Drupal::service('plugin.manager.webform.element');
$composite_elements = static::getCompositeElements($element);
$composite_elements = WebformElementHelper::getFlattened($composite_elements);
// Get default value for inputs.
$default_value = [];
foreach ($composite_elements as $composite_key => $composite_element) {
$element_plugin = $element_manager
->getElementInstance($composite_element);
if ($element_plugin
->isInput($composite_element)) {
$default_value[$composite_key] = '';
}
}
if ($input === FALSE) {
if (empty($element['#default_value']) || !is_array($element['#default_value'])) {
$element['#default_value'] = [];
}
return $element['#default_value'] + $default_value;
}
return is_array($input) ? $input + $default_value : $default_value;
}
/**
* {@inheritdoc}
*/
public static function preRenderCompositeFormElement($element) {
$element['#theme_wrappers'][] = 'form_element';
$element['#wrapper_attributes']['id'] = $element['#id'] . '--wrapper';
$element['#wrapper_attributes']['class'][] = 'form-composite';
$element['#attributes']['id'] = $element['#id'];
// Add class name to wrapper attributes.
$class_name = str_replace('_', '-', $element['#type']);
static::setAttributes($element, [
'js-' . $class_name,
$class_name,
]);
return $element;
}
/**
* Processes a composite webform element.
*/
public static function processWebformComposite(&$element, FormStateInterface $form_state, &$complete_form) {
if (isset($element['#initialize'])) {
return $element;
}
$element['#initialize'] = TRUE;
$element['#tree'] = TRUE;
$composite_elements = static::initializeCompositeElements($element);
static::processWebformCompositeElementsRecursive($element, $composite_elements, $form_state, $complete_form);
$element += $composite_elements;
// Add validate callback.
$element += [
'#element_validate' => [],
];
array_unshift($element['#element_validate'], [
get_called_class(),
'validateWebformComposite',
]);
if (!empty($element['#flexbox'])) {
$element['#attached']['library'][] = 'webform/webform.element.flexbox';
}
return $element;
}
/**
* Recursively processes a composite's elements.
*/
public static function processWebformCompositeElementsRecursive(&$element, array &$composite_elements, FormStateInterface $form_state, &$complete_form) {
/** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
$element_manager = \Drupal::service('plugin.manager.webform.element');
// Get composite element required/options states from visible/hidden states.
$composite_required_states = WebformElementHelper::getRequiredFromVisibleStates($element);
foreach ($composite_elements as $composite_key => &$composite_element) {
// Make sure the composite key is a string.
$composite_key = (string) $composite_key;
if (!Element::child($composite_key) || !is_array($composite_element)) {
continue;
}
// Set parents.
$composite_element['#parents'] = array_merge($element['#parents'], [
$composite_key,
]);
// If the element's #access is FALSE, apply it to all sub elements.
if ($element['#access'] === FALSE) {
$composite_element['#access'] = FALSE;
}
// Get element plugin and set inputs #default_value.
$element_plugin = $element_manager
->getElementInstance($composite_element);
if ($element_plugin
->isInput($composite_element)) {
// Set #default_value for sub elements.
if (isset($element['#value'][$composite_key])) {
$composite_element['#default_value'] = $element['#value'][$composite_key];
}
}
// Build the webform element.
$element_manager
->buildElement($composite_element, $complete_form, $form_state);
// Custom validate required sub-element because they can be hidden
// via #access or #states.
// @see \Drupal\webform\Element\WebformCompositeBase::validateWebformComposite
if ($composite_required_states && !empty($composite_element['#required'])) {
unset($composite_element['#required']);
$composite_element['#_required'] = TRUE;
if (!isset($composite_element['#states'])) {
$composite_element['#states'] = [];
}
$composite_element['#states'] += $composite_required_states;
}
static::processWebformCompositeElementsRecursive($element, $composite_element, $form_state, $complete_form);
}
}
/**
* Validates a composite element.
*/
public static function validateWebformComposite(&$element, FormStateInterface $form_state, &$complete_form) {
// IMPORTANT: Must get values from the $form_states since sub-elements
// may call $form_state->setValueForElement() via their validation hook.
// @see \Drupal\webform\Element\WebformEmailConfirm::validateWebformEmailConfirm
// @see \Drupal\webform\Element\WebformOtherBase::validateWebformOther
$value = NestedArray::getValue($form_state
->getValues(), $element['#parents']);
// Only validate composite elements that are visible.
if (Element::isVisibleElement($element)) {
// Validate required composite elements.
$composite_elements = static::getCompositeElements($element);
$composite_elements = WebformElementHelper::getFlattened($composite_elements);
foreach ($composite_elements as $composite_key => $composite_element) {
$is_required = !empty($element[$composite_key]['#required']);
$is_empty = isset($value[$composite_key]) && $value[$composite_key] === '';
if ($is_required && $is_empty) {
WebformElementHelper::setRequiredError($element[$composite_key], $form_state);
}
}
}
// Clear empty composites value.
if (empty(array_filter($value))) {
$element['#value'] = NULL;
$form_state
->setValueForElement($element, NULL);
}
}
/****************************************************************************/
// Composite Elements.
/****************************************************************************/
/**
* {@inheritdoc}
*/
public static function getCompositeElements(array $element) {
return [];
}
/**
* {@inheritdoc}
*/
public static function initializeCompositeElements(array &$element) {
$composite_elements = static::getCompositeElements($element);
static::initializeCompositeElementsRecursive($element, $composite_elements);
return $composite_elements;
}
/**
* Initialize a composite's elements recursively.
*
* @param array $element
* A render array for the current element.
* @param array $composite_elements
* A render array containing a composite's elements.
*
* @throws \Exception
* Throws exception when unsupported element type is used with a composite
* element.
*/
protected static function initializeCompositeElementsRecursive(array &$element, array &$composite_elements) {
/** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
$element_manager = \Drupal::service('plugin.manager.webform.element');
foreach ($composite_elements as $composite_key => &$composite_element) {
if (WebformElementHelper::property($composite_key)) {
continue;
}
// Transfer '#{composite_key}_{property}' from main element to composite
// element.
foreach ($element as $property_key => $property_value) {
if (strpos($property_key, '#' . $composite_key . '__') === 0) {
$composite_property_key = str_replace('#' . $composite_key . '__', '#', $property_key);
$composite_element[$composite_property_key] = $property_value;
}
}
// Initialize composite sub-element.
$element_plugin = $element_manager
->getElementInstance($composite_element);
// Make sure to remove any #options references from unsupported elements.
// This prevents "An illegal choice has been detected." error.
// @see FormValidator::performRequiredValidation()
if (isset($composite_element['#options']) && !$element_plugin
->hasProperty('options')) {
unset($composite_element['#options']);
}
// Convert #placeholder to #empty_option for select elements.
if (isset($composite_element['#placeholder']) && $element_plugin
->hasProperty('empty_option')) {
$composite_element['#empty_option'] = $composite_element['#placeholder'];
}
// Apply #select2, #choices, and #chosen to select elements.
if (isset($composite_element['#type']) && strpos($composite_element['#type'], 'select') !== FALSE) {
$select_properties = [
'#select2' => '#select2',
'#choices' => '#choices',
'#chosen' => '#chosen',
];
$composite_element += array_intersect_key($element, $select_properties);
}
if ($element_plugin
->hasMultipleValues($composite_element)) {
throw new \Exception('Multiple elements are not supported within composite elements.');
}
if ($element_plugin
->isComposite()) {
throw new \Exception('Nested composite elements are not supported within composite elements.');
}
$element_plugin
->initialize($composite_element);
static::initializeCompositeElementsRecursive($element, $composite_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. | |
WebformCompositeBase:: |
public static | function |
Get a renderable array of webform elements. Overrides WebformCompositeInterface:: |
11 |
WebformCompositeBase:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
7 |
WebformCompositeBase:: |
public static | function |
Initialize a composite's elements. Overrides WebformCompositeInterface:: |
|
WebformCompositeBase:: |
protected static | function | Initialize a composite's elements recursively. | |
WebformCompositeBase:: |
public static | function | ||
WebformCompositeBase:: |
public static | function | Processes a composite webform element. | 2 |
WebformCompositeBase:: |
public static | function | Recursively processes a composite's elements. | |
WebformCompositeBase:: |
public static | function | Validates a composite element. | |
WebformCompositeBase:: |
public static | function |
Determines how user input is mapped to an element's #value property. Overrides FormElement:: |
|
WebformCompositeFormElementTrait:: |
public static | function | Adds form element theming to an element if its title or description is set. | 1 |