You are here

trait FileInputExtensionMatchTrait in Lightning Media 8

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

Implements InputMatchInterface for media types that use a file field.

Hierarchy

2 files declare their use of FileInputExtensionMatchTrait
File.php in modules/lightning_media_document/src/Plugin/media/Source/File.php
Image.php in modules/lightning_media_image/src/Plugin/media/Source/Image.php

File

src/FileInputExtensionMatchTrait.php, line 11

Namespace

Drupal\lightning_media
View source
trait FileInputExtensionMatchTrait {

  /**
   * Returns the entity type manager.
   *
   * @return \Drupal\Core\Entity\EntityTypeManagerInterface
   *   The entity type manager.
   */
  private function entityTypeManager() {
    return @($this->entityTypeManager ?: \Drupal::entityTypeManager());
  }

  /**
   * Implements InputMatchInterface::appliesTo().
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileInputExtensionMatchTrait::appliesTo public function Implements InputMatchInterface::appliesTo().
FileInputExtensionMatchTrait::entityTypeManager private function Returns the entity type manager.