diff --git a/core/modules/file/file.field.inc b/core/modules/file/file.field.inc
index 9d58d1d..83427ef 100644
--- a/core/modules/file/file.field.inc
+++ b/core/modules/file/file.field.inc
@@ -7,6 +7,7 @@
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityInterface;
+use Drupal\Core\Template\Attribute;
/**
* Implements hook_field_info().
@@ -579,40 +580,36 @@ function file_field_widget_submit($form, &$form_state) {
}
/**
- * Returns HTML for an individual file upload widget.
+ * Preprocess variables to display a file form widget.
*
- * @param $variables
+ * @param array $variables
* An associative array containing:
- * - element: A render element representing the widget.
+ * - element: A render element representing the file.
+ * - element_attributes: An Array of HTML attributes.
*
- * @ingroup themeable
+ * @see file-widget.html.twig
*/
-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 .= '
';
if ($element['fid']['#value'] != 0) {
// Add the file size after the file name.
- $element['filename']['#markup'] .= ' (' . format_size($element['#file']->filesize) . ') ';
+ $element['filename']['#markup'] .= ' array('file-size'))) . '>(' . format_size($element['#file']->filesize) . ') ';
}
- $output .= drupal_render_children($element);
- $output .= '
';
-
- return $output;
+ $variables['element'] = drupal_render_children($element);
+ // The "form-managed-file" class is required for proper Ajax functionality.
+ $variables['element_attributes'] = new Attribute(array('class' => array('file-widget', 'form-managed-file', 'clearfix')));
}
/**
- * Returns HTML for a group of file upload widgets.
+ * Preprocess variables to display a multi file form widget.
*
- * @param $variables
+ * @param array $variables
* An associative array containing:
* - element: A render element representing the widgets.
*
- * @ingroup themeable
+ * @see file-widget-multiple.html.twig
*/
-function theme_file_widget_multiple($variables) {
+function template_preprocess_file_widget_multiple(&$variables) {
$element = $variables['element'];
// Special ID and classes for draggable tables.
@@ -694,6 +691,7 @@ function theme_file_widget_multiple($variables) {
}
$row[] = $weight;
$row[] = $operations;
+
$rows[] = array(
'data' => $row,
'class' => isset($widget['#attributes']['class']) ? array_merge($widget['#attributes']['class'], array('draggable')) : array('draggable'),
@@ -702,26 +700,28 @@ function theme_file_widget_multiple($variables) {
drupal_add_tabledrag($table_id, 'order', 'sibling', $weight_class);
- $output = '';
- $output = empty($rows) ? '' : theme('table', array('header' => $headers, 'rows' => $rows, 'attributes' => array('id' => $table_id)));
- $output .= drupal_render_children($element);
- return $output;
+ $variables['table'] = array(
+ '#theme' => 'table',
+ '#header' => $headers,
+ '#rows' => $rows,
+ '#attributes' => array('id' => $table_id),
+ );
+ $variables['children'] = drupal_render_children($element);
}
-
/**
- * Returns HTML for help text based on file upload validators.
+ * Preprocess variables for help text based on file upload validators.
*
- * @param $variables
+ * @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
+ * @see file-upload-help.html.twig
*/
-function theme_file_upload_help($variables) {
+function template_preprocess_file_upload_help(&$variables) {
$description = $variables['description'];
$upload_validators = $variables['upload_validators'];
@@ -731,29 +731,30 @@ function theme_file_upload_help($variables) {
$descriptions[] = $description;
}
if (isset($upload_validators['file_validate_size'])) {
- $descriptions[] = t('Files must be less than !size.', array('!size' => '' . format_size($upload_validators['file_validate_size'][0]) . ''));
+ $descriptions[] = format_string('Files must be less than !size.', array('!size' => '' . format_size($upload_validators['file_validate_size'][0]) . ''));
}
if (isset($upload_validators['file_validate_extensions'])) {
- $descriptions[] = t('Allowed file types: !extensions.', array('!extensions' => '' . check_plain($upload_validators['file_validate_extensions'][0]) . ''));
+ $descriptions[] = format_string('Allowed file types: !extensions.', array('!extensions' => '' . check_plain($upload_validators['file_validate_extensions'][0]) . ''));
}
+
if (isset($upload_validators['file_validate_image_resolution'])) {
$max = $upload_validators['file_validate_image_resolution'][0];
$min = $upload_validators['file_validate_image_resolution'][1];
if ($min && $max && $min == $max) {
- $descriptions[] = t('Images must be exactly !size pixels.', array('!size' => '' . $max . ''));
+ $descriptions[] = format_string('Images must be exactly !size pixels.', array('!size' => '' . $max . ''));
}
elseif ($min && $max) {
- $descriptions[] = t('Images must be between !min and !max pixels.', array('!min' => '' . $min . '', '!max' => '' . $max . ''));
+ $descriptions[] = format_string('Images must be between !min and !max pixels.', array('!min' => '' . $min . '', '!max' => '' . $max . ''));
}
elseif ($min) {
- $descriptions[] = t('Images must be larger than !min pixels.', array('!min' => '' . $min . ''));
+ $descriptions[] = format_string('Images must be larger than !min pixels.', array('!min' => '' . $min . ''));
}
elseif ($max) {
- $descriptions[] = t('Images must be smaller than !max pixels.', array('!max' => '' . $max . ''));
+ $descriptions[] = format_string('Images must be smaller than !max pixels.', array('!max' => '' . $max . ''));
}
}
- return implode('
', $descriptions);
+ $variables['descriptions'] = $descriptions;
}
/**
@@ -780,23 +781,35 @@ function file_field_find_file_reference_column($field) {
}
/**
- * Returns HTML for a file attachments table.
+ * Preprocess variables to display a file attachments table.
*
- * @param $variables
+ * @param array $variables
* An associative array containing:
* - items: An array of file attachments.
*
- * @ingroup themeable
+ * @see file-formatter-table.html.twig
*/
-function theme_file_formatter_table($variables) {
+function template_preprocess_file_formatter_table(&$variables) {
$header = array(t('Attachment'), t('Size'));
$rows = array();
foreach ($variables['items'] as $delta => $item) {
$rows[] = array(
- theme('file_link', array('file' => (object) $item)),
- format_size($item['filesize']),
+ array(
+ 'data' => array(
+ '#theme' => 'file_link',
+ '#file' => (object) $item,
+ ),
+ ),
+ array('data' => format_size($item['filesize'])),
);
}
- return empty($rows) ? '' : theme('table', array('header' => $header, 'rows' => $rows));
+ $variables['content'] = array();
+ if (!empty($rows)) {
+ $variables['content'] = array(
+ '#theme' => 'table',
+ '#header' => $header,
+ '#rows' => $rows,
+ );
+ }
}
diff --git a/core/modules/file/file.module b/core/modules/file/file.module
index 75c4f84..5ab02b9 100644
--- a/core/modules/file/file.module
+++ b/core/modules/file/file.module
@@ -588,29 +588,36 @@ 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),
+ 'template' => 'file-link',
),
'file_icon' => array(
'variables' => array('file' => NULL, 'icon_directory' => NULL),
+ 'template' => 'file-icon',
),
'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_widget_multiple' => array(
'render element' => 'element',
+ 'template' => 'file-widget-multiple'
),
'file_formatter_table' => array(
'variables' => array('items' => NULL),
+ 'template' => 'file-formatter-table',
),
'file_upload_help' => array(
'variables' => array('description' => NULL, 'upload_validators' => NULL),
+ 'template' => 'file-upload-help',
),
);
}
@@ -1167,15 +1174,16 @@ function file_managed_file_save_upload($element) {
}
/**
- * Returns HTML for a managed file element.
+ * Preprocess variables to display a file form widget.
*
- * @param $variables
+ * @param array $variables
* An associative array containing:
* - element: A render element representing the file.
+ * - element_attributes: An Array of HTML attributes.
*
- * @ingroup themeable
+ * @see file-managed-file.html.twig
*/
-function theme_file_managed_file($variables) {
+function template_preprocess_file_managed_file(&$variables) {
$element = $variables['element'];
$attributes = array();
@@ -1187,12 +1195,8 @@ function theme_file_managed_file($variables) {
}
$attributes['class'][] = 'form-managed-file';
- // This wrapper is required to apply JS behaviors and CSS styling.
- $output = '';
- $output .= '';
- $output .= drupal_render_children($element);
- $output .= '
';
- return $output;
+ $variables['attributes'] = new Attribute($attributes);
+ $variables['element'] = drupal_render_children($element);
}
/**
@@ -1230,24 +1234,26 @@ function file_managed_file_pre_render($element) {
}
/**
- * Returns HTML for a link to a file.
+ * Preprocess variables for a link to a file.
*
- * @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.
*
- * @ingroup themeable
+ * @see file-link.html.twig
*/
-function theme_file_link($variables) {
+function template_preprocess_file_link(&$variables) {
$file = $variables['file'];
$icon_directory = $variables['icon_directory'];
$url = file_create_url($file->uri);
- // theme_file_icon() requires a file entity, make sure it gets one.
- $icon = theme('file_icon', array('file' => ($file instanceof File) ? $file : file_load($file->fid), 'icon_directory' => $icon_directory));
+ $variables['icon'] = array(
+ '#theme' => 'file_icon',
+ // theme_file_icon() requires a file entity, make sure it gets one.
+ '#file' => ($file instanceof File) ? $file : file_load($file->fid), 'icon_directory' => $icon_directory,
+ );
// Set options as per anchor format described at
// http://microformats.org/wiki/file-format-examples
@@ -1266,28 +1272,27 @@ function theme_file_link($variables) {
$options['attributes']['title'] = check_plain($file->filename);
}
- return '' . $icon . ' ' . l($link_text, $url, $options) . '';
+ $variables['link'] = l($link_text, $url, $options);
+ $variables['attributes'] = new Attribute(array('class' => array('file')));
}
/**
- * Returns HTML for an image with an appropriate icon for the given file.
+ * Preprocess variables for file_icon theme.
*
- * @param $variables
+ * @param array $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.
+ * files. Defaults to the value of the "icon.directory" variable.
*
- * @ingroup themeable
+ * @see file-icon.html.twig
*/
-function theme_file_icon($variables) {
+function template_preprocess_file_icon(&$variables) {
$file = $variables['file'];
$icon_directory = $variables['icon_directory'];
- $mime = check_plain($file->filemime);
- $icon_url = file_icon_url($file, $icon_directory);
- return '';
+ $variables['mime'] = check_plain($file->filename);
+ $variables['icon_url'] = file_icon_url($file, $icon_directory);
}
/**
diff --git a/core/modules/file/templates/file-formatter-table.html.twig b/core/modules/file/templates/file-formatter-table.html.twig
new file mode 100644
index 0000000..e51cfd8
--- /dev/null
+++ b/core/modules/file/templates/file-formatter-table.html.twig
@@ -0,0 +1,15 @@
+{#
+/**
+* @file
+* Default theme implementation to display a file attachments table.
+*
+* Available variables:
+* - content: File form element HTML.
+*
+* @see template_preprocess()
+* @see template_preprocess_file_formatter_table()
+*
+* @ingroup themeable
+*/
+#}
+{{ content }}
diff --git a/core/modules/file/templates/file-icon.html.twig b/core/modules/file/templates/file-icon.html.twig
new file mode 100755
index 0000000..9921b7e
--- /dev/null
+++ b/core/modules/file/templates/file-icon.html.twig
@@ -0,0 +1,18 @@
+{#
+/**
+ * @file
+ * Default theme implementation for an image with an appropriate icon for the given file.
+ *
+ * Available variables:
+ * - 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 "file_icon_directory" variable.
+ *
+ * @see template_preprocess()
+ * @see template_preprocess_file_icon()
+ *
+ * @ingroup themeable
+ */
+ @todo: rename icon_url to src in preprocess as per http://drupal.org/node/1820688.
+ #}
+
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..de8b117
--- /dev/null
+++ b/core/modules/file/templates/file-link.html.twig
@@ -0,0 +1,17 @@
+{#
+/**
+ * @file
+ * Default theme implementation for a link to a file.
+ *
+ * Available variables:
+ * - attributes: An associative array containing attributes of an element.
+ * - icon: An icon themed by file-icon.html.twig.
+ * - link: A rendered hyperlink.
+ *
+ * @see template_preprocess()
+ * @see template_preprocess_file_link()
+ *
+ * @ingroup themeable
+ */
+#}
+{{ icon }} {{ link }}
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..1d8b1aa
--- /dev/null
+++ b/core/modules/file/templates/file-managed-file.html.twig
@@ -0,0 +1,16 @@
+{#
+/**
+* @file
+* Default theme implementation to display a file form widget.
+*
+* Available variables:
+* - element: File form element HTML.
+* - attributes: Remaining HTML attributes for the containing element.
+*
+* @see template_preprocess()
+* @see template_preprocess_file_managed_file()
+*
+* @ingroup themeable
+*/
+#}
+{{ element }}
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..3b6417b
--- /dev/null
+++ b/core/modules/file/templates/file-upload-help.html.twig
@@ -0,0 +1,17 @@
+{#
+/**
+* @file
+* Default theme implementation to display help text for file fields.
+*
+* Available variables:
+* - descriptions: An array containing file field help texts.
+*
+* @see template_preprocess()
+* @see template_preprocess_file_upload_help()
+*
+* @ingroup themeable
+*/
+#}
+{% for description in descriptions %}
+ {{ description|t }}
+{% endfor %}
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..02e4784
--- /dev/null
+++ b/core/modules/file/templates/file-widget-multiple.html.twig
@@ -0,0 +1,17 @@
+{#
+/**
+* @file
+* Default theme implementation to display a multi file form widget.
+*
+* Available variables:
+* - table: Table of files.
+* - children: The rendered form element children for this widget.
+*
+* @see template_preprocess()
+* @see template_preprocess_file_widget_multiple()
+*
+* @ingroup themeable
+*/
+#}
+{{ table }}
+{{ children }}
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..3df9d6c
--- /dev/null
+++ b/core/modules/file/templates/file-widget.html.twig
@@ -0,0 +1,18 @@
+{#
+/**
+* @file
+* Default theme implementation to display a file widget.
+*
+* Available variables:
+* - element: File form element HTML.
+* - element_attributes: Remaining HTML attributes for the containing element.
+*
+* @see template_preprocess()
+* @see template_preprocess_file_widget()
+*
+* @ingroup themeable
+*/
+#}
+
+ {{ element }}
+