public static function Zone::processZone in Address 8
Processes the zone form element.
Parameters
array $element: The form element to process.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
array $complete_form: The complete form structure.
Return value
array The processed element.
Throws
\InvalidArgumentException Thrown when the #default_value is malformed.
File
- src/
Element/ Zone.php, line 112
Class
- Zone
- Provides a zone form element.
Namespace
Drupal\address\ElementCode
public static function processZone(array &$element, FormStateInterface $form_state, array &$complete_form) {
if (!empty($element['#default_value']['territories']) && !is_array($element['#default_value']['territories'])) {
throw new \InvalidArgumentException('The #default_value "territories" property must be an array.');
}
$id_prefix = implode('-', $element['#parents']);
$wrapper_id = Html::getUniqueId($id_prefix . '-ajax-wrapper');
$button_id_prefix = implode('_', $element['#parents']);
$value = $element['#value'];
$element_state = self::getElementState($element['#parents'], $form_state);
if (!isset($element_state['territories'])) {
// Default to a single empty row if no other value was provided.
$element_state['territories'] = $value['territories'];
$element_state['territories'] = $element_state['territories'] ?: [
NULL,
];
self::setElementState($element['#parents'], $form_state, $element_state);
}
$element = [
'#tree' => TRUE,
'#prefix' => '<div id="' . $wrapper_id . '">',
'#suffix' => '</div>',
] + $element;
$element['label'] = [
'#type' => 'textfield',
'#title' => t('Zone label'),
'#default_value' => $value['label'],
'#access' => $element['#show_label_field'],
];
$element['territories'] = [
'#type' => 'table',
'#header' => [
t('Territory'),
t('Operations'),
],
'#input' => FALSE,
];
foreach ($element_state['territories'] as $index => $territory) {
$territory_form =& $element['territories'][$index];
$territory_form['territory'] = [
'#type' => 'address_zone_territory',
'#default_value' => $territory,
'#available_countries' => $element['#available_countries'],
'#required' => $element['#required'],
// Remove the 'territory' level from form state values.
'#parents' => array_merge($element['#parents'], [
'territories',
$index,
]),
];
$territory_form['remove'] = [
'#type' => 'submit',
'#name' => $button_id_prefix . '_remove_territory' . $index,
'#value' => t('Remove'),
'#limit_validation_errors' => [],
'#submit' => [
[
get_called_class(),
'removeTerritorySubmit',
],
],
'#territory_index' => $index,
'#ajax' => [
'callback' => [
get_called_class(),
'ajaxRefresh',
],
'wrapper' => $wrapper_id,
],
];
}
$element['territories'][] = [
'add_territory' => [
'#type' => 'submit',
'#name' => $button_id_prefix . '_add_territory',
'#value' => t('Add territory'),
'#submit' => [
[
get_called_class(),
'addTerritorySubmit',
],
],
'#limit_validation_errors' => [],
'#ajax' => [
'callback' => [
get_called_class(),
'ajaxRefresh',
],
'wrapper' => $wrapper_id,
],
],
];
return $element;
}