You are here

public function OptionsLimitWebformHandler::alterElement 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::alterElement()

Alter webform element.

Parameters

array $element: The webform element.

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

array $context: An associative array containing the following key-value pairs:

  • form: The form structure to which elements is being attached.

Overrides WebformHandlerBase::alterElement

See also

\Drupal\webform\WebformSubmissionForm::prepareElements()

hook_webform_element_alter()

File

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

Class

OptionsLimitWebformHandler
Webform options and boolean (boolean) limit handler.

Namespace

Drupal\webform_options_limit\Plugin\WebformHandler

Code

public function alterElement(array &$element, FormStateInterface $form_state, array $context) {
  if (empty($element['#webform_key']) || $element['#webform_key'] !== $this->configuration['element_key']) {
    return;
  }

  // Set webform submission for form object.

  /** @var \Drupal\webform\WebformSubmissionForm $form_object */
  $form_object = $form_state
    ->getFormObject();
  $operation = $form_object
    ->getOperation();

  /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
  $webform_submission = $form_object
    ->getEntity();
  $this
    ->setWebformSubmission($webform_submission);
  if ($this
    ->isOptionsElement()) {

    // Get options limits, reached, and (form) operation.
    $limits = $this
      ->getOptionsLimits();
    $reached = $this
      ->getOptionsReached($limits);

    // Set table select description in header.
    if ($this
      ->isTableSelectElement()) {
      $message_display = $this->configuration['option_message_display'];
      if ($message_display === WebformOptionsLimitHandlerInterface::MESSAGE_DISPLAY_DESCRIPTION) {
        $element['#header']['webform_options_limit'] = $this->configuration['tableselect_header'] ?: '';
      }
    }

    // Cleanup options element default value.
    $this
      ->setOptionsElementDefaultValue($element, $limits, $reached, $operation);

    // Alter options element.
    $this
      ->alterOptionsElement($element, $limits, $reached);
  }
  else {

    // Get boolean limit.
    $limits = $this
      ->getBooleanLimits();
    $limit = reset($limits);

    // Cleanup boolean element default value.
    $this
      ->setBooleanElementDefaultValue($element, $limit, $operation);

    // Alter boolean element.
    $this
      ->alterBooleanElement($element, $limit);
  }

  // Add element validate callback.
  $element['#webform_option_limit_handler_id'] = $this
    ->getHandlerId();
  $element['#element_validate'][] = [
    get_called_class(),
    'validateElement',
  ];
}