protected function MediaDirectoriesLibraryWidget::getAllowedMediaTypeIdsSorted in Media Directories 3.x
Gets the enabled media type IDs sorted by weight.
Return value
string[] The media type IDs sorted by weight.
Overrides MediaLibraryWidget::getAllowedMediaTypeIdsSorted
1 call to MediaDirectoriesLibraryWidget::getAllowedMediaTypeIdsSorted()
- MediaDirectoriesLibraryWidget::formElement in modules/
media_directories_ui/ src/ Plugin/ Field/ FieldWidget/ MediaDirectoriesLibraryWidget.php - Returns the form for a single field widget.
File
- modules/
media_directories_ui/ src/ Plugin/ Field/ FieldWidget/ MediaDirectoriesLibraryWidget.php, line 46
Class
- MediaDirectoriesLibraryWidget
- Plugin implementation of the 'media_directories_library_widget' widget.
Namespace
Drupal\media_directories_ui\Plugin\Field\FieldWidgetCode
protected function getAllowedMediaTypeIdsSorted() {
// Get the media type IDs sorted by the user in the settings form.
$sorted_media_type_ids = $this
->getSetting('media_types');
// Get the configured media types from the field storage.
$handler_settings = $this
->getFieldSetting('handler_settings');
// The target bundles will be blank when saving field storage settings,
// when first adding a media reference field.
$allowed_media_type_ids = isset($handler_settings['target_bundles']) ? $handler_settings['target_bundles'] : NULL;
// When there are no allowed media types, return the empty array.
if ($allowed_media_type_ids === []) {
return $allowed_media_type_ids;
}
// When no target bundles are configured for the field, all are allowed.
if ($allowed_media_type_ids === NULL) {
$allowed_media_type_ids = $this->entityTypeManager
->getStorage('media_type')
->getQuery()
->execute();
}
// When the user did not sort the media types, return the media type IDs
// configured for the field.
if (empty($sorted_media_type_ids)) {
return $allowed_media_type_ids;
}
// Some of the media types may no longer exist, and new media types may have
// been added that we don't yet know about. We need to make sure new media
// types are added to the list and remove media types that are no longer
// configured for the field.
$new_media_type_ids = array_diff($allowed_media_type_ids, $sorted_media_type_ids);
// Add new media type IDs to the list.
$sorted_media_type_ids = array_merge($sorted_media_type_ids, array_values($new_media_type_ids));
// Remove media types that are no longer available.
$sorted_media_type_ids = array_intersect($sorted_media_type_ids, $allowed_media_type_ids);
// Make sure the keys are numeric.
return array_values($sorted_media_type_ids);
}