class Document in Media entity document 8
Provides media type plugin for Document.
Plugin annotation
@MediaType(
id = "document",
label = @Translation("Document"),
description = @Translation("Provides business logic and metadata for local documents."),
allowed_field_types = {
"file"
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\media_entity\MediaTypeBase implements ContainerFactoryPluginInterface, MediaTypeInterface uses StringTranslationTrait
- class \Drupal\media_entity_document\Plugin\MediaEntity\Type\Document
- class \Drupal\media_entity\MediaTypeBase implements ContainerFactoryPluginInterface, MediaTypeInterface uses StringTranslationTrait
Expanded class hierarchy of Document
File
- src/
Plugin/ MediaEntity/ Type/ Document.php, line 21
Namespace
Drupal\media_entity_document\Plugin\MediaEntity\TypeView source
class Document extends MediaTypeBase {
/**
* {@inheritdoc}
*/
public function providedFields() {
return [
'mime' => $this
->t('File MIME'),
'size' => $this
->t('Size'),
];
}
/**
* {@inheritdoc}
*/
public function getField(MediaInterface $media, $name) {
$source_field = $this->configuration['source_field'];
// Get the file document.
/** @var \Drupal\file\FileInterface $file */
$file = $media->{$source_field}->entity;
// Return the field.
switch ($name) {
case 'mime':
return !$file->filemime
->isEmpty() ? $file
->getMimeType() : FALSE;
case 'size':
$size = $file
->getSize();
return $size ? $size : FALSE;
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function thumbnail(MediaInterface $media) {
$source_field = $this->configuration['source_field'];
/** @var \Drupal\file\FileInterface $file */
$file = $media->{$source_field}->entity;
if ($file) {
$mimetype = $file
->getMimeType();
$mimetype = explode('/', $mimetype);
$thumbnail = $this->config
->get('icon_base') . "/{$mimetype[0]}-{$mimetype[1]}.png";
if (!is_file($thumbnail)) {
$thumbnail = $this->config
->get('icon_base') . "/{$mimetype[1]}.png";
if (!is_file($thumbnail)) {
$thumbnail = $this->config
->get('icon_base') . '/document.png';
}
}
}
else {
$thumbnail = $this->config
->get('icon_base') . '/document.png';
}
return $thumbnail;
}
/**
* {@inheritdoc}
*/
public function getDefaultName(MediaInterface $media) {
// The default name will be the filename of the source_field, if present.
$source_field = $this->configuration['source_field'];
/** @var \Drupal\file\FileInterface $file */
if (!empty($source_field) && ($file = $media->{$source_field}->entity)) {
return $file
->getFilename();
}
return parent::getDefaultName($media);
}
/**
* {@inheritdoc}
*/
protected function createSourceFieldStorage() {
return $this->entityTypeManager
->getStorage('field_storage_config')
->create([
'entity_type' => 'media',
'field_name' => $this
->getSourceFieldName(),
'type' => 'file',
]);
}
/**
* {@inheritdoc}
*/
protected function createSourceField(MediaBundleInterface $bundle) {
return $this->entityTypeManager
->getStorage('field_config')
->create([
'field_storage' => $this
->getSourceFieldStorage(),
'bundle' => $bundle
->id(),
'required' => TRUE,
'label' => $this
->t('Source file'),
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Document:: |
protected | function | ||
Document:: |
protected | function | ||
Document:: |
public | function |
Provide a default name for the media. Overrides MediaTypeBase:: |
|
Document:: |
public | function |
Gets a media-related field/value. Overrides MediaTypeInterface:: |
|
Document:: |
public | function |
Gets list of fields provided by this plugin. Overrides MediaTypeInterface:: |
|
Document:: |
public | function |
Gets thumbnail image. Overrides MediaTypeInterface:: |
|
MediaTypeBase:: |
protected | property | Media entity image config object. | |
MediaTypeBase:: |
protected | property | The entity field manager service. | |
MediaTypeBase:: |
protected | property | The entity type manager service. | |
MediaTypeBase:: |
protected | property | Plugin label. | |
MediaTypeBase:: |
public | function |
Attaches type-specific constraints to media. Overrides MediaTypeInterface:: |
|
MediaTypeBase:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
1 |
MediaTypeBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
MediaTypeBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
MediaTypeBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurablePluginInterface:: |
1 |
MediaTypeBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurablePluginInterface:: |
|
MediaTypeBase:: |
public | function |
Gets the default thumbnail image. Overrides MediaTypeInterface:: |
|
MediaTypeBase:: |
public | function |
Returns the display label. Overrides MediaTypeInterface:: |
|
MediaTypeBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface:: |
|
MediaTypeBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
MediaTypeBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
MediaTypeBase:: |
public | function |
Constructs a new class instance. Overrides PluginBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |