You are here

protected function OptionsLimitWebformHandler::alterBooleanElement 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::alterBooleanElement()

Alter a boolean element's.

Parameters

array $element: An boolean element.

array $limit: A webform boolean element's limit.

1 call to OptionsLimitWebformHandler::alterBooleanElement()
OptionsLimitWebformHandler::alterElement in modules/webform_options_limit/src/Plugin/WebformHandler/OptionsLimitWebformHandler.php
Alter webform element.

File

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

Class

OptionsLimitWebformHandler
Webform options and boolean (boolean) limit handler.

Namespace

Drupal\webform_options_limit\Plugin\WebformHandler

Code

protected function alterBooleanElement(array &$element, array $limit) {

  // Set message.
  $message_display = $this->configuration['option_message_display'];
  if ($message_display !== WebformOptionsLimitHandlerInterface::MESSAGE_DISPLAY_NONE) {
    $message = $this
      ->getElementLimitStatusMessage($limit['status'], $limit);
    if ($message) {
      switch ($message_display) {
        case WebformOptionsLimitHandlerInterface::MESSAGE_DISPLAY_LABEL:
          $t_args = [
            '@label' => $element['#title'],
            '@message' => $message,
          ];
          $element['#title'] = $this
            ->t('@label @message', $t_args);
          break;
        case WebformOptionsLimitHandlerInterface::MESSAGE_DISPLAY_DESCRIPTION:
          $element += [
            '#description' => '',
          ];
          $element['#description'] .= $element['#description'] ? '<br/>' . $message : $message;
          break;
      }
    }
  }

  // Disable or remove boolean element.
  $element_key = $this->configuration['element_key'];
  $webform_submission = $this
    ->getWebformSubmission();
  $boolean_value = (bool) ($webform_submission
    ->getElementOriginalData($element_key) ?: FALSE);
  if ($limit['status'] === WebformOptionsLimitHandlerInterface::LIMIT_STATUS_NONE && !$boolean_value) {
    switch ($this->configuration['option_none_action']) {
      case WebformOptionsLimitHandlerInterface::LIMIT_ACTION_DISABLE:
        $element['#disabled'] = TRUE;
        break;
      case WebformOptionsLimitHandlerInterface::LIMIT_ACTION_REMOVE:
        $element['#access'] = FALSE;
        break;
    }

    // Display limit reached message.
    $this
      ->setElementLimitReachedMessage($element);
  }
}