class CivicrmSelectOptions in Webform CiviCRM Integration 8.5
Plugin annotation
@FormElement("civicrm_select_options");
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_civicrm\Element\CivicrmSelectOptions
- 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 CivicrmSelectOptions
1 #type use of CivicrmSelectOptions
- CivicrmOptions::form in src/
Plugin/ WebformElement/ CivicrmOptions.php - Gets the actual configuration webform array to be built.
File
- src/
Element/ CivicrmSelectOptions.php, line 11
Namespace
Drupal\webform_civicrm\ElementView source
class CivicrmSelectOptions extends FormElement {
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#label' => t('option'),
'#labels' => t('options'),
'#civicrm_live_options' => TRUE,
'#default_option' => NULL,
'#form_key' => NULL,
'#process' => [
[
$class,
'processSelectOptions',
],
],
'#theme_wrappers' => [
'form_element',
],
];
}
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if ($input === FALSE) {
if (!isset($element['#default_value'])) {
return [];
}
return $element['#default_value'];
// return static::convertOptionsToValues($options, $element['#options_description']);
}
elseif (is_array($input) && isset($input['options'])) {
return $input['options'];
}
else {
return $element['#default_value'];
}
}
protected static function getFieldOptions($form_key, $data = []) {
\Drupal::getContainer()
->get('civicrm')
->initialize();
$field_options = \Drupal::service('webform_civicrm.field_options');
return $field_options
->get([
'form_key' => $form_key,
], 'create', $data);
}
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;
}
/**
* Validates webform options element.
*/
public static function validateSelectOptions(&$element, FormStateInterface $form_state, &$complete_form) {
if ($element['#civicrm_live_options']) {
$webform = $form_state
->getFormObject()
->getWebform();
$data = $webform
->getHandler('webform_civicrm')
->getConfiguration()['settings']['data'] ?? [];
$options_value = self::getFieldOptions($element['#form_key'], $data);
}
else {
$raw_values = $form_state
->getValue($element['options']['#parents']);
uasort($raw_values, '\\Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
$options_value = [];
foreach ($raw_values as $raw_key => $raw_value) {
if (!empty($raw_value['enabled'])) {
$new_key = str_replace('civicrm_option_', '', $raw_key);
$options_value[$new_key] = $raw_value['label'];
}
}
}
$element['#default_option'] = $form_state
->getValue([
'properties',
'options',
'default',
]);
$element['#value'] = $options_value;
$form_state
->setValueForElement($element, $options_value);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CivicrmSelectOptions:: |
protected static | function | ||
CivicrmSelectOptions:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
CivicrmSelectOptions:: |
public static | function | ||
CivicrmSelectOptions:: |
public static | function | Validates webform options element. | |
CivicrmSelectOptions:: |
public static | function |
Determines how user input is mapped to an element's #value property. Overrides FormElement:: |
|
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. |