diff --git a/core/modules/file/file.field.inc b/core/modules/file/file.field.inc index 3e3806e..a8b4941 100644 --- a/core/modules/file/file.field.inc +++ b/core/modules/file/file.field.inc @@ -586,41 +586,37 @@ function file_field_widget_submit($form, &$form_state) { } /** - * Returns HTML for an individual file upload widget. + * Prepares variables for file form widget templates. * - * @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. + * - element_attributes: An Array of HTML attributes. */ -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 (!empty($element['fids']['#value'])) { // Add the file size after the file name. $file = reset($element['#files']); $element['file_' . $file->id()]['filename']['#suffix'] = ' (' . format_size($file->getSize()) . ') '; } - $output .= drupal_render_children($element); - $output .= '
'; - - 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. @@ -702,6 +698,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'), @@ -710,34 +707,30 @@ function theme_file_widget_multiple($variables) { drupal_add_tabledrag($table_id, 'order', 'sibling', $weight_class); - $build = array( + $variables['table'] = array( '#theme' => 'table', '#header' => $headers, '#rows' => $rows, - '#attributes' => array( - 'id' => $table_id, - ), + '#attributes' => array('id' => $table_id), ); $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 help text templates. + * + * Default template: file-upload-help.html.twig * - * @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 */ -function theme_file_upload_help($variables) { +function template_preprocess_file_upload_help(&$variables) { $description = $variables['description']; $upload_validators = $variables['upload_validators']; $cardinality = $variables['cardinality']; @@ -778,7 +771,7 @@ function theme_file_upload_help($variables) { } } - return implode('
', $descriptions); + $variables['descriptions'] = $descriptions; } /** @@ -805,15 +798,15 @@ function file_field_find_file_reference_column($field) { } /** - * Returns HTML for a file attachments table. + * Prepares variables for file attachments table templates. * - * @param $variables + * Default template: file-formatter-table.html.twig. + * + * @param array $variables * An associative array containing: * - items: An array of file attachments. - * - * @ingroup themeable */ -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) { @@ -825,16 +818,18 @@ function theme_file_formatter_table($variables) { '#file' => $item['entity'], ), ), - format_size($item['entity']->getSize()), + array('data' => format_size($item['entity']->getSize())), ); } } - $build = array( - '#theme' => 'table', - '#header' => $header, - '#rows' => $rows, - ); + $variables['content'] = array(); + if (!empty($rows)) { + $variables['content'] = array( + '#theme' => '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 e25a17b..609f526 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -584,29 +584,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, 'description' => NULL), - ), - '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', + 'template' => 'file-widget-multiple', + 'file' => 'file.field.inc', ), 'file_formatter_table' => array( 'variables' => array('items' => NULL), + 'template' => 'file-formatter-table', + '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', ), ); } @@ -1501,15 +1508,15 @@ function file_managed_file_save_upload($element) { } /** - * 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(); @@ -1521,12 +1528,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'] = $attributes; + $variables['element'] = $element; } /** @@ -1566,36 +1569,46 @@ function file_managed_file_pre_render($element) { } /** - * Returns HTML for a link to a file. + * Prepares variables for file link templates. * - * @param $variables + * Default template: file-link.html.twig. + * + * @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. - * - * @ingroup themeable */ -function theme_file_link($variables) { +function template_preprocess_file_link(&$variables) { $file = $variables['file']; + $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 $options = array( 'attributes' => array( - 'type' => $file->getMimeType() . '; length=' . $file->getSize(), + 'type' => $file_entity->getMimeType() . '; length=' . $file_entity->getSize(), ), ); // 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'] = check_plain($file->getFilename()); + $options['attributes']['title'] = check_plain($file_entity->getFilename()); } $file_icon = array( @@ -1604,28 +1617,8 @@ function theme_file_link($variables) { '#icon_directory' => $variables['icon_directory'], ); - return '' . drupal_render($file_icon) . ' ' . l($link_text, file_create_url($file->getFileUri()), $options) . ''; -} - -/** - * 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 = check_plain($file->getMimeType()); - $icon_url = file_icon_url($file, $icon_directory); - return ''; + $variables['link'] = l($link_text, $url, $options); + $variables['attributes'] = array('class' => array('file')); } /** 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..71e3dc2 --- /dev/null +++ b/core/modules/file/templates/file-formatter-table.html.twig @@ -0,0 +1,14 @@ +{# +/** + * @file + * Default theme implementation to display a file attachments table. + * + * Available variables: + * - content: File form element HTML. + * + * @see template_preprocess_file_formatter_table() + * + * @ingroup themeable + */ +#} +{{ content }} 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..6dfa936 --- /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: Remaining HTML attributes of an element. + * - attributes.class: HTML classes. + * - icon: An icon as produced by file-icon.html.twig. + * - link: A rendered hyperlink. + * + * @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..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 + */ +#} + + {{ 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..cdcb30a --- /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|t|join('
') }} 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 + */ +#} + + {{ element }} +