You are here

class FileEventSubscriber in Thunder 6.2.x

Class for file upload events.

Hierarchy

  • class \Drupal\thunder_media\EventSubscriber\FileEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of FileEventSubscriber

1 string reference to 'FileEventSubscriber'
thunder_media.services.yml in modules/thunder_media/thunder_media.services.yml
modules/thunder_media/thunder_media.services.yml
1 service uses FileEventSubscriber
thunder_media.file.event.subscriber in modules/thunder_media/thunder_media.services.yml
Drupal\thunder_media\EventSubscriber\FileEventSubscriber

File

modules/thunder_media/src/EventSubscriber/FileEventSubscriber.php, line 13

Namespace

Drupal\thunder_media\EventSubscriber
View source
class FileEventSubscriber implements EventSubscriberInterface {

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * The alias cleaner service.
   *
   * @var \Drupal\pathauto\AliasCleanerInterface|null
   */
  protected $aliasCleaner;

  /**
   * FileEventSubscriber constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config factory service.
   * @param \Drupal\pathauto\AliasCleanerInterface|null $aliasCleaner
   *   The alias cleaner service.
   */
  public function __construct(ConfigFactoryInterface $configFactory, AliasCleanerInterface $aliasCleaner = NULL) {
    $this->config = $configFactory
      ->get('thunder_media.settings');
    $this->aliasCleaner = $aliasCleaner;
  }

  /**
   * Sanitize a filename during upload.
   *
   * @param \Drupal\Core\File\Event\FileUploadSanitizeNameEvent $event
   *   The file upload event.
   */
  public function sanitizeFilename(FileUploadSanitizeNameEvent $event) {
    if ($this->config
      ->get('enable_filename_transliteration')) {
      $pathinfo = pathinfo($event
        ->getFilename());

      // Check for needed pathinfo array keys.
      if (!empty($pathinfo['filename']) && !empty($pathinfo['extension'])) {
        $cleanFilename = $this->aliasCleaner
          ->cleanString($pathinfo['filename']) . '.' . $pathinfo['extension'];
        $event
          ->setFilename($cleanFilename);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      FileUploadSanitizeNameEvent::class => 'sanitizeFilename',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileEventSubscriber::$aliasCleaner protected property The alias cleaner service.
FileEventSubscriber::$config protected property The config factory service.
FileEventSubscriber::getSubscribedEvents public static function
FileEventSubscriber::sanitizeFilename public function Sanitize a filename during upload.
FileEventSubscriber::__construct public function FileEventSubscriber constructor.