Skip to content

MediaHelper

Helpers for working with media entities.


  • Full name: \Drupal\helper\MediaHelper

Properties

entityTypeManager

protected \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager

Methods

__construct

Constructs a new MediaHelper object.

public __construct(\Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager): mixed

Parameters:

Parameter Type Description
$entityTypeManager \Drupal\Core\Entity\EntityTypeManagerInterface
The entity type manager.

getFile

Get the source file from a media entity.

public getFile(\Drupal\media\MediaInterface $media): \Drupal\file\FileInterface|null

This does not check if the current user has access to the file.

Parameters:

Parameter Type Description
$media \Drupal\media\MediaInterface
The media entity.

Return Value:

The file if available, otherwise NULL.


getFileExtensions

Get the list of allowed file extensions for a media type.

public getFileExtensions(\Drupal\media\MediaInterface|\Drupal\media\MediaTypeInterface|string $mediaType): string[]|null

Parameters:

Parameter Type Description
$mediaType \Drupal\media\MediaInterface|\Drupal\media\MediaTypeInterface|string
A media type entity, a media entity, or a media type ID.

Return Value:

The list of allowed file extensions, or an empty array if any extension is allowed. NULL if the media type does not exist or its source is not file-based.