public static function CivicrmSelectOptions::processSelectOptions in Webform CiviCRM Integration 8.5
File
- src/
Element/ CivicrmSelectOptions.php, line 55
Class
- CivicrmSelectOptions
- Plugin annotation @FormElement("civicrm_select_options");
Namespace
Drupal\webform_civicrm\ElementCode
public static function processSelectOptions(&$element, FormStateInterface $form_state, &$complete_form) {
$element['#tree'] = TRUE;
// Add validate callback that extracts the associative array of options.
$element += [
'#element_validate' => [],
];
array_unshift($element['#element_validate'], [
get_called_class(),
'validateSelectOptions',
]);
$element['options'] = [
'#type' => 'table',
'#tableselect' => TRUE,
'#header' => [
'item' => [
'data' => [
'#markup' => 'Item',
],
],
'enabled' => [
'data' => [
'#markup' => 'Enabled',
'#access' => !$element['#civicrm_live_options'],
],
],
'label' => [
'data' => [
'#markup' => 'Label',
'#access' => !$element['#civicrm_live_options'],
],
],
'default' => [
'data' => [
'#markup' => 'Default',
],
],
'weight' => [
'data' => [
'#markup' => 'Weight',
'#access' => !$element['#civicrm_live_options'],
],
],
],
'#empty' => 'Nothing',
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'weight',
],
],
'#value_callback' => [
get_called_class(),
'valueCallback',
],
];
if ($element['#civicrm_live_options']) {
$element['options']['#tabledrag'] = [];
$element['options']['#tableselect'] = FALSE;
}
$current_options = $element['#default_value'];
$weight = 0;
$webform = $form_state
->getFormObject()
->getWebform();
$data = $webform
->getHandler('webform_civicrm')
->getConfiguration()['settings']['data'] ?? [];
$field_options = static::getFieldOptions($element['#form_key'], $data);
// Sort the field options by the current options.
if (!$element['#civicrm_live_options']) {
uasort($field_options, function ($a, $b) use ($current_options) {
$current_options = array_flip($current_options);
$weight_values = array_flip(array_values(array_flip($current_options)));
if (!isset($current_options[$b]) && isset($current_options[$a])) {
return -1;
}
if (!isset($current_options[$a]) && isset($current_options[$b])) {
return 1;
}
$a_weight = $weight_values[$a] ?? 0;
$b_weight = $weight_values[$b] ?? 0;
if ($a_weight == $b_weight) {
return 0;
}
return $a_weight < $b_weight ? -1 : 1;
});
}
foreach ($field_options as $key => $option) {
$row_key = 'civicrm_option_' . $key;
$element['options'][$row_key]['#attributes']['class'][] = 'draggable';
$element['options'][$row_key]['#weight'] = $weight;
$element['options'][$row_key]['item'] = [
'#plain_text' => $option,
];
$element['options'][$row_key]['enabled'] = [
'#type' => 'checkbox',
'#title' => t('Enable @item', [
'@item' => $option,
]),
'#title_display' => 'invisible',
'#default_value' => !empty($current_options[$key]),
'#access' => !$element['#civicrm_live_options'],
];
$element['options'][$row_key]['label'] = [
'#type' => 'textfield',
'#title' => t('Label for @item', [
'@item' => $option,
]),
'#title_display' => 'invisible',
'#default_value' => !empty($current_options[$key]) ? $current_options[$key] : $option,
'#access' => !$element['#civicrm_live_options'],
];
$element['options'][$row_key]['default_option'] = [
'#type' => 'radio',
'#title' => t('Mark @item as the default value', [
'@item' => $option,
]),
'#title_display' => 'invisible',
'#default_value' => $element['#default_option'] == $key ? $key : 0,
'#parents' => array_merge($element['#parents'], [
'default',
]),
'#return_value' => $key,
];
$element['options'][$row_key]['weight'] = [
'#type' => 'weight',
'#title' => t('Weight for @option', [
'@option' => $option,
]),
'#title_display' => 'invisible',
// @todo support these values.
'#default_value' => $weight,
'#attributes' => [
'class' => [
'weight',
],
],
'#access' => !$element['#civicrm_live_options'],
];
$weight++;
}
$element['#attached']['library'][] = 'webform_civicrm/civicrmoptions';
return $element;
}