class FileMediaFormatterBase
Same name in other branches
- 9 core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php \Drupal\file\Plugin\Field\FieldFormatter\FileMediaFormatterBase
- 8.9.x core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php \Drupal\file\Plugin\Field\FieldFormatter\FileMediaFormatterBase
- 10 core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php \Drupal\file\Plugin\Field\FieldFormatter\FileMediaFormatterBase
Base class for media file formatter.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Core\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait
- class \Drupal\Core\Field\PluginSettingsBase extends \Drupal\Core\Plugin\PluginBase implements \Drupal\Core\Field\PluginSettingsInterface, \Drupal\Component\Plugin\DependentPluginInterface
- class \Drupal\Core\Field\FormatterBase extends \Drupal\Core\Field\PluginSettingsBase implements \Drupal\Core\Field\FormatterInterface, \Drupal\Core\Plugin\ContainerFactoryPluginInterface
- class \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase extends \Drupal\Core\Field\FormatterBase
- class \Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase extends \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase
- class \Drupal\file\Plugin\Field\FieldFormatter\FileMediaFormatterBase extends \Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase implements \Drupal\file\Plugin\Field\FieldFormatter\FileMediaFormatterInterface
- class \Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase extends \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase
- class \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase extends \Drupal\Core\Field\FormatterBase
- class \Drupal\Core\Field\FormatterBase extends \Drupal\Core\Field\PluginSettingsBase implements \Drupal\Core\Field\FormatterInterface, \Drupal\Core\Plugin\ContainerFactoryPluginInterface
- class \Drupal\Core\Field\PluginSettingsBase extends \Drupal\Core\Plugin\PluginBase implements \Drupal\Core\Field\PluginSettingsInterface, \Drupal\Component\Plugin\DependentPluginInterface
- class \Drupal\Core\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait
Expanded class hierarchy of FileMediaFormatterBase
File
-
core/
modules/ file/ src/ Plugin/ Field/ FieldFormatter/ FileMediaFormatterBase.php, line 15
Namespace
Drupal\file\Plugin\Field\FieldFormatterView source
abstract class FileMediaFormatterBase extends FileFormatterBase implements FileMediaFormatterInterface {
/**
* Gets the HTML tag for the formatter.
*
* @return string
* The HTML tag of this formatter.
*/
protected function getHtmlTag() {
return static::getMediaType();
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'controls' => TRUE,
'autoplay' => FALSE,
'loop' => FALSE,
'multiple_file_display_type' => 'tags',
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
return [
'controls' => [
'#title' => $this->t('Show playback controls'),
'#type' => 'checkbox',
'#default_value' => $this->getSetting('controls'),
],
'autoplay' => [
'#title' => $this->t('Autoplay'),
'#type' => 'checkbox',
'#default_value' => $this->getSetting('autoplay'),
],
'loop' => [
'#title' => $this->t('Loop'),
'#type' => 'checkbox',
'#default_value' => $this->getSetting('loop'),
],
'multiple_file_display_type' => [
'#title' => $this->t('Display of multiple files'),
'#type' => 'radios',
'#options' => [
'tags' => $this->t('Use multiple @tag tags, each with a single source.', [
'@tag' => '<' . $this->getHtmlTag() . '>',
]),
'sources' => $this->t('Use multiple sources within a single @tag tag.', [
'@tag' => '<' . $this->getHtmlTag() . '>',
]),
],
'#default_value' => $this->getSetting('multiple_file_display_type'),
],
];
}
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
if (!parent::isApplicable($field_definition)) {
return FALSE;
}
/** @var \Symfony\Component\Mime\MimeTypeGuesserInterface $extension_mime_type_guesser */
$extension_mime_type_guesser = \Drupal::service('file.mime_type.guesser.extension');
$extension_list = array_filter(preg_split('/\\s+/', $field_definition->getSetting('file_extensions')));
foreach ($extension_list as $extension) {
$mime_type = $extension_mime_type_guesser->guessMimeType('fakedFile.' . $extension);
if (static::mimeTypeApplies($mime_type)) {
return TRUE;
}
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$summary[] = $this->t('Playback controls: %controls', [
'%controls' => $this->getSetting('controls') ? $this->t('visible') : $this->t('hidden'),
]);
$summary[] = $this->t('Autoplay: %autoplay', [
'%autoplay' => $this->getSetting('autoplay') ? $this->t('yes') : $this->t('no'),
]);
$summary[] = $this->t('Loop: %loop', [
'%loop' => $this->getSetting('loop') ? $this->t('yes') : $this->t('no'),
]);
switch ($this->getSetting('multiple_file_display_type')) {
case 'tags':
$summary[] = $this->t('Multiple file display: Multiple HTML tags');
break;
case 'sources':
$summary[] = $this->t('Multiple file display: One HTML tag with multiple sources');
break;
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$source_files = $this->getSourceFiles($items, $langcode);
if (empty($source_files)) {
return $elements;
}
$attributes = $this->prepareAttributes();
foreach ($source_files as $delta => $files) {
$elements[$delta] = [
'#theme' => $this->getPluginId(),
'#attributes' => $attributes,
'#files' => $files,
'#cache' => [
'tags' => [],
],
];
$cache_tags = [];
foreach ($files as $file) {
$cache_tags = Cache::mergeTags($cache_tags, $file['file']->getCacheTags());
}
$elements[$delta]['#cache']['tags'] = $cache_tags;
}
return $elements;
}
/**
* Prepare the attributes according to the settings.
*
* @param string[] $additional_attributes
* Additional attributes to be applied to the HTML element. Attribute names
* will be used as key and value in the HTML element.
*
* @return \Drupal\Core\Template\Attribute
* Container with all the attributes for the HTML tag.
*/
protected function prepareAttributes(array $additional_attributes = []) {
$attributes = new Attribute();
foreach (array_merge([
'controls',
'autoplay',
'loop',
], $additional_attributes) as $attribute) {
if ($this->getSetting($attribute)) {
$attributes->setAttribute($attribute, $attribute);
}
}
return $attributes;
}
/**
* Check if given MIME type applies to the media type of the formatter.
*
* @param string $mime_type
* The complete MIME type.
*
* @return bool
* TRUE if the MIME type applies, FALSE otherwise.
*/
protected static function mimeTypeApplies($mime_type) {
[
$type,
] = explode('/', $mime_type, 2);
return $type === static::getMediaType();
}
/**
* Gets source files with attributes.
*
* @param \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items
* The item list.
* @param string $langcode
* The language code of the referenced entities to display.
*
* @return array
* Numerically indexed array, which again contains an associative array with
* the following key/values:
* - file => \Drupal\file\Entity\File
* - source_attributes => \Drupal\Core\Template\Attribute
*/
protected function getSourceFiles(EntityReferenceFieldItemListInterface $items, $langcode) {
$source_files = [];
// Because we can have the files grouped in a single media tag, we do a
// grouping in case the multiple file behavior is not 'tags'.
/** @var \Drupal\file\Entity\File $file */
foreach ($this->getEntitiesToView($items, $langcode) as $file) {
if (static::mimeTypeApplies($file->getMimeType())) {
$source_attributes = new Attribute();
$source_attributes->setAttribute('src', $file->createFileUrl())
->setAttribute('type', $file->getMimeType());
if ($this->getSetting('multiple_file_display_type') === 'tags') {
$source_files[] = [
[
'file' => $file,
'source_attributes' => $source_attributes,
],
];
}
else {
$source_files[0][] = [
'file' => $file,
'source_attributes' => $source_attributes,
];
}
}
}
return $source_files;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
EntityReferenceFormatterBase::getEntitiesToView | protected | function | Returns the referenced entities for display. | 1 | |
EntityReferenceFormatterBase::prepareView | public | function | Loads the entities referenced in that field across all the entities being viewed. |
Overrides FormatterBase::prepareView | |
EntityReferenceFormatterBase::view | public | function | Overrides FormatterBase::view | ||
FileFormatterBase::checkAccess | protected | function | Checks access to the given entity. | Overrides EntityReferenceFormatterBase::checkAccess | 1 |
FileFormatterBase::needsEntityLoad | protected | function | Returns whether the entity referenced by an item needs to be loaded. | Overrides EntityReferenceFormatterBase::needsEntityLoad | 1 |
FileMediaFormatterBase::defaultSettings | public static | function | Defines the default settings for this plugin. | Overrides PluginSettingsBase::defaultSettings | 1 |
FileMediaFormatterBase::getHtmlTag | protected | function | Gets the HTML tag for the formatter. | ||
FileMediaFormatterBase::getSourceFiles | protected | function | Gets source files with attributes. | ||
FileMediaFormatterBase::isApplicable | public static | function | Returns if the formatter can be used for the provided field. | Overrides FormatterBase::isApplicable | |
FileMediaFormatterBase::mimeTypeApplies | protected static | function | Check if given MIME type applies to the media type of the formatter. | ||
FileMediaFormatterBase::prepareAttributes | protected | function | Prepare the attributes according to the settings. | 1 | |
FileMediaFormatterBase::settingsForm | public | function | Returns a form to configure settings for the formatter. | Overrides FormatterBase::settingsForm | 1 |
FileMediaFormatterBase::settingsSummary | public | function | Returns a short summary for the current formatter settings. | Overrides FormatterBase::settingsSummary | 1 |
FileMediaFormatterBase::viewElements | public | function | Builds a renderable array for a field value. | Overrides FormatterInterface::viewElements | |
FileMediaFormatterInterface::getMediaType | public static | function | Gets the applicable media type for a formatter. | 2 | |
FormatterBase::$fieldDefinition | protected | property | The field definition. | ||
FormatterBase::$label | protected | property | The label display setting. | ||
FormatterBase::$settings | protected | property | The formatter settings. | Overrides PluginSettingsBase::$settings | |
FormatterBase::$viewMode | protected | property | The view mode. | ||
FormatterBase::create | public static | function | Creates an instance of the plugin. | Overrides ContainerFactoryPluginInterface::create | 13 |
FormatterBase::getFieldSetting | protected | function | Returns the value of a field setting. | ||
FormatterBase::getFieldSettings | protected | function | Returns the array of field settings. | ||
FormatterBase::__construct | public | function | Constructs a FormatterBase object. | 13 | |
PluginInspectionInterface::getPluginDefinition | public | function | Gets the definition of the plugin implementation. | 6 | |
PluginInspectionInterface::getPluginId | public | function | Gets the plugin ID of the plugin instance. | 2 | |
PluginSettingsBase::$defaultSettingsMerged | protected | property | Whether default settings have been merged into the current $settings. | ||
PluginSettingsBase::$thirdPartySettings | protected | property | The plugin settings injected by third party modules. | ||
PluginSettingsBase::calculateDependencies | public | function | Calculates dependencies for the configured plugin. | Overrides DependentPluginInterface::calculateDependencies | 6 |
PluginSettingsBase::getSetting | public | function | Returns the value of a setting, or its default value if absent. | Overrides PluginSettingsInterface::getSetting | |
PluginSettingsBase::getSettings | public | function | Returns the array of settings, including defaults for missing settings. | Overrides PluginSettingsInterface::getSettings | |
PluginSettingsBase::getThirdPartyProviders | public | function | Gets the list of third parties that store information. | Overrides ThirdPartySettingsInterface::getThirdPartyProviders | |
PluginSettingsBase::getThirdPartySetting | public | function | Gets the value of a third-party setting. | Overrides ThirdPartySettingsInterface::getThirdPartySetting | |
PluginSettingsBase::getThirdPartySettings | public | function | Gets all third-party settings of a given module. | Overrides ThirdPartySettingsInterface::getThirdPartySettings | |
PluginSettingsBase::mergeDefaults | protected | function | Merges default settings values into $settings. | ||
PluginSettingsBase::onDependencyRemoval | public | function | Informs the plugin that some configuration it depends on will be deleted. | Overrides PluginSettingsInterface::onDependencyRemoval | 3 |
PluginSettingsBase::setSetting | public | function | Sets the value of a setting for the plugin. | Overrides PluginSettingsInterface::setSetting | |
PluginSettingsBase::setSettings | public | function | Sets the settings for the plugin. | Overrides PluginSettingsInterface::setSettings | |
PluginSettingsBase::setThirdPartySetting | public | function | Sets the value of a third-party setting. | Overrides ThirdPartySettingsInterface::setThirdPartySetting | |
PluginSettingsBase::unsetThirdPartySetting | public | function | Unsets a third-party setting. | Overrides ThirdPartySettingsInterface::unsetThirdPartySetting |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.