class YamlFormTableSort in YAML Form 8
Same name in this branch
- 8 src/Element/YamlFormTableSort.php \Drupal\yamlform\Element\YamlFormTableSort
- 8 src/Plugin/YamlFormElement/YamlFormTableSort.php \Drupal\yamlform\Plugin\YamlFormElement\YamlFormTableSort
Provides a form element for a sortable table element.
Plugin annotation
@FormElement("yamlform_table_sort");
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\Core\Render\Element\Table
- class \Drupal\yamlform\Element\YamlFormTableSort
- class \Drupal\Core\Render\Element\Table
- 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 YamlFormTableSort
File
- src/
Element/ YamlFormTableSort.php, line 15
Namespace
Drupal\yamlform\ElementView source
class YamlFormTableSort extends Table {
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#js_select' => TRUE,
'#responsive' => TRUE,
'#sticky' => FALSE,
'#pre_render' => [
[
$class,
'preRenderTable',
],
[
$class,
'preRenderYamlFormTableSort',
],
],
'#process' => [
[
$class,
'processYamlFormTableSort',
],
],
'#options' => [],
'#empty' => '',
'#theme' => 'table__table_sort',
];
}
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if ($input === FALSE) {
$value = [];
$element += [
'#default_value' => [],
];
foreach ($element['#default_value'] as $key => $flag) {
if ($flag) {
$value[$key] = $key;
}
}
return $value;
}
else {
if (is_array($input)) {
uasort($input, [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
$values = [];
foreach ($input as $key => $item) {
if (!empty($item['value'])) {
$values[$item['value']] = $item['value'];
}
}
return $values;
}
else {
return [];
}
}
}
/**
* Prepares a 'yamlform_table_sort' #type element for rendering.
*
* @return array
* The processed element.
*/
public static function preRenderYamlFormTableSort($element) {
$rows = [];
$header = $element['#header'];
if (!empty($element['#options'])) {
// Generate a table row for each selectable item in #options.
foreach (Element::children($element) as $key) {
$row = [];
$row['data'] = [];
// Set the row to be draggable.
$row['class'] = [
'draggable',
];
if (isset($element['#options'][$key]['#attributes'])) {
$row += $element['#options'][$key]['#attributes'];
}
// As table.html.twig only maps header and row columns by order, create
// the correct order by iterating over the header fields.
foreach ($element['#header'] as $fieldname => $title) {
// A row cell can span over multiple headers, which means less row
// cells than headers could be present.
if (isset($element['#options'][$key][$fieldname])) {
// A header can span over multiple cells and in this case the cells
// are passed in an array. The order of this array determines the
// order in which they are added.
if (is_array($element['#options'][$key][$fieldname]) && !isset($element['#options'][$key][$fieldname]['data'])) {
foreach ($element['#options'][$key][$fieldname] as $cell) {
$row['data'][] = $cell;
}
}
else {
$row['data'][] = $element['#options'][$key][$fieldname];
}
}
}
// Render the weight and hidden value element.
$weight = [
$element[$key]['weight'],
$element[$key]['value'],
];
$row['data'][] = \Drupal::service('renderer')
->render($weight);
$rows[] = $row;
}
}
// Append weight to header.
$header[] = t('Weight');
// Set header and rows.
$element['#header'] = $header;
$element['#rows'] = $rows;
// Attach table sort.
$element['#attributes']['class'][] = 'js-table-sort';
$element['#attributes']['class'][] = 'table-sort';
return $element;
}
/**
* Creates checkbox and weights to populate a 'yamlform_table_order' table.
*
* @param array $element
* An associative array containing the properties and children of the
* yamlform_table_order element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $complete_form
* The complete form structure.
*
* @return array
* The processed element.
*/
public static function processYamlFormTableSort(&$element, FormStateInterface $form_state, &$complete_form) {
$value = is_array($element['#value']) ? $element['#value'] : [];
// Add validate callback that extracts the associative array of options.
if (isset($element['#element_validate'])) {
array_unshift($element['#element_validate'], [
get_called_class(),
'validateYamlFormTableSelectOrder',
]);
}
else {
$element['#element_validate'][] = [
get_called_class(),
'validateYamlFormTableSelectOrder',
];
}
$element['#tree'] = TRUE;
if (count($element['#options']) > 0) {
if (!isset($element['#default_value']) || $element['#default_value'] === 0) {
$element['#default_value'] = [];
}
// Place checked options first.
$options = [];
foreach ($value as $checked_option_key) {
if (isset($element['#options'][$checked_option_key])) {
$options[$checked_option_key] = $element['#options'][$checked_option_key];
unset($element['#options'][$checked_option_key]);
}
}
$options += $element['#options'];
$element['#options'] = $options;
// Set delta and default weight.
$delta = count($element['#options']);
$weight = 0;
foreach ($element['#options'] as $key => $choice) {
// Do not overwrite manually created children.
if (!isset($element[$key])) {
$weight_title = '';
if (isset($element['#options'][$key]['title']) && is_array($element['#options'][$key]['title'])) {
if (!empty($element['#options'][$key]['title']['data']['#title'])) {
$weight_title = new TranslatableMarkup('Weight for @title', [
'@title' => $element['#options'][$key]['title']['data']['#title'],
]);
}
}
$element[$key]['value'] = [
'#type' => 'hidden',
'#value' => $key,
];
$element[$key]['weight'] = [
'#type' => 'weight',
'#title' => $weight_title,
'#title_display' => 'invisible',
'#delta' => $delta,
'#default_value' => $weight++,
'#attributes' => [
'class' => [
'table-sort-weight',
],
],
];
}
}
}
else {
$element['#value'] = [];
}
// Enable tabledrag.
$element['#tabledrag'] = [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'table-sort-weight',
],
];
return $element;
}
/**
* Validates yamlform_table_other.
*/
public static function validateYamlFormTableSelectOrder(&$element, FormStateInterface $form_state, &$complete_form) {
// Get and sort checked values.
$checked_values = [];
foreach (Element::children($element) as $key) {
if ($element[$key]['value']['#value']) {
$checked_values[] = [
'value' => $element[$key]['value']['#value'],
'weight' => $element[$key]['weight']['#value'],
];
}
}
uasort($checked_values, [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
// Set values.
$values = [];
foreach ($checked_values as $item) {
$values[$item['value']] = $item['value'];
}
// Clear the element's value by setting it to NULL.
$form_state
->setValueForElement($element, NULL);
// Now, set the values as the element's value.
$form_state
->setValueForElement($element, $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 | |
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. | 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. | |
Table:: |
public static | function | #pre_render callback to transform children of an element of #type 'table'. | |
Table:: |
public static | function | #process callback for #type 'table' to add tableselect support. | |
Table:: |
public static | function | #element_validate callback for #type 'table'. | |
YamlFormTableSort:: |
public | function |
Returns the element properties for this element. Overrides Table:: |
|
YamlFormTableSort:: |
public static | function | Prepares a 'yamlform_table_sort' #type element for rendering. | |
YamlFormTableSort:: |
public static | function | Creates checkbox and weights to populate a 'yamlform_table_order' table. | |
YamlFormTableSort:: |
public static | function | Validates yamlform_table_other. | |
YamlFormTableSort:: |
public static | function |
Determines how user input is mapped to an element's #value property. Overrides Table:: |