diff -u b/core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php b/core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php --- b/core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php @@ -6,64 +6,12 @@ use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Plugin\ContainerFactoryPluginInterface; -use Drupal\Core\Render\RendererInterface; use Drupal\Core\Template\Attribute; -use Drupal\file\Entity\File; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Base class for media file formatter. */ -abstract class FileMediaFormatterBase extends FileFormatterBase implements ContainerFactoryPluginInterface, FileMediaFormatterInterface { - - /** - * The renderer service. - * - * @var \Drupal\Core\Render\RendererInterface - */ - protected $renderer; - - /** - * Constructs a FileMediaFormatter instance. - * - * @param string $plugin_id - * The plugin_id for the formatter. - * @param mixed $plugin_definition - * The plugin implementation definition. - * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition - * The definition of the field to which the formatter is associated. - * @param array $settings - * The formatter settings. - * @param string $label - * The formatter label display setting. - * @param string $view_mode - * The view mode. - * @param array $third_party_settings - * Any third party settings settings. - * @param \Drupal\Core\Render\RendererInterface $renderer - * The renderer service. - */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, RendererInterface $renderer) { - parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); - $this->renderer = $renderer; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $plugin_id, - $plugin_definition, - $configuration['field_definition'], - $configuration['settings'], - $configuration['label'], - $configuration['view_mode'], - $configuration['third_party_settings'], - $container->get('renderer') - ); - } +abstract class FileMediaFormatterBase extends FileFormatterBase implements FileMediaFormatterInterface { /** * {@inheritdoc} @@ -81,32 +29,32 @@ * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { - $element['controls'] = [ - '#title' => $this->t('Show audio controls'), - '#type' => 'checkbox', - '#default_value' => $this->getSetting('controls'), - ]; - $element['autoplay'] = [ - '#title' => $this->t('Autoplay'), - '#type' => 'checkbox', - '#default_value' => $this->getSetting('autoplay'), - ]; - $element['loop'] = [ - '#title' => $this->t('Loop'), - '#type' => 'checkbox', - '#default_value' => $this->getSetting('loop'), - ]; - $element['multiple_file_behavior'] = [ - '#title' => $this->t('Display of multiple files'), - '#type' => 'radios', - '#options' => [ - 'tags' => $this->t('Use multiple @tag tags, each with a single source.', ['@tag' => '<' . static::getMimeType() . '>']), - 'sources' => $this->t('Use multiple sources within a single @tag tag.', ['@tag' => '<' . static::getMimeType() . '>']), + return [ + 'controls' => [ + '#title' => $this->t('Show audio 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_behavior' => [ + '#title' => $this->t('Display of multiple files'), + '#type' => 'radios', + '#options' => [ + 'tags' => $this->t('Use multiple @tag tags, each with a single source.', ['@tag' => '<' . static::getMimeType() . '>']), + 'sources' => $this->t('Use multiple sources within a single @tag tag.', ['@tag' => '<' . static::getMimeType() . '>']), + ], + '#default_value' => $this->getSetting('multiple_file_behavior'), ], - '#default_value' => $this->getSetting('multiple_file_behavior'), ]; - - return $element; } /** @@ -116,7 +64,6 @@ if (!parent::isApplicable($field_definition)) { return FALSE; } - /** @var \Symfony\Component\HttpFoundation\File\MimeType\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'))); @@ -128,7 +75,6 @@ return TRUE; } } - return FALSE; } @@ -151,7 +97,6 @@ $elements = []; $source_files = $this->getSourceFiles($items, $langcode); - if (empty($source_files)) { return $elements; } @@ -162,10 +107,14 @@ '#theme' => 'file_' . static::getMimeType(), '#attributes' => $attributes, '#files' => $files, + '#cache' => ['tags' => []], ]; + + $cache_tags = []; foreach ($files as $file) { - $this->renderer->addCacheableDependency($elements[$delta], $file['file']); + $cache_tags = array_merge($cache_tags, $file['file']->getCacheTags()); } + $elements[$delta]['#cache']['tags'] = $cache_tags; } return $elements; @@ -213,7 +162,8 @@ * The language code of the referenced entities to display. * * @return array - * Numeric bases array, which again contains an associative array with the following key/values: + * Numeric based array, which again contains an associative array with the + * following key/values: * - file => \Drupal\file\Entity\File * - source_attributes => \Drupal\Core\Template\Attribute */ @@ -246,7 +196,6 @@ } } } - return $source_files; } diff -u b/core/modules/file/src/Plugin/Field/FieldFormatter/FileVideoFormatter.php b/core/modules/file/src/Plugin/Field/FieldFormatter/FileVideoFormatter.php --- b/core/modules/file/src/Plugin/Field/FieldFormatter/FileVideoFormatter.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/FileVideoFormatter.php @@ -40,35 +40,33 @@ * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { - $element = parent::settingsForm($form, $form_state); - - $element['muted'] = [ - '#title' => $this->t('Muted'), - '#type' => 'checkbox', - '#default_value' => $this->getSetting('muted'), - ]; - $element['width'] = [ - '#type' => 'number', - '#title' => $this->t('Width'), - '#default_value' => $this->getSetting('width'), - '#size' => 5, - '#maxlength' => 5, - '#field_suffix' => $this->t('pixels'), - '#min' => 0, - '#required' => TRUE, + return parent::settingsForm($form, $form_state) + [ + 'muted' => [ + '#title' => $this->t('Muted'), + '#type' => 'checkbox', + '#default_value' => $this->getSetting('muted'), + ], + 'width' => [ + '#type' => 'number', + '#title' => $this->t('Width'), + '#default_value' => $this->getSetting('width'), + '#size' => 5, + '#maxlength' => 5, + '#field_suffix' => $this->t('pixels'), + '#min' => 0, + '#required' => TRUE, + ], + 'height' => [ + '#type' => 'number', + '#title' => $this->t('Height'), + '#default_value' => $this->getSetting('height'), + '#size' => 5, + '#maxlength' => 5, + '#field_suffix' => $this->t('pixels'), + '#min' => 0, + '#required' => TRUE, + ], ]; - $element['height'] = [ - '#type' => 'number', - '#title' => $this->t('Height'), - '#default_value' => $this->getSetting('height'), - '#size' => 5, - '#maxlength' => 5, - '#field_suffix' => $this->t('pixels'), - '#min' => 0, - '#required' => TRUE, - ]; - - return $element; } /**