You are here

class CivicrmSelectOptions in Webform CiviCRM Integration 8.5

Plugin annotation

@FormElement("civicrm_select_options");

Hierarchy

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\Element
View 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

Namesort descending Modifiers Type Description Overrides
CivicrmSelectOptions::getFieldOptions protected static function
CivicrmSelectOptions::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
CivicrmSelectOptions::processSelectOptions public static function
CivicrmSelectOptions::validateSelectOptions public static function Validates webform options element.
CivicrmSelectOptions::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides FormElement::valueCallback
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormElement::processAutocomplete public static function Adds autocomplete functionality to elements.
FormElement::processPattern public static function #process callback for #pattern form element property.
FormElement::validatePattern public static function #element_validate callback for #pattern form element property.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.