You are here

public static function WebformHeight::processWebformHeight in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Element/WebformHeight.php \Drupal\webform\Element\WebformHeight::processWebformHeight()

Display feet and inches for height element.

File

src/Element/WebformHeight.php, line 80

Class

WebformHeight
Provides a webform height element.

Namespace

Drupal\webform\Element

Code

public static function processWebformHeight(&$element, FormStateInterface $form_state, &$complete_form) {
  switch ($element['#height_format']) {
    case static::HEIGHT_SYMBOL:
      $feet_plural = '″';
      $feet_singular = '″';
      $inches_plural = '′';
      $inches_singular = '′';
      break;
    case static::HEIGHT_ABBREVIATE:
      $feet_plural = t('ft', [], [
        'context' => 'Imperial height unit abbreviate',
      ]);
      $feet_singular = t('ft', [], [
        'context' => 'Imperial height unit abbreviate',
      ]);
      $inches_plural = t('in', [], [
        'context' => 'Imperial height unit abbreviate',
      ]);
      $inches_singular = t('in', [], [
        'context' => 'Imperial height unit abbreviate',
      ]);
      break;
    default:
      $feet_plural = t('feet', [], [
        'context' => 'Imperial height unit',
      ]);
      $feet_singular = t('foot', [], [
        'context' => 'Imperial height unit',
      ]);
      $inches_plural = t('inches', [], [
        'context' => 'Imperial height unit',
      ]);
      $inches_singular = t('inch', [], [
        'context' => 'Imperial height unit',
      ]);
      break;
  }
  $select_element_defaults = [
    '#type' => 'select',
    '#empty_option' => '',
  ];
  $element['#tree'] = TRUE;

  // Feet options.
  $feet_range = range($element['#feet__min'], $element['#feet__max']);
  $feet_options = array_combine($feet_range, $feet_range);

  // Inches options.
  $inches_step = $element['#inches__step'];
  $inches_range = range(0, 11, $inches_step);
  if ($inches_step !== 1 && floor($inches_step) !== $inches_step) {
    $decimals = strlen(substr(strrchr($inches_step, '.'), 1));
    $inches_range = array_map(function ($number) use ($decimals) {
      return number_format($number, $decimals);
    }, $inches_range);
  }
  $inches_options = array_combine($inches_range, $inches_range);

  // Container.
  $element['container'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'form--inline',
      ],
    ],
  ];
  $t_args = [
    '@title' => $element['#title'],
    '@unit' => t('Feet', [], [
      'context' => 'Imperial height unit',
    ]),
  ];
  $element['container']['feet'] = [
    '#title' => t('@title: @unit', $t_args),
    '#title_display' => 'invisible',
    '#required' => $element['#required'],
    '#error_no_message' => TRUE,
    '#default_value' => $element['#default_value']['feet'],
    '#parents' => array_merge($element['#parents'], [
      'feet',
    ]),
  ];
  $t_args = [
    '@title' => $element['#title'],
    '@unit' => t('Inches', [], [
      'context' => 'Imperial height unit',
    ]),
  ];
  $element['container']['inches'] = [
    '#title' => t('@title: @unit', $t_args),
    '#title_display' => 'invisible',
    '#field_prefix' => ' ',
    '#required' => $element['#required'],
    '#error_no_message' => TRUE,
    '#default_value' => $element['#default_value']['inches'],
    '#parents' => array_merge($element['#parents'], [
      'inches',
    ]),
  ];
  switch ($element['#height_type']) {
    case 'select':
      $element['container']['feet'] += $select_element_defaults + [
        '#field_suffix' => $feet_plural,
        '#options' => $feet_options,
      ];
      $element['container']['inches'] += $select_element_defaults + [
        '#field_suffix' => $inches_plural,
        '#options' => $inches_options,
      ];
      break;
    case 'select_suffix':
      foreach ($feet_options as $option_value => $option_text) {
        $feet_options[$option_value] .= ' ' . ($option_value === 1 ? $feet_singular : $feet_plural);
      }
      foreach ($inches_options as $option_value => $option_text) {
        $inches_options[$option_value] .= ' ' . ($option_value === 1 ? $inches_singular : $inches_plural);
      }
      $element['container']['feet'] += $select_element_defaults + [
        '#options' => $feet_options,
      ];
      $element['container']['inches'] += $select_element_defaults + [
        '#options' => $inches_options,
      ];
      break;
    default:
      $element['container']['feet'] += [
        '#type' => 'number',
        '#field_suffix' => $feet_plural,
        '#min' => $element['#feet__min'],
        '#max' => $element['#feet__max'],
        '#step' => 1,
      ];
      $element['container']['inches'] += [
        '#type' => 'number',
        '#field_suffix' => $inches_plural,
        '#min' => 0,
        '#max' => 11,
        '#step' => $inches_step,
      ];
      break;
  }

  // Apply custom properties to feet and inches elements.
  foreach ($element as $key => $value) {
    if (strpos($key, '__') !== FALSE) {
      list($element_key, $property_key) = explode('__', ltrim($key, '#'));
      if (isset($element['container'][$element_key])) {
        $element['container'][$element_key]["#{$property_key}"] = $value;
      }
    }
  }

  // Initialize the feet and inches elements to allow
  // for webform enhancements.

  /** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
  $element_manager = \Drupal::service('plugin.manager.webform.element');
  $element_manager
    ->buildElement($element['container']['feet'], $complete_form, $form_state);
  $element_manager
    ->buildElement($element['container']['inches'], $complete_form, $form_state);

  // Add validate callback.
  $element += [
    '#element_validate' => [],
  ];
  array_unshift($element['#element_validate'], [
    get_called_class(),
    'validateWebformHeight',
  ]);
  return $element;
}