You are here

class MediaSubFormManager in Media Bulk Upload 8

Class MediaSubFormManager.

@package Drupal\media_bulk_upload

Hierarchy

Expanded class hierarchy of MediaSubFormManager

1 file declares its use of MediaSubFormManager
MediaBulkUploadForm.php in src/Form/MediaBulkUploadForm.php
1 string reference to 'MediaSubFormManager'
media_bulk_upload.services.yml in ./media_bulk_upload.services.yml
media_bulk_upload.services.yml
1 service uses MediaSubFormManager
media_bulk_upload.subform_manager in ./media_bulk_upload.services.yml
Drupal\media_bulk_upload\MediaSubFormManager

File

src/MediaSubFormManager.php, line 24

Namespace

Drupal\media_bulk_upload
View source
class MediaSubFormManager implements ContainerInjectionInterface, MediaSubFormManagerInterface {

  /**
   * Default max file size.
   *
   * @var string
   */
  protected $defaultMaxFileSize = '32MB';

  /**
   * Media Type storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $mediaTypeStorage;

  /**
   * Entity Form Display storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $entityFormDisplayStorage;

  /**
   * Media entity storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $mediaStorage;

  /**
   * Media Type Manager.
   *
   * @var \Drupal\media_bulk_upload\MediaTypeManager
   */
  protected $mediaTypeManager;

  /**
   * Token service.
   *
   * @var \Drupal\Core\Utility\Token
   */
  protected $token;

  /**
   * File system interface.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * BulkMediaUploadForm constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity type manager.
   * @param \Drupal\media_bulk_upload\MediaTypeManager $mediaTypeManager
   *   Media Type Manager.
   * @param \Drupal\Core\Utility\Token $token
   *   Token service.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, MediaTypeManager $mediaTypeManager, Token $token, FileSystemInterface $fileSystem) {
    $this->mediaTypeManager = $mediaTypeManager;
    $this->mediaTypeStorage = $entityTypeManager
      ->getStorage('media_type');
    $this->mediaStorage = $entityTypeManager
      ->getStorage('media');
    $this->entityFormDisplayStorage = $entityTypeManager
      ->getStorage('entity_form_display');
    $this->token = $token;
    $this->defaultMaxFileSize = $this
      ->formatSize(Environment::getUploadMaxSize());
    $this->fileSystem = $fileSystem;
  }

  /**
   * Format the amount of bites into a common string format.
   *
   * @param int $size
   *  Size in bytes.
   *
   * @return string
   *   Formatted file size.
   */
  private function formatSize($size) {
    $unit = 'B';
    $calculatedSize = $size;
    if ($size >= Bytes::KILOBYTE) {
      $calculatedSize = $size / Bytes::KILOBYTE;
      $units = [
        'KB',
        'MB',
        'GB',
        'TB',
        'PB',
        'EB',
        'ZB',
        'YB',
      ];
      foreach ($units as $unit) {
        if (round($calculatedSize, 2) >= Bytes::KILOBYTE) {
          $calculatedSize /= Bytes::KILOBYTE;
        }
        else {
          break;
        }
      }
    }
    $calculatedSize = round($calculatedSize, 2);
    return sprintf('%d ' . $unit, $calculatedSize);
  }

  /**
   * {@inheritdoc}
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('media_bulk_upload.media_type_manager'), $container
      ->get('token'), $container
      ->get('file_system'));
  }

  /**
   * {@inheritdoc}
   */
  public function getTargetFieldDirectory(MediaTypeInterface $mediaType) {
    $targetFieldSettings = $this->mediaTypeManager
      ->getTargetFieldSettings($mediaType);
    $fileDirectory = trim($targetFieldSettings['file_directory'], '/');
    $fileDirectory = PlainTextOutput::renderFromHtml($this->token
      ->replace($fileDirectory));
    $targetDirectory = $targetFieldSettings['uri_scheme'] . '://' . $fileDirectory;
    $this->fileSystem
      ->prepareDirectory($targetDirectory, FileSystemInterface::CREATE_DIRECTORY);
    return $targetDirectory;
  }

  /**
   * {@inheritdoc}
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function buildMediaSubForm(array &$form, FormStateInterface $form_state, MediaBulkConfigInterface $mediaBulkConfig) {
    $mediaTypes = $this->mediaTypeManager
      ->getBulkMediaTypes($mediaBulkConfig);
    $mediaType = reset($mediaTypes);

    /** @var \Drupal\media\MediaInterface $dummyMedia */
    $dummyMedia = $this->mediaStorage
      ->create([
      'bundle' => $mediaType
        ->id(),
    ]);
    $mediaFormDisplay = $this
      ->getMediaFormDisplay($mediaBulkConfig, $mediaType);
    $mediaFormDisplay
      ->buildForm($dummyMedia, $form, $form_state);
    $targetFieldName = $this->mediaTypeManager
      ->getTargetFieldName($mediaType);
    unset($form[$targetFieldName]);
    $fields = $this
      ->getFields($mediaBulkConfig);
    if (empty($fields)) {
      return $this;
    }
    $this
      ->configureSharedFields($form, $fields);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getMediaFormDisplay(MediaBulkConfigInterface $mediaBulkConfig, MediaTypeInterface $mediaType) {

    /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $mediaFormDisplay */
    $mediaFormDisplay = $this->entityFormDisplayStorage
      ->load('media.' . $mediaType
      ->id() . '.' . $mediaBulkConfig
      ->get('form_mode'));
    if ($mediaFormDisplay === NULL) {
      $mediaFormDisplay = $this->entityFormDisplayStorage
        ->load('media.' . $mediaType
        ->id() . '.default');
    }
    return $mediaFormDisplay;
  }

  /**
   * {@inheritdoc}
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function getFields(MediaBulkConfigInterface $mediaBulkConfig) {
    $mediaTypes = $this->mediaTypeManager
      ->getBulkMediaTypes($mediaBulkConfig);
    $fields = $this
      ->getMediaEntityFieldComponents($mediaBulkConfig, array_shift($mediaTypes));
    foreach ($mediaTypes as $mediaType) {
      $fields = array_intersect($fields, $this
        ->getMediaEntityFieldComponents($mediaBulkConfig, $mediaType));
    }
    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function getMediaEntityFieldComponents(MediaBulkConfigInterface $mediaBulkConfig, MediaTypeInterface $mediaType) {
    $mediaFormDisplay = $this
      ->getMediaFormDisplay($mediaBulkConfig, $mediaType);
    $fieldComponents = $mediaFormDisplay
      ->getComponents();
    return array_keys($fieldComponents);
  }

  /**
   * {@inheritdoc}
   */
  public function configureSharedFields(array &$elements, array $allowedFields) {
    $children = Element::children($elements);
    foreach ($children as $child) {
      if (!in_array($child, $allowedFields, TRUE)) {
        unset($elements[$child]);
        continue;
      }
      $this
        ->forceFieldsAsOptional($elements[$child]);
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function forceFieldsAsOptional(array &$elements) {
    if (isset($elements['#required'])) {
      $elements['#required'] = FALSE;
    }
    $children = Element::children($elements);
    foreach ($children as $child) {
      $this
        ->forceFieldsAsOptional($elements[$child]);
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function validateMediaFormDisplayUse(MediaBulkConfigInterface $mediaBulkConfig) {
    $formMode = $mediaBulkConfig
      ->get('form_mode');
    if (!empty($formMode)) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getMediaTypeManager() {
    return $this->mediaTypeManager;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultMaxFileSize() {
    return $this->defaultMaxFileSize;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MediaSubFormManager::$defaultMaxFileSize protected property Default max file size.
MediaSubFormManager::$entityFormDisplayStorage protected property Entity Form Display storage.
MediaSubFormManager::$fileSystem protected property File system interface.
MediaSubFormManager::$mediaStorage protected property Media entity storage.
MediaSubFormManager::$mediaTypeManager protected property Media Type Manager.
MediaSubFormManager::$mediaTypeStorage protected property Media Type storage.
MediaSubFormManager::$token protected property Token service.
MediaSubFormManager::buildMediaSubForm public function Overrides MediaSubFormManagerInterface::buildMediaSubForm
MediaSubFormManager::configureSharedFields public function Configure all the shared fields. Overrides MediaSubFormManagerInterface::configureSharedFields
MediaSubFormManager::create public static function Overrides ContainerInjectionInterface::create
MediaSubFormManager::forceFieldsAsOptional public function Make sure the fields are optional, instead of required. Overrides MediaSubFormManagerInterface::forceFieldsAsOptional
MediaSubFormManager::formatSize private function Format the amount of bites into a common string format.
MediaSubFormManager::getDefaultMaxFileSize public function Get the default max file size. Overrides MediaSubFormManagerInterface::getDefaultMaxFileSize
MediaSubFormManager::getFields public function Overrides MediaSubFormManagerInterface::getFields
MediaSubFormManager::getMediaEntityFieldComponents public function Get the field components for the given media type. Overrides MediaSubFormManagerInterface::getMediaEntityFieldComponents
MediaSubFormManager::getMediaFormDisplay public function Get the media form display for the given media type. Overrides MediaSubFormManagerInterface::getMediaFormDisplay
MediaSubFormManager::getMediaTypeManager public function Get Media Type Manager. Overrides MediaSubFormManagerInterface::getMediaTypeManager
MediaSubFormManager::getTargetFieldDirectory public function Get the target field settings for the media type. Overrides MediaSubFormManagerInterface::getTargetFieldDirectory
MediaSubFormManager::validateMediaFormDisplayUse public function Check if the media form fields should be used in the upload form. Overrides MediaSubFormManagerInterface::validateMediaFormDisplayUse
MediaSubFormManager::__construct public function BulkMediaUploadForm constructor.