You are here

protected function MediaLibraryWidget::getAllowedMediaTypeIdsSorted in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php \Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::getAllowedMediaTypeIdsSorted()
  2. 9 core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php \Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::getAllowedMediaTypeIdsSorted()

Gets the enabled media type IDs sorted by weight.

Return value

string[] The media type IDs sorted by weight.

3 calls to MediaLibraryWidget::getAllowedMediaTypeIdsSorted()
MediaLibraryWidget::formElement in core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
Returns the form for a single field widget.
MediaLibraryWidget::settingsForm in core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
Returns a form to configure settings for the widget.
MediaLibraryWidget::settingsSummary in core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
Returns a short summary for the current widget settings.

File

core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php, line 133

Class

MediaLibraryWidget
Plugin implementation of the 'media_library_widget' widget.

Namespace

Drupal\media_library\Plugin\Field\FieldWidget

Code

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 = $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);
}