You are here

public function MediaHelper::getFileExtensions in Lightning Media 8

Same name and namespace in other branches
  1. 8.4 src/MediaHelper.php \Drupal\lightning_media\MediaHelper::getFileExtensions()
  2. 8.2 src/MediaHelper.php \Drupal\lightning_media\MediaHelper::getFileExtensions()
  3. 8.3 src/MediaHelper.php \Drupal\lightning_media\MediaHelper::getFileExtensions()

Returns all file extensions accepted by bundles that use file fields.

Parameters

bool $check_access: (optional) Whether to filter the bundles by create access for the current user. Defaults to FALSE.

string[] $bundles: (optional) An array of bundle IDs from which to retrieve source field extensions. If omitted, all available bundles are allowed.

Return value

string[] The file extensions accepted by all available bundles.

File

src/MediaHelper.php, line 46

Class

MediaHelper
Provides helper methods for dealing with media entities.

Namespace

Drupal\lightning_media

Code

public function getFileExtensions($check_access = FALSE, array $bundles = []) {
  $extensions = '';

  // Lightning Media overrides the media_bundle storage handler with a special
  // one that adds an optional second parameter to loadMultiple().
  $storage = $this->entityTypeManager
    ->getStorage('media_type');
  $media_types = $storage
    ->loadMultiple($bundles ?: NULL, $check_access);

  /** @var \Drupal\media\MediaTypeInterface $media_type */
  foreach ($media_types as $media_type) {
    $field = $media_type
      ->getSource()
      ->getSourceFieldDefinition($media_type);

    // If the field is a FileItem or any of its descendants, we can consider
    // it a file field. This will automatically include things like image
    // fields, which extend file fields.
    if (is_a($field
      ->getItemDefinition()
      ->getClass(), FileItem::class, TRUE)) {
      $extensions .= $field
        ->getSetting('file_extensions') . ' ';
    }
  }
  $extensions = preg_split('/,?\\s+/', rtrim($extensions));
  return array_unique($extensions);
}