You are here

public static function Weight::processWeight in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Render/Element/Weight.php \Drupal\Core\Render\Element\Weight::processWeight()

Expands a weight element into a select element.

File

core/lib/Drupal/Core/Render/Element/Weight.php, line 55
Contains \Drupal\Core\Render\Element\Weight.

Class

Weight
Provides a form element for input of a weight.

Namespace

Drupal\Core\Render\Element

Code

public static function processWeight(&$element, FormStateInterface $form_state, &$complete_form) {
  $element['#is_weight'] = TRUE;
  $element_info_manager = \Drupal::service('element_info');

  // If the number of options is small enough, use a select field.
  $max_elements = \Drupal::config('system.site')
    ->get('weight_select_max');
  if ($element['#delta'] <= $max_elements) {
    $element['#type'] = 'select';
    $weights = array();
    for ($n = -1 * $element['#delta']; $n <= $element['#delta']; $n++) {
      $weights[$n] = $n;
    }
    $element['#options'] = $weights;
    $element += $element_info_manager
      ->getInfo('select');
  }
  else {
    $element['#type'] = 'number';

    // Use a field big enough to fit most weights.
    $element['#size'] = 10;
    $element += $element_info_manager
      ->getInfo('number');
  }
  return $element;
}