public static function MediaLibraryWidget::setMediaTypesValue in Drupal 10
Same name and namespace in other branches
- 8 core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php \Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::setMediaTypesValue()
- 9 core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php \Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::setMediaTypesValue()
Value callback to optimize the way the media type weights are stored.
The tabledrag functionality needs a specific weight field, but we don't want to store this extra weight field in our settings.
Parameters
array $element: An associative array containing the properties of the element.
mixed $input: The incoming input to populate the form element. If this is FALSE, the element's default value should be returned.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
mixed The value to assign to the element.
File
- core/
modules/ media_library/ src/ Plugin/ Field/ FieldWidget/ MediaLibraryWidget.php, line 239
Class
- MediaLibraryWidget
- Plugin implementation of the 'media_library_widget' widget.
Namespace
Drupal\media_library\Plugin\Field\FieldWidgetCode
public static function setMediaTypesValue(array &$element, $input, FormStateInterface $form_state) {
if ($input === FALSE) {
return $element['#default_value'] ?? [];
}
// Sort the media types by weight value and set the value in the form state.
uasort($input, 'Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
$sorted_media_type_ids = array_keys($input);
$form_state
->setValue($element['#parents'], $sorted_media_type_ids);
// We have to unset the child elements containing the weight fields for each
// media type to stop FormBuilder::doBuildForm() from processing the weight
// fields as well.
foreach ($sorted_media_type_ids as $media_type_id) {
unset($element[$media_type_id]);
}
return $sorted_media_type_ids;
}