You are here

class MediaTypeManager in Media Bulk Upload 8

Class MediaTypeManager

@package Drupal\media_bulk_upload

Hierarchy

Expanded class hierarchy of MediaTypeManager

1 string reference to 'MediaTypeManager'
media_bulk_upload.services.yml in ./media_bulk_upload.services.yml
media_bulk_upload.services.yml
1 service uses MediaTypeManager
media_bulk_upload.media_type_manager in ./media_bulk_upload.services.yml
Drupal\media_bulk_upload\MediaTypeManager

File

src/MediaTypeManager.php, line 15

Namespace

Drupal\media_bulk_upload
View source
class MediaTypeManager implements MediaTypeManagerInterface {

  /**
   * List of media types, grouped by file extension.
   *
   * @var array
   */
  protected $mediaTypeExtensions;

  /**
   * Entity Type Manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Entity Field Manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * MediaTypeMatcher constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity Type Manager.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
   *   Entity Field Manager.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityFieldManagerInterface $entityFieldManager) {
    $this->entityTypeManager = $entityTypeManager;
    $this->entityFieldManager = $entityFieldManager;

    /** @var \Drupal\media\MediaTypeInterface[] $mediaTypes */
    $mediaTypes = $entityTypeManager
      ->getStorage('media_type')
      ->loadMultiple();
    $this
      ->groupMediaTypes($mediaTypes);
  }

  /**
   * Group media types by extension.
   *
   * @param \Drupal\media\MediaTypeInterface[] $mediaTypes
   *   Media Types.
   *
   * @return $this
   *   Media Type Manager.
   */
  private function groupMediaTypes($mediaTypes) {
    foreach ($mediaTypes as $mediaType) {
      $this
        ->processMediaTypeExtensions($mediaType);
    }
    return $this;
  }

  /**
   * Process the extensions belonging to the media type.
   *
   * @param \Drupal\media\MediaTypeInterface $mediaType
   *   Media Type.
   *
   * @return $this
   *   Media Type Manager.
   */
  private function processMediaTypeExtensions(MediaTypeInterface $mediaType) {
    foreach ($this
      ->getMediaTypeExtensions($mediaType) as $extension) {
      $this->mediaTypeExtensions[$extension][$mediaType
        ->id()] = $mediaType;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getMediaTypeExtensions(MediaTypeInterface $mediaType) {
    return $this
      ->getTargetFieldExtensions($this
      ->getTargetFieldSettings($mediaType));
  }

  /**
   * {@inheritdoc}
   */
  public function getTargetFieldExtensions(array $targetFieldSettings) {
    $extensions = isset($targetFieldSettings['file_extensions']) ? $targetFieldSettings['file_extensions'] : '';
    return array_map('trim', explode(' ', $extensions));
  }

  /**
   * {@inheritdoc}
   */
  public function getTargetFieldSettings(MediaTypeInterface $mediaType) {
    $fieldDefinitions = $this->entityFieldManager
      ->getFieldDefinitions('media', $mediaType
      ->id());
    $targetFieldName = $this
      ->getTargetFieldName($mediaType);

    /** @var \Drupal\field\Entity\FieldConfig $targetField */
    $targetField = $fieldDefinitions[$targetFieldName];
    return $targetField
      ->getSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function getTargetFieldName(MediaTypeInterface $mediaType) {
    return $mediaType
      ->getSource()
      ->getConfiguration()['source_field'];
  }

  /**
   * {@inheritdoc}
   *
   * @throws \Exception
   */
  public function getMediaTypeIdsByFileExtension($extension) {
    if (!isset($this->mediaTypeExtensions[$extension])) {
      throw new \Exception('No matching media type id for the given file.');
    }
    return $this->mediaTypeExtensions[$extension];
  }

  /**
   * {@inheritdoc}
   */
  public function getTargetFieldMaxSize(MediaTypeInterface $mediaType) {
    $targetFieldSettings = $this
      ->getTargetFieldSettings($mediaType);
    $filesize = isset($targetFieldSettings['max_filesize']) ? $targetFieldSettings['max_filesize'] : '';
    return $filesize;
  }

  /**
   * {@inheritdoc}
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function getBulkMediaTypes(MediaBulkConfigInterface $mediaBulkConfig) {
    $mediaTypeIds = $mediaBulkConfig
      ->get('media_types');
    return $this->entityTypeManager
      ->getStorage('media_type')
      ->loadMultiple($mediaTypeIds);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MediaTypeManager::$entityFieldManager protected property Entity Field Manager.
MediaTypeManager::$entityTypeManager protected property Entity Type Manager.
MediaTypeManager::$mediaTypeExtensions protected property List of media types, grouped by file extension.
MediaTypeManager::getBulkMediaTypes public function Overrides MediaTypeManagerInterface::getBulkMediaTypes
MediaTypeManager::getMediaTypeExtensions public function Get the Media Type extensions. Overrides MediaTypeManagerInterface::getMediaTypeExtensions
MediaTypeManager::getMediaTypeIdsByFileExtension public function Overrides MediaTypeManagerInterface::getMediaTypeIdsByFileExtension
MediaTypeManager::getTargetFieldExtensions public function Return the media type target field. Overrides MediaTypeManagerInterface::getTargetFieldExtensions
MediaTypeManager::getTargetFieldMaxSize public function Get the target maximum upload size. Overrides MediaTypeManagerInterface::getTargetFieldMaxSize
MediaTypeManager::getTargetFieldName public function Get the target field name. Overrides MediaTypeManagerInterface::getTargetFieldName
MediaTypeManager::getTargetFieldSettings public function Get the target field settings for the media type. Overrides MediaTypeManagerInterface::getTargetFieldSettings
MediaTypeManager::groupMediaTypes private function Group media types by extension.
MediaTypeManager::processMediaTypeExtensions private function Process the extensions belonging to the media type.
MediaTypeManager::__construct public function MediaTypeMatcher constructor.