From cfd993492ffcde76edbcb3f4e1adf7fecd6b1b5c Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 10 Jun 2019 19:42:39 +0300 Subject: [PATCH] Issue #3033279 by lauriii, mandclu, Eli-T: Move markup from template_preprocess_image_widget to a template --- core/modules/file/file.module | 1 + core/modules/file/templates/file-link.html.twig | 5 +++++ core/modules/image/image.field.inc | 5 ----- core/themes/classy/templates/field/file-link.html.twig | 2 ++ core/themes/stable/stable.theme | 10 ++++++++++ core/themes/stable/templates/field/file-link.html.twig | 2 ++ 6 files changed, 20 insertions(+), 5 deletions(-) diff --git a/core/modules/file/file.module b/core/modules/file/file.module index 8eef9c40dc10..53483c16f9fb 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -1502,6 +1502,7 @@ function template_preprocess_file_link(&$variables) { // Set file classes to the options array. $variables['attributes'] = new Attribute($variables['attributes']); $variables['attributes']->addClass($classes); + $variables['file_size'] = format_size($file->getSize()); $variables['link'] = \Drupal::l($link_text, Url::fromUri($url, $options)); } diff --git a/core/modules/file/templates/file-link.html.twig b/core/modules/file/templates/file-link.html.twig index 44cbd7f6892e..9fb2fa67c560 100644 --- a/core/modules/file/templates/file-link.html.twig +++ b/core/modules/file/templates/file-link.html.twig @@ -6,6 +6,7 @@ * Available variables: * - attributes: The HTML attributes for the containing element. * - link: A link to the file. + * - file_size: The size of the file. * * @see template_preprocess_file_link() * @@ -13,3 +14,7 @@ */ #} {{ link }} +{% if file_size %} + {# @todo remove class before Drupal 9.0.0 #} + ({{ file_size }}) +{% endif %} diff --git a/core/modules/image/image.field.inc b/core/modules/image/image.field.inc index bf69b90442ec..1afdde2fdda8 100644 --- a/core/modules/image/image.field.inc +++ b/core/modules/image/image.field.inc @@ -21,11 +21,6 @@ function template_preprocess_image_widget(&$variables) { $variables['attributes'] = ['class' => ['image-widget', 'js-form-managed-file', 'form-managed-file', 'clearfix']]; - if (!empty($element['fids']['#value'])) { - $file = reset($element['#files']); - $element['file_' . $file->id()]['filename']['#suffix'] = ' (' . format_size($file->getSize()) . ') '; - } - $variables['data'] = []; foreach (Element::children($element) as $child) { $variables['data'][$child] = $element[$child]; diff --git a/core/themes/classy/templates/field/file-link.html.twig b/core/themes/classy/templates/field/file-link.html.twig index 11170c0c0f65..78384e653307 100644 --- a/core/themes/classy/templates/field/file-link.html.twig +++ b/core/themes/classy/templates/field/file-link.html.twig @@ -7,8 +7,10 @@ * - attributes: The HTML attributes for the containing element. * - link: A link to the file. * - icon: The icon image representing the file type. + * - file_size: The size of the file. * * @see template_preprocess_file_link() + * @see stable_preprocess_image_widget() */ #} {{ attach_library('classy/file') }} diff --git a/core/themes/stable/stable.theme b/core/themes/stable/stable.theme index 456a4ad39fab..5812f890a185 100644 --- a/core/themes/stable/stable.theme +++ b/core/themes/stable/stable.theme @@ -23,3 +23,13 @@ function stable_preprocess_links(&$variables) { } } } + +/** + * Implements hook_preprocess_image_widget(). + */ +function stable_preprocess_image_widget(&$variables) { + if (!empty($variables['element']['fids']['#value'])) { + $file = reset($variables['element']['#files']); + $variables['data']['file_' . $file->id()]['filename']['#suffix'] = ' (' . format_size($file->getSize()) . ') '; + } +} diff --git a/core/themes/stable/templates/field/file-link.html.twig b/core/themes/stable/templates/field/file-link.html.twig index 0424830f574b..7da6aa466105 100644 --- a/core/themes/stable/templates/field/file-link.html.twig +++ b/core/themes/stable/templates/field/file-link.html.twig @@ -6,8 +6,10 @@ * Available variables: * - attributes: The HTML attributes for the containing element. * - link: A link to the file. + * - file_size: The size of the file. * * @see template_preprocess_file_link() + * @see stable_preprocess_image_widget() */ #} {{ link }}