You are here

public function OptionsLimitWebformHandler::buildConfigurationForm in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_options_limit/src/Plugin/WebformHandler/OptionsLimitWebformHandler.php \Drupal\webform_options_limit\Plugin\WebformHandler\OptionsLimitWebformHandler::buildConfigurationForm()

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides WebformHandlerBase::buildConfigurationForm

File

modules/webform_options_limit/src/Plugin/WebformHandler/OptionsLimitWebformHandler.php, line 185

Class

OptionsLimitWebformHandler
Webform options and boolean (boolean) limit handler.

Namespace

Drupal\webform_options_limit\Plugin\WebformHandler

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $this
    ->applyFormStateToConfiguration($form_state);

  // Get elements with options.
  $elements_with_options = $this
    ->getElements();

  // Make sure that there an options element available.
  if (empty($elements_with_options)) {
    $form['message'] = [
      '#type' => 'webform_message',
      '#message_type' => 'warning',
      '#message_message' => [
        'message' => [
          '#markup' => $this
            ->t('No options or checkbox elements are available.'),
          '#prefix' => '<p>',
          '#suffix' => '</p>',
        ],
        'link' => [
          '#type' => 'link',
          '#title' => $this
            ->t('Please add a new options or checkbox element.'),
          '#url' => $this
            ->getWebform()
            ->toUrl('edit-form'),
          '#prefix' => '<p>',
          '#suffix' => '</p>',
        ],
      ],
    ];
    return $form;
  }

  // Element settings.
  $form['element_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Element settings'),
    '#open' => TRUE,
  ];
  $form['element_settings']['element_key'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Element'),
    '#options' => $this
      ->getElements(),
    '#default_value' => $this->configuration['element_key'],
    '#required' => TRUE,
    '#empty_option' => empty($this->configuration['element_key']) ? $this
      ->t('- Select -') : NULL,
  ];
  $form['element_settings']['options_container'] = [];
  $element = $this
    ->getElement();
  if ($element) {
    $webform_element = $this
      ->getWebformElement();
    $t_args = [
      '@title' => $webform_element
        ->getAdminLabel($element),
      '@type' => $this
        ->t('option'),
    ];
    if ($this
      ->isOptionsElement()) {
      $element_options = $this
        ->getElementOptions() + [
        WebformOptionsLimitHandlerInterface::DEFAULT_LIMIT => $this
          ->t('Default (Used when option has no limit)'),
      ];
      $form['element_settings']['options_container']['limits'] = [
        '#type' => 'webform_mapping',
        '#title' => $this
          ->t('@title @type limits', $t_args),
        '#description_display' => 'before',
        '#source' => $element_options,
        '#source__title' => $this
          ->t('Options'),
        '#destination__type' => 'number',
        '#destination__min' => 0,
        '#destination__title' => $this
          ->t('Limit'),
        '#destination__description' => NULL,
        '#default_value' => $this->configuration['limits'],
        '#filter' => FALSE,
      ];
    }
    else {
      $form['element_settings']['options_container']['limit'] = [
        '#type' => 'number',
        '#title' => $this
          ->t('@title @type limit', $t_args),
        '#min' => 0,
        '#default_value' => $this->configuration['limit'],
      ];
    }
  }
  else {
    $form['element_settings']['options_container']['limits'] = [
      '#type' => 'value',
      '#value' => [],
    ];
    $form['element_settings']['options_container']['limit'] = [
      '#type' => 'value',
      '#value' => NULL,
    ];
  }
  $this
    ->buildAjaxElement('webform-options-limit', $form['element_settings']['options_container'], $form['element_settings']['element_key']);

  // Limit settings.
  $form['limit_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Limit settings'),
  ];
  $form['limit_settings']['limit_reached_message'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Limit reached message'),
    '#description' => $this
      ->t('This message will be displayed when all limits are reached.') . '<br/><br/>' . $this
      ->t('Leave blank to hide this message.'),
    '#default_value' => $this->configuration['limit_reached_message'],
  ];
  $form['limit_settings']['limit_source_entity'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Apply limits to each source entity'),
    '#description' => $this
      ->t('If checked, limits will be applied to this webform and each source entity individually.'),
    '#return_value' => TRUE,
    '#default_value' => $this->configuration['limit_source_entity'],
  ];
  $form['limit_settings']['limit_user'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Apply limits to per user'),
    '#description' => $this
      ->t("If checked, limits will be applied per submission for authenticated and anonymous users. Anonymous user limits are only tracked by the user's browser sessions. Per user limits work best for authenticated users."),
    '#return_value' => TRUE,
    '#default_value' => $this->configuration['limit_user'],
  ];
  $form['limit_settings']['limit_user_message'] = [
    '#type' => 'webform_message',
    '#message_type' => 'warning',
    '#message_message' => $this
      ->t("Anonymous user limits are only tracked by the user's browser session. It is recommended that limits per user only be used on forms restricted to authenticated users."),
    '#message_close' => TRUE,
    '#message_storage' => WebformMessage::STORAGE_SESSION,
    '#states' => [
      'visible' => [
        ':input[name="settings[limit_user]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  // Option settings.
  $form['option_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Option/checkbox settings'),
  ];
  $form['option_settings']['option_none_action'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Limit reached behavior'),
    '#options' => [
      WebformOptionsLimitHandlerInterface::LIMIT_ACTION_DISABLE => $this
        ->t('Disable the option/checkbox'),
      WebformOptionsLimitHandlerInterface::LIMIT_ACTION_REMOVE => $this
        ->t('Remove the option/checkbox'),
      WebformOptionsLimitHandlerInterface::LIMIT_ACTION_NONE => $this
        ->t('Do not alter the option/checkbox'),
    ],
    '#default_value' => $this->configuration['option_none_action'],
  ];
  $form['option_settings']['option_message_display'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Message display'),
    '#options' => [
      WebformOptionsLimitHandlerInterface::MESSAGE_DISPLAY_LABEL => $this
        ->t("Append message to the option/checkbox's text"),
      WebformOptionsLimitHandlerInterface::MESSAGE_DISPLAY_DESCRIPTION => $this
        ->t("Append message to the option/checkbox's description"),
      WebformOptionsLimitHandlerInterface::MESSAGE_DISPLAY_NONE => $this
        ->t("Do not display a message"),
    ],
    '#default_value' => $this->configuration['option_message_display'],
  ];
  $form['option_settings']['option_message'] = [
    '#type' => 'container',
    '#states' => [
      'visible' => [
        ':input[name="settings[option_message_display]"]' => [
          '!value' => 'none',
        ],
      ],
    ],
  ];
  $form['option_settings']['option_message']['option_multiple_message'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Multiple remaining message'),
    '#description' => $this
      ->t('This message is displayed when the remaining submission is greater than one.') . '<br/><br/>' . $this
      ->t('Leave blank to hide this message.'),
    '#default_value' => $this->configuration['option_multiple_message'],
  ];
  $form['option_settings']['option_message']['option_single_message'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('One remaining message'),
    '#description' => $this
      ->t('This message is displayed when there is only one remaining submission available.') . '<br/><br/>' . $this
      ->t('Leave blank to hide this message.'),
    '#default_value' => $this->configuration['option_single_message'],
  ];
  $form['option_settings']['option_message']['option_none_message'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('None remaining message'),
    '#description' => $this
      ->t('This message is displayed when there are remaining submissions allows.') . '<br/><br/>' . $this
      ->t('Leave blank to hide this message.'),
    '#default_value' => $this->configuration['option_none_message'],
  ];
  $form['option_settings']['option_message']['option_unlimited_message'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Unlimited message'),
    '#description' => $this
      ->t('This message is displayed when there are not submissions limits.') . '<br/><br/>' . $this
      ->t('Leave blank to hide this message.'),
    '#default_value' => $this->configuration['option_unlimited_message'],
  ];
  $form['option_settings']['option_error_message'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Validation error message'),
    '#description' => $this
      ->t('The message is displayed when an element has validation error is submitted.'),
    '#default_value' => $this->configuration['option_error_message'],
    '#required' => TRUE,
  ];
  if ($tableselect_elements = $this
    ->getTableSelectElements()) {
    $tableselect_states = [];
    foreach ($tableselect_elements as $tableselect_element_key) {
      if ($tableselect_states) {
        $tableselect_states[] = 'or';
      }
      $tableselect_states[] = [
        ':input[name="settings[element_key]"]' => [
          'value' => $tableselect_element_key,
        ],
      ];
    }
    $form['option_settings']['tableselect_header'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Table select description header'),
      '#description' => $this
        ->t("The label is displayed in the header for the table select's option limit column."),
      '#default_value' => $this->configuration['tableselect_header'],
      '#states' => [
        'visible' => [
          ':input[name="settings[option_message_display]"]' => [
            'value' => WebformOptionsLimitHandlerInterface::MESSAGE_DISPLAY_DESCRIPTION,
          ],
          $tableselect_states,
        ],
      ],
    ];
  }

  // Placeholder help.
  $form['placeholder_help'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Placeholder help'),
    'description' => [
      '#markup' => $this
        ->t('The following placeholders can be used:'),
    ],
    'items' => [
      '#theme' => 'item_list',
      '#items' => [
        $this
          ->t('@limit - The total number of submissions allowed for the option/checkbox.'),
        $this
          ->t('@total - The current number of submissions for the option/checkbox.'),
        $this
          ->t('@remaining - The remaining number of submissions for the option/checkbox.'),
        $this
          ->t("@label - The element option/checkbox's label."),
        $this
          ->t("@name - The element's title."),
      ],
    ],
  ];
  return $this
    ->setSettingsParents($form);
}