You are here

public function WebformOptionsCustom::form in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_options_custom/src/Plugin/WebformElement/WebformOptionsCustom.php \Drupal\webform_options_custom\Plugin\WebformElement\WebformOptionsCustom::form()

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 Select::form

File

modules/webform_options_custom/src/Plugin/WebformElement/WebformOptionsCustom.php, line 120

Class

WebformOptionsCustom
Provides a custom options element.

Namespace

Drupal\webform_options_custom\Plugin\WebformElement

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  list(, $webform_options_custom_id) = explode(':', $this
    ->getPluginId());

  /** @var \Drupal\webform_options_custom\WebformOptionsCustomInterface $webform_options_custom */
  $webform_options_custom = $this->entityTypeManager
    ->getStorage('webform_options_custom')
    ->load($webform_options_custom_id);
  $element = $webform_options_custom
    ->getElement();
  $this
    ->setOptions($element);
  $rows = [];
  foreach ($element['#options'] as $value => $text) {
    $rows[] = [
      $value,
      [
        'data' => [
          '#markup' => str_replace(WebformOptionsHelper::DESCRIPTION_DELIMITER, '<br/>', $text),
        ],
      ],
    ];
  }

  // If the custom options are defined, then the options element
  // is not required.
  if ($this
    ->hasProperty('options')) {
    $form['options']['options']['#options_description'] = TRUE;
    if ($this
      ->getEntity()
      ->getOptions() || $this
      ->getEntity()
      ->getTemplateOptions()) {
      $form['options']['options']['#type'] = 'webform_options';
      $form['options']['options']['#required'] = FALSE;
      $form['options']['options_message'] = [
        '#type' => 'webform_message',
        '#message_type' => 'info',
        '#message_message' => $this
          ->t('Below options are used to enhance and also translate the custom options.'),
        '#message_close' => TRUE,
        '#message_storage' => WebformMessage::STORAGE_SESSION,
        '#access' => TRUE,
        '#weight' => -20,
      ];
    }
  }
  if ($rows) {
    $form['options']['custom_options'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Custom options'),
      'table' => [
        '#type' => 'table',
        '#header' => [
          $this
            ->t('Option value'),
          $this
            ->t('Option text / description'),
        ],
        '#rows' => $rows,
        '#access' => TRUE,
      ],
      '#access' => TRUE,
      '#weight' => -20,
    ];
  }

  // Set help to the description.
  $form['options']['#description'] = WebformHtmlEditor::checkMarkup($webform_options_custom
    ->get('help'));

  // Do not allows options disable to be altered since this handled
  // via tooltips.
  unset($form['options']['options_display_container']);
  return $form;
}