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;
}
doc_Drupal
2016-10-29 09:56:37
Comments
Leave a Comment

Please login to continue.