Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Render/Element/Weight.php
- 9 core/lib/Drupal/Core/Render/Element/Weight.php
Namespace
Drupal\Core\Render\Element
File
core/lib/Drupal/Core/Render/Element/Weight.php
View source
<?php
namespace Drupal\Core\Render\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Attribute\FormElement;
class Weight extends FormElementBase {
public function getInfo() {
$class = static::class;
return [
'#input' => TRUE,
'#delta' => 10,
'#default_value' => 0,
'#process' => [
[
$class,
'processWeight',
],
[
$class,
'processAjaxForm',
],
],
];
}
public static function processWeight(&$element, FormStateInterface $form_state, &$complete_form) {
$type = $element['#delta'] <= \Drupal::config('system.site')
->get('weight_select_max') ? 'select' : 'number';
$element = array_merge($element, \Drupal::service('element_info')
->getInfo($type));
$element['#is_weight'] = TRUE;
if ($type === 'select') {
$weights = [];
for ($n = -1 * $element['#delta']; $n <= $element['#delta']; $n++) {
$weights[$n] = $n;
}
$default_value = (int) $element['#default_value'];
if (!isset($weights[$default_value])) {
$weights[$default_value] = $default_value;
ksort($weights);
}
$element['#options'] = $weights;
}
else {
$element['#size'] = 10;
}
return $element;
}
}
Classes