public static function validateTable(&
$element, FormStateInterface
$form_state, &
$complete_form) { // Skip this validation if the button to submit the form does not require
// selected table row data.
$triggering_element =
$form_state->
getTriggeringElement();
if (empty($triggering_element['#tableselect'
])) { return;
} if ($element['#multiple'
]) { if (!
is_array($element['#value'
]) || !
count(array_filter($element['#value'
]))) { $form_state->
setError($element,
t('No items selected.'
));
} } elseif (!
isset($element['#value'
]) ||
$element['#value'
] === ''
) { $form_state->
setError($element,
t('No item selected.'
));
} } /**
* #pre_render callback to transform children of an element of #type 'table'.
*
* This function converts sub-elements of an element of #type 'table' to be
* suitable for table.html.twig:
* - The first level of sub-elements are table rows. Only the #attributes
* property is taken into account.
* - The second level of sub-elements is converted into columns for the
* corresponding first-level table row.
*
* Simple example usage:
* @code
* $form['table'] = array(
* '#type' => 'table',
* '#header' => array($this->t('Title'), array('data' => $this->t('Operations'), 'colspan' => '1')),
* // Optionally, to add tableDrag support:
* '#tabledrag' => array(
* array(
* 'action' => 'order',
* 'relationship' => 'sibling',
* 'group' => 'thing-weight',
* ),
* ),
* );
* foreach ($things as $row => $thing) {
* $form['table'][$row]['#weight'] = $thing['weight'];
*
* $form['table'][$row]['title'] = array(
* '#type' => 'textfield',
* '#default_value' => $thing['title'],
* );
*
* // Optionally, to add tableDrag support:
* $form['table'][$row]['#attributes']['class'][] = 'draggable';
* $form['table'][$row]['weight'] = array(
* '#type' => 'textfield',
* '#title' => $this->t('Weight for @title', array('@title' => $thing['title'])),
* '#title_display' => 'invisible',
* '#size' => 4,
* '#default_value' => $thing['weight'],
* '#attributes' => array('class' => array('thing-weight')),
* );
*
* // The amount of link columns should be identical to the 'colspan'
* // attribute in #header above.
* $form['table'][$row]['edit'] = array(
* '#type' => 'link',
* '#title' => $this->t('Edit'),
* '#url' => Url::fromRoute('entity.test_entity.edit_form', ['test_entity' => $row]),
* );
* }
* @endcode
*
* @param array $element
* A structured array containing two sub-levels of elements. Properties used:
* - #tabledrag: The value is a list of $options arrays that are passed to
* drupal_attach_tabledrag(). The HTML ID of the table is added to each
* $options array.
*
* @return array
*
* @see template_preprocess_table()
* @see \Drupal\Core\Render\AttachmentsResponseProcessorInterface::processAttachments()
* @see drupal_attach_tabledrag()
*/