public static function Weight::processWeight

public static Weight::processWeight(&$element, FormStateInterface $form_state, &$complete_form)

Expands a weight element into a select element.

File

core/lib/Drupal/Core/Render/Element/Weight.php, line 50

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');
  }
  // Otherwise, use a text field.
  else {
    $element['#type'] = 'number';
    // Use a field big enough to fit most weights.
    $element['#size'] = 10;
    $element += $element_info_manager->getInfo('number');
  }

  return $element;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!Element!Weight.php/function/Weight::processWeight/8.1.x