You are here

public function CivicrmOptions::form in Webform CiviCRM Integration 8.5

Gets the actual configuration webform array to be built.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array An associative array contain the element's configuration webform without any default values.

Overrides OptionsBase::form

File

src/Plugin/WebformElement/CivicrmOptions.php, line 55

Class

CivicrmOptions
Provides a 'civicrm_options' element.

Namespace

Drupal\webform_civicrm\Plugin\WebformElement

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  // Get element properties.
  $element_properties = $form_state
    ->getValues() ?: $form_state
    ->get('element_properties');
  $form['extra'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Extra'),
    '#open' => TRUE,
    '#access' => TRUE,
    '#parents' => [
      'properties',
      'extra',
    ],
  ];
  $form['extra']['aslist'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Listbox'),
    '#description' => $this
      ->t('Check this option if you want the select component to be displayed as a select list box instead of radio buttons or checkboxes.'),
    '#access' => TRUE,
    '#default_value' => $element_properties['extra']['aslist'],
    '#parents' => [
      'properties',
      'extra',
      'aslist',
    ],
  ];
  $form['extra']['multiple'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Multiple'),
    '#description' => $this
      ->t('Check this option if multiple options can be selected for the input field.'),
    '#access' => TRUE,
    '#default_value' => $element_properties['extra']['multiple'],
    '#parents' => [
      'properties',
      'extra',
      'multiple',
    ],
  ];

  // Options.
  $form['options'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Element options'),
    '#open' => TRUE,
    '#prefix' => '<div id="webform-civicrm-options-wrapper">',
    '#suffix' => '</div>',
  ];
  $form['options']['empty_option'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Empty option label'),
    '#description' => $this
      ->t('The label to show for the initial option denoting no selection in a select element.'),
    '#default_value' => $element_properties['empty_option'],
  ];
  if ($element_properties['civicrm_live_options']) {
    $live_options_description = t('You cannot control the presentation of live options. They will be loaded from the CiviCRM database every time the form is displayed.');
  }
  else {
    $live_options_description = t('Drag the arrows to re-order these options. Click the "enabled" checkbox to show/remove an item from the form. Set the label as you want it to appear on the form.');
  }
  $form['options']['civicrm_live_options'] = [
    '#type' => 'radios',
    '#options' => [
      t('<strong>Static Options</strong> (fully configurable)'),
      t('<strong>Live Options</strong> (update automatically)'),
    ],
    '#description' => Markup::create('<p>' . $live_options_description . '</p>' . '<p>' . t('Check the "default" box for an option to be selected by default when a user views the form.') . '</p>'),
    '#ajax' => [
      'callback' => [
        static::class,
        'ajaxCallback',
      ],
      'wrapper' => 'webform-civicrm-options-wrapper',
      'progress' => [
        'type' => 'fullscreen',
      ],
    ],
  ];
  $form['options']['options'] = [
    '#type' => 'civicrm_select_options',
    '#civicrm_live_options' => $element_properties['civicrm_live_options'],
    '#default_option' => $element_properties['default_option'],
    '#form_key' => $form_state
      ->get('element_properties')['form_key'],
  ];
  return $form;
}