You are here

public function FileInputExtensionMatchTrait::appliesTo in Lightning Media 8

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

Implements InputMatchInterface::appliesTo().

File

src/FileInputExtensionMatchTrait.php, line 26

Class

FileInputExtensionMatchTrait
Implements InputMatchInterface for media types that use a file field.

Namespace

Drupal\lightning_media

Code

public function appliesTo($value, MediaTypeInterface $media_type) {
  if (is_numeric($value)) {
    $value = $this
      ->entityTypeManager()
      ->getStorage('file')
      ->load($value);
  }
  if ($value instanceof FileInterface && ($field = $this
    ->getSourceFieldDefinition($media_type))) {
    $extension = pathinfo($value
      ->getFilename(), PATHINFO_EXTENSION);
    $extension = strtolower($extension);
    return in_array($extension, preg_split('/,?\\s+/', $field
      ->getSetting('file_extensions')));
  }
  return FALSE;
}