From 624f9e62de2caec0f8e6de7624f2256216307ad3 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Fri, 24 May 2013 10:37:11 -0700 Subject: [PATCH] Issue #1898062 by c4rl, jenlampton, shlapa, joelpittet, Cottser, duellj, swentel: Convert field module to Twig. --- core/modules/field/field.api.php | 2 +- core/modules/field/field.module | 37 +++++++----- .../Drupal/field/Plugin/views/field/Field.php | 2 +- core/modules/field/templates/field.html.twig | 48 +++++++++++++++ core/modules/field/templates/field.tpl.php | 59 ------------------- 5 files changed, 72 insertions(+), 76 deletions(-) create mode 100644 core/modules/field/templates/field.html.twig delete mode 100644 core/modules/field/templates/field.tpl.php diff --git a/core/modules/field/field.api.php b/core/modules/field/field.api.php index b159f59579d4..75a99df2c894 100644 --- a/core/modules/field/field.api.php +++ b/core/modules/field/field.api.php @@ -970,7 +970,7 @@ function hook_field_attach_update(\Drupal\Core\Entity\EntityInterface $entity) { /** * Alter field_attach_preprocess() variables. * - * This hook is invoked while preprocessing the field.tpl.php template file in + * This hook is invoked while preprocessing field templates in * field_attach_preprocess(). * * @param $variables diff --git a/core/modules/field/field.module b/core/modules/field/field.module index bf0ac5699add..a429eea515b8 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -985,10 +985,17 @@ function field_page_build(&$page) { } /** - * Theme preprocess function for theme_field() and field.tpl.php. + * Prepares variables for field templates. * - * @see theme_field() - * @see field.tpl.php + * Default template: field.html.twig. + * + * @param array $variables + * An associative array containing: + * - element: A render element representing the field. + * - attributes: A string containing the attributes for the wrapping div. + * - title_attributes: A string containing the attributes for the title. + * - content_attributes: A string containing the attributes for the content's + * div. */ function template_preprocess_field(&$variables, $hook) { $element = $variables['element']; @@ -1041,10 +1048,10 @@ function template_preprocess_field(&$variables, $hook) { } /** - * Theme process function for theme_field() and field.tpl.php. + * Theme process function for theme_field() and field.html.twig. * * @see theme_field() - * @see field.tpl.php + * @see field.html.twig */ function template_process_field(&$variables, $hook) { static $default_attributes; @@ -1084,23 +1091,23 @@ function template_process_field(&$variables, $hook) { * - THEMENAME_field() * * Theme developers who prefer to customize templates instead of overriding - * functions may copy the "field.tpl.php" from the "modules/field/theme" folder - * of the Drupal installation to somewhere within the theme's folder and + * functions may copy the "field.html.twig" from the "modules/field/theme" + * folder of the Drupal installation to somewhere within the theme's folder and * customize it, just like customizing other Drupal templates such as - * page.tpl.php or node.tpl.php. However, it takes longer for the server to + * page.html.twig or node.html.twig. However, it takes longer for the server to * process templates than to call a function, so for websites with many fields * displayed on a page, this can result in a noticeable slowdown of the website. - * For these websites, developers are discouraged from placing a field.tpl.php + * For these websites, developers are discouraged from placing a field.html.twig * file into the theme's folder, but may customize templates for specific * fields. For example, for a field named 'body' displayed on the 'article' * content type, any of the following templates will override this default * implementation. The first of these templates that exists is used: - * - field--body--article.tpl.php - * - field--article.tpl.php - * - field--body.tpl.php - * - field.tpl.php + * - field--body--article.html.twig + * - field--article.html.twig + * - field--body.html.twig + * - field.html.twig * So, if the body field on the article content type needs customization, a - * field--body--article.tpl.php file can be added within the theme's folder. + * field--body--article.html.twig file can be added within the theme's folder. * Because it's a template, it will result in slightly more time needed to * display that field, but it will not impact other fields, and therefore, is * unlikely to cause a noticeable change in website performance. A very rough @@ -1125,7 +1132,7 @@ function template_process_field(&$variables, $hook) { * * @see template_preprocess_field() * @see template_process_field() - * @see field.tpl.php + * @see field.html.twig * * @ingroup themeable */ diff --git a/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php b/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php index 7e7de463319b..19fe3e163ca0 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php +++ b/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php @@ -371,7 +371,7 @@ public function buildOptionsForm(&$form, &$form_state) { '#title' => t('Use field template'), '#type' => 'checkbox', '#default_value' => $this->options['field_api_classes'], - '#description' => t('If checked, field api classes will be added using field.tpl.php (or equivalent). This is not recommended unless your CSS depends upon these classes. If not checked, template will not be used.'), + '#description' => t('If checked, field api classes will be added by field templates. This is not recommended unless your CSS depends upon these classes. If not checked, template will not be used.'), '#fieldset' => 'style_settings', '#weight' => 20, ); diff --git a/core/modules/field/templates/field.html.twig b/core/modules/field/templates/field.html.twig new file mode 100644 index 000000000000..4b1336668475 --- /dev/null +++ b/core/modules/field/templates/field.html.twig @@ -0,0 +1,48 @@ +{# +/** + * @file + * Default theme implementation for a field. + * + * To override output, copy the "field.html.twig" from the templates directory + * to your theme's directory and customize it, just like customizing other + * Drupal templates such as page.html.twig or node.html.twig. + * + * For example, for a field named 'body' displayed on the 'article' content + * type, any of the following templates will override this default + * implementation. The first of these templates that exists is used: + * - field--body--article.html.twig + * - field--article.html.twig + * - field--body.html.twig + * - field.html.twig + * + * Available variables: + * - attributes: HTML attributes for the containing element. + * - label_hidden: Whether to show the field label or not. + * - title_attributes: HTML attributes for the title. + * - label: The label for the field. + * - content_attributes: HTML attributes for the content. + * - items: List of all the field items. + * - item_attributes: List of HTML attributes for each item. + * + * @see template_preprocess_field() + * @see theme_field() + * + * @ingroup themeable + */ +#} +<!-- +THIS FILE IS NOT USED AND IS HERE AS A STARTING POINT FOR CUSTOMIZATION ONLY. +See http://api.drupal.org/api/function/theme_field/8 for details. +After copying this file to your theme's folder and customizing it, remove this +HTML comment. +--> +<div{{ attributes }}> + {% if not label_hidden %} + <div class="field-label"{{ title_attributes }}>{{ label }}: </div> + {% endif %} + <div class="field-items"{{ content_attributes }}> + {% for delta, item in items %} + <div class="field-item {{ cycle(["even", "odd"], delta) }}"{{ item_attributes[delta] }}>{{ item }}</div> + {% endfor %} + </div> +</div> diff --git a/core/modules/field/templates/field.tpl.php b/core/modules/field/templates/field.tpl.php deleted file mode 100644 index a8ffe322af5f..000000000000 --- a/core/modules/field/templates/field.tpl.php +++ /dev/null @@ -1,59 +0,0 @@ -<?php - -/** - * @file field.tpl.php - * Default template implementation to display the value of a field. - * - * This file is not used and is here as a starting point for customization only. - * @see theme_field() - * - * Available variables: - * - $items: An array of field values. Use render() to output them. - * - $label: The item label. - * - $label_hidden: Whether the label display is set to 'hidden'. - * - $attributes: An instance of Attributes class that can be manipulated as an - * array and printed as a string. - * It includes the 'class' information, which includes: - * - field: The current template type, i.e., "theming hook". - * - field-name-[field_name]: The current field name. For example, if the - * field name is "field_description" it would result in - * "field-name-field-description". - * - field-type-[field_type]: The current field type. For example, if the - * field type is "text" it would result in "field-type-text". - * - field-label-[label_display]: The current label position. For example, if - * the label position is "above" it would result in "field-label-above". - * - * Other variables: - * - $element['#object']: The entity to which the field is attached. - * - $element['#view_mode']: View mode, e.g. 'full', 'teaser'... - * - $element['#field_name']: The field name. - * - $element['#field_type']: The field type. - * - $element['#field_language']: The field language. - * - $element['#field_translatable']: Whether the field is translatable or not. - * - $element['#label_display']: Position of label display, inline, above, or - * hidden. - * - $field_name_css: The css-compatible field name. - * - $field_type_css: The css-compatible field type. - * - * @see template_preprocess_field() - * @see theme_field() - * - * @ingroup themeable - */ -?> -<!-- -THIS FILE IS NOT USED AND IS HERE AS A STARTING POINT FOR CUSTOMIZATION ONLY. -See http://api.drupal.org/api/function/theme_field/8 for details. -After copying this file to your theme's folder and customizing it, remove this -HTML comment. ---> -<div class="<?php print $attributes['class']; ?>"<?php print $attributes; ?>> - <?php if (!$label_hidden): ?> - <div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>: </div> - <?php endif; ?> - <div class="field-items"<?php print $content_attributes; ?>> - <?php foreach ($items as $delta => $item): ?> - <div class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><?php print render($item); ?></div> - <?php endforeach; ?> - </div> -</div> -- GitLab