summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwebchick2014-06-13 03:13:30 (GMT)
committerwebchick2014-06-13 03:13:30 (GMT)
commita4a887ac91983705094b72b08f4b3ab5dc678bf3 (patch)
tree70d6b8d64d377b7a3f4bc33139c62173fbee8197
parent71acbe3732ef40c4a4c100d11ea257340458d082 (diff)
Issue #1898070 by benjifisher, jjcarrion, LinL, idflood, joelpittet, oshelach, tlattimore, Les Lim, sergey.semashko, jesse.d, bdgreen, jenlampton, Cottser, duellj, artofeclipse, vlad.dancer, steveoliver, EVIIILJ, c4rl, elv, OpenChimp: File.module - Convert theme_ functions to Twig.
-rw-r--r--core/modules/file/file.field.inc85
-rw-r--r--core/modules/file/file.module101
-rw-r--r--core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php30
-rw-r--r--core/modules/file/src/Plugin/views/field/FileMime.php2
-rw-r--r--core/modules/file/templates/file-link.html.twig16
-rw-r--r--core/modules/file/templates/file-managed-file.html.twig17
-rw-r--r--core/modules/file/templates/file-upload-help.html.twig14
-rw-r--r--core/modules/file/templates/file-widget-multiple.html.twig16
-rw-r--r--core/modules/file/templates/file-widget.html.twig17
9 files changed, 167 insertions, 131 deletions
diff --git a/core/modules/file/file.field.inc b/core/modules/file/file.field.inc
index 510966c..75d7160 100644
--- a/core/modules/file/file.field.inc
+++ b/core/modules/file/file.field.inc
@@ -13,39 +13,34 @@ use Drupal\Core\Render\Element;
/**
* Returns HTML for an individual file upload widget.
*
- * @param $variables
- * An associative array containing:
- * - element: A render element representing the widget.
+ * Default template: file-widget.html.twig.
*
- * @ingroup themeable
+ * @param array $variables
+ * An associative array containing:
+ * - element: A render element representing the file.
*/
-function theme_file_widget($variables) {
+function template_preprocess_file_widget(&$variables) {
$element = $variables['element'];
- $output = '';
-
- // The "form-managed-file" class is required for proper Ajax functionality.
- $output .= '<div class="file-widget form-managed-file clearfix">';
if (!empty($element['fids']['#value'])) {
// Add the file size after the file name.
$file = reset($element['#files']);
$element['file_' . $file->id()]['filename']['#suffix'] = ' <span class="file-size">(' . format_size($file->getSize()) . ')</span> ';
}
- $output .= drupal_render_children($element);
- $output .= '</div>';
-
- return $output;
+ $variables['element'] = $element;
+ // The "form-managed-file" class is required for proper Ajax functionality.
+ $variables['attributes'] = array('class' => array('file-widget', 'form-managed-file', 'clearfix'));
}
/**
- * Returns HTML for a group of file upload widgets.
+ * Prepares variables for multi file form widget templates.
+ *
+ * Default template: file-widget-multiple.html.twig.
*
- * @param $variables
+ * @param array $variables
* An associative array containing:
* - element: A render element representing the widgets.
- *
- * @ingroup themeable
*/
-function theme_file_widget_multiple($variables) {
+function template_preprocess_file_widget_multiple(&$variables) {
$element = $variables['element'];
// Special ID and classes for draggable tables.
@@ -133,7 +128,7 @@ function theme_file_widget_multiple($variables) {
);
}
- $build = array(
+ $variables['table'] = array(
'#type' => 'table',
'#header' => $headers,
'#rows' => $rows,
@@ -149,25 +144,22 @@ function theme_file_widget_multiple($variables) {
),
);
- $output = empty($rows) ? '' : drupal_render($build);
- $output .= drupal_render_children($element);
- return $output;
+ $variables['element'] = $element;
}
-
/**
- * Returns HTML for help text based on file upload validators.
+ * Prepares variables for file upload help text templates.
*
- * @param $variables
+ * Default template: file-upload-help.html.twig.
+ *
+ * @param array $variables
* An associative array containing:
* - description: The normal description for this field, specified by the
* user.
* - upload_validators: An array of upload validators as used in
* $element['#upload_validators'].
- *
- * @ingroup themeable
*/
-function theme_file_upload_help($variables) {
+function template_preprocess_file_upload_help(&$variables) {
$description = $variables['description'];
$upload_validators = $variables['upload_validators'];
$cardinality = $variables['cardinality'];
@@ -209,7 +201,7 @@ function theme_file_upload_help($variables) {
}
}
- return implode(' ', $descriptions);
+ $variables['descriptions'] = $descriptions;
}
/**
@@ -235,38 +227,3 @@ function file_field_find_file_reference_column(FieldDefinitionInterface $field)
}
return FALSE;
}
-
-/**
- * Returns HTML for a file attachments table.
- *
- * @param $variables
- * An associative array containing:
- * - items: field values, as a FileFieldItemList object.
- *
- * @ingroup themeable
- */
-function theme_file_formatter_table($variables) {
- $header = array(t('Attachment'), t('Size'));
- $rows = array();
- foreach ($variables['items'] as $delta => $item) {
- if ($item->isDisplayed() && $item->entity) {
- $rows[] = array(
- array(
- 'data' => array(
- '#theme' => 'file_link',
- '#file' => $item->entity,
- ),
- ),
- format_size($item->entity->getSize()),
- );
- }
- }
-
- $build = array(
- '#type' => 'table',
- '#header' => $header,
- '#rows' => $rows,
- );
-
- return empty($rows) ? '' : drupal_render($build);
-}
diff --git a/core/modules/file/file.module b/core/modules/file/file.module
index d7bb3ee..e1f4b28 100644
--- a/core/modules/file/file.module
+++ b/core/modules/file/file.module
@@ -583,29 +583,31 @@ function file_get_content_headers(File $file) {
*/
function file_theme() {
return array(
- // file.module.
+ // From file.module.
'file_link' => array(
'variables' => array('file' => NULL, 'icon_directory' => NULL, 'description' => NULL, 'attributes' => array()),
- ),
- 'file_icon' => array(
- 'variables' => array('file' => NULL, 'icon_directory' => NULL),
+ 'template' => 'file-link',
),
'file_managed_file' => array(
'render element' => 'element',
+ 'template' => 'file-managed-file',
),
- // file.field.inc.
+ // From file.field.inc.
'file_widget' => array(
'render element' => 'element',
+ 'template' => 'file-widget',
+ 'file' => 'file.field.inc',
),
'file_widget_multiple' => array(
'render element' => 'element',
- ),
- 'file_formatter_table' => array(
- 'variables' => array('items' => NULL),
+ 'template' => 'file-widget-multiple',
+ 'file' => 'file.field.inc',
),
'file_upload_help' => array(
'variables' => array('description' => NULL, 'upload_validators' => NULL, 'cardinality' => NULL),
+ 'template' => 'file-upload-help',
+ 'file' => 'file.field.inc',
),
);
}
@@ -1534,32 +1536,25 @@ function file_managed_file_save_upload($element, array &$form_state) {
}
/**
- * Returns HTML for a managed file element.
+ * Prepares variables for file form widget templates.
+ *
+ * Default template: file-managed-file.html.twig.
*
- * @param $variables
+ * @param array $variables
* An associative array containing:
* - element: A render element representing the file.
- *
- * @ingroup themeable
*/
-function theme_file_managed_file($variables) {
+function template_preprocess_file_managed_file(&$variables) {
$element = $variables['element'];
- $attributes = array();
+ $variables['attributes'] = array();
if (isset($element['#id'])) {
- $attributes['id'] = $element['#id'];
+ $variables['attributes']['id'] = $element['#id'];
}
if (!empty($element['#attributes']['class'])) {
- $attributes['class'] = (array) $element['#attributes']['class'];
+ $variables['attributes']['class'] = (array) $element['#attributes']['class'];
}
- $attributes['class'][] = 'form-managed-file';
-
- // This wrapper is required to apply JS behaviors and CSS styling.
- $output = '';
- $output .= '<div' . new Attribute($attributes) . '>';
- $output .= drupal_render_children($element);
- $output .= '</div>';
- return $output;
+ $variables['attributes']['class'][] = 'form-managed-file';
}
/**
@@ -1598,24 +1593,34 @@ function file_managed_file_pre_render($element) {
}
/**
- * Returns HTML for a link to a file.
+ * Prepares variables for file link templates.
+ *
+ * Default template: file-link.html.twig.
*
- * @param $variables
+ * @param array $variables
* An associative array containing:
* - file: A file object to which the link will be created.
* - icon_directory: (optional) A path to a directory of icons to be used for
- * files. Defaults to the value of the "icon.directory"
- * variable.
+ * files. Defaults to the value of the "icon.directory" variable.
* - description: A description to be displayed instead of the filename.
* - attributes: An associative array of attributes to be placed in the a tag.
- *
- * @ingroup themeable
*/
-function theme_file_link($variables) {
+function template_preprocess_file_link(&$variables) {
$file = $variables['file'];
$options = array(
'attributes' => $variables['attributes'],
);
+ $icon_directory = $variables['icon_directory'];
+
+ $url = file_create_url($file->getFileUri());
+ $file_entity = ($file instanceof File) ? $file : file_load($file->fid);
+ $variables['icon'] = array(
+ '#theme' => 'image__file_icon',
+ '#uri' => file_icon_url($file_entity, $icon_directory),
+ '#alt' => '',
+ '#title' => check_plain($file_entity->getFilename()),
+ '#attributes' => array('class' => 'file-icon'),
+ );
// Set options as per anchor format described at
// http://microformats.org/wiki/file-format-examples
@@ -1623,41 +1628,15 @@ function theme_file_link($variables) {
// Use the description as the link text if available.
if (empty($variables['description'])) {
- $link_text = $file->getFilename();
+ $link_text = $file_entity->getFilename();
}
else {
$link_text = $variables['description'];
- $options['attributes']['title'] = String::checkPlain($file->getFilename());
+ $options['attributes']['title'] = String::checkPlain($file_entity->getFilename());
}
- $file_icon = array(
- '#theme' => 'file_icon',
- '#file' => $file,
- '#icon_directory' => $variables['icon_directory'],
- );
-
- return '<span class="file">' . drupal_render($file_icon) . ' ' . l($link_text, file_create_url($file->getFileUri()), $options) . '</span>';
-}
-
-/**
- * Returns HTML for an image with an appropriate icon for the given file.
- *
- * @param $variables
- * An associative array containing:
- * - file: A file entity for which to make an icon.
- * - icon_directory: (optional) A path to a directory of icons to be used for
- * files. Defaults to the value of the "icon.directory"
- * variable.
- *
- * @ingroup themeable
- */
-function theme_file_icon($variables) {
- $file = $variables['file'];
- $icon_directory = $variables['icon_directory'];
-
- $mime = String::checkPlain($file->getMimeType());
- $icon_url = file_icon_url($file, $icon_directory);
- return '<img class="file-icon" alt="" title="' . $mime . '" src="' . $icon_url . '" />';
+ $variables['link'] = l($link_text, $url, $options);
+ $variables['attributes'] = array('class' => array('file'));
}
/**
diff --git a/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php b/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php
index 5f9d4bf..b5346c6 100644
--- a/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php
+++ b/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php
@@ -29,11 +29,31 @@ class TableFormatter extends FileFormatterBase {
$elements = array();
if (!$items->isEmpty()) {
- // Display all values in a single element.
- $elements[0] = array(
- '#theme' => 'file_formatter_table',
- '#items' => $items,
- );
+
+ $header = array(t('Attachment'), t('Size'));
+ $rows = array();
+ foreach ($items as $delta => $item) {
+ if ($item->isDisplayed() && $item->entity) {
+ $rows[] = array(
+ array(
+ 'data' => array(
+ '#theme' => 'file_link',
+ '#file' => $item->entity,
+ ),
+ ),
+ array('data' => format_size($item->entity->getSize())),
+ );
+ }
+ }
+
+ $elements[0] = array();
+ if (!empty($rows)) {
+ $elements[0] = array(
+ '#theme' => 'table__file_formatter_table',
+ '#header' => $header,
+ '#rows' => $rows,
+ );
+ }
}
return $elements;
diff --git a/core/modules/file/src/Plugin/views/field/FileMime.php b/core/modules/file/src/Plugin/views/field/FileMime.php
index ac98724..fd1c355 100644
--- a/core/modules/file/src/Plugin/views/field/FileMime.php
+++ b/core/modules/file/src/Plugin/views/field/FileMime.php
@@ -40,7 +40,7 @@ class FileMime extends File {
$data = $values->{$this->field_alias};
if (!empty($this->options['filemime_image']) && $data !== NULL && $data !== '') {
$file_icon = array(
- '#theme' => 'file_icon',
+ '#theme' => 'image__file_icon',
'#file' => $values->_entity,
);
$data = drupal_render($file_icon);
diff --git a/core/modules/file/templates/file-link.html.twig b/core/modules/file/templates/file-link.html.twig
new file mode 100644
index 0000000..fbbf08b
--- /dev/null
+++ b/core/modules/file/templates/file-link.html.twig
@@ -0,0 +1,16 @@
+{#
+/**
+ * @file
+ * Default theme implementation for a link to a file.
+ *
+ * Available variables:
+ * - attributes: The HTML attributes for the containing element.
+ * - link: A link to the file.
+ * - icon: The icon image representing the file type.
+ *
+ * @see template_preprocess_file_link()
+ *
+ * @ingroup themeable
+ */
+#}
+<span{{ attributes }}>{{ icon }} {{ link }}</span>
diff --git a/core/modules/file/templates/file-managed-file.html.twig b/core/modules/file/templates/file-managed-file.html.twig
new file mode 100644
index 0000000..9a33ae1
--- /dev/null
+++ b/core/modules/file/templates/file-managed-file.html.twig
@@ -0,0 +1,17 @@
+{#
+/**
+ * @file
+ * Default theme implementation to display a file form widget.
+ *
+ * Available variables:
+ * - element: Form element for the file upload.
+ * - attributes: HTML attributes for the containing element.
+ *
+ * @see template_preprocess_file_managed_file()
+ *
+ * @ingroup themeable
+ */
+#}
+<div{{ attributes }}>
+ {{ element }}
+</div>
diff --git a/core/modules/file/templates/file-upload-help.html.twig b/core/modules/file/templates/file-upload-help.html.twig
new file mode 100644
index 0000000..fe4d194
--- /dev/null
+++ b/core/modules/file/templates/file-upload-help.html.twig
@@ -0,0 +1,14 @@
+{#
+/**
+ * @file
+ * Default theme implementation to display help text for file fields.
+ *
+ * Available variables:
+ * - descriptions: Lines of help text for uploading a file.
+ *
+ * @see template_preprocess_file_upload_help()
+ *
+ * @ingroup themeable
+ */
+#}
+{{ descriptions|join('<br />') }}
diff --git a/core/modules/file/templates/file-widget-multiple.html.twig b/core/modules/file/templates/file-widget-multiple.html.twig
new file mode 100644
index 0000000..25534a5
--- /dev/null
+++ b/core/modules/file/templates/file-widget-multiple.html.twig
@@ -0,0 +1,16 @@
+{#
+/**
+ * @file
+ * Default theme implementation to display a multi file form widget.
+ *
+ * Available variables:
+ * - table: Table of previously uploaded files.
+ * - element: The form element for uploading another file.
+ *
+ * @see template_preprocess_file_widget_multiple()
+ *
+ * @ingroup themeable
+ */
+#}
+{{ table }}
+{{ element }}
diff --git a/core/modules/file/templates/file-widget.html.twig b/core/modules/file/templates/file-widget.html.twig
new file mode 100644
index 0000000..892ed3d
--- /dev/null
+++ b/core/modules/file/templates/file-widget.html.twig
@@ -0,0 +1,17 @@
+{#
+/**
+ * @file
+ * Default theme implementation to display a file widget.
+ *
+ * Available variables:
+ * - element: Form element for the managed file.
+ * - attributes: Remaining HTML attributes for the containing element.
+ *
+ * @see template_preprocess_file_widget()
+ *
+ * @ingroup themeable
+ */
+#}
+<div{{ attributes }}>
+ {{ element }}
+</div>