Skip to content

Instantly share code, notes, and snippets.

@arturu
Created February 2, 2019 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arturu/e47d5b9e2b31d48449d9dc97882e52a2 to your computer and use it in GitHub Desktop.
Save arturu/e47d5b9e2b31d48449d9dc97882e52a2 to your computer and use it in GitHub Desktop.
Drupal 8 Template for Paragraphs customized for Width and Background color fields.
{#
/**
* @file
* Default theme implementation to display a paragraph.
*
* Available variables:
* - paragraph: Full paragraph entity.
* - id: The paragraph ID.
* - bundle: The type of the paragraph, for example, "image" or "text".
* - authorid: The user ID of the paragraph author.
* - createdtime: Formatted creation date. Preprocess functions can
* reformat it by calling format_date() with the desired parameters on
* $variables['paragraph']->getCreatedTime().
* - content: All paragraph items. Use {{ content }} to print them all,
* or print a subset such as {{ content.field_example }}. Use
* {{ content|without('field_example') }} to temporarily suppress the printing
* of a given child element.
* - attributes: HTML attributes for the containing element.
* The attributes.class element may contain one or more of the following
* classes:
* - paragraphs: The current template type (also known as a "theming hook").
* - paragraphs--type-[type]: The current paragraphs type. For example, if the paragraph is an
* "Image" it would result in "paragraphs--type--image". Note that the machine
* name will often be in a short form of the human readable label.
* - paragraphs--view-mode--[view_mode]: The View Mode of the paragraph; for example, a
* preview would result in: "paragraphs--view-mode--preview", and
* default: "paragraphs--view-mode--default".
* - view_mode: View mode; for example, "preview" or "full".
* - logged_in: Flag for authenticated user status. Will be true when the
* current user is a logged-in member.
* - is_admin: Flag for admin user status. Will be true when the current user
* is an administrator.
*
* @see template_preprocess_paragraph()
*
* @ingroup themeable
*/
#}
{# Renders Width field. #}
{# Sets class name from values in database. #}
{% if content.field_wi|render %}
{% set layout_width = content.field_wi['#items'].getString() %}
{% set layout_width_classes = [
'element-width--medium' == layout_width ? 'paragraph--width__medium',
'element-width--wide' == layout_width ? 'paragraph--width__wide',
'element-width--full' == layout_width ? 'paragraph--width__full',
]
%}
{% endif %}
{# Renders Background field. #}
{# Sets class name from values in database. #}
{% if content.field_bg|render %}
{% set layout_background = content.field_bg['#items'].getString() %}
{% set layout_background_classes = [
'element--color element--color__blue' == layout_background ? 'paragraph--color paragraph--color__blue',
'element--color element--color__blue-light' == layout_background ? 'paragraph--color paragraph--color__blue-light',
'element--color element--color__gray' == layout_background ? 'paragraph--color paragraph--color__gray',
'element--color element--color__white' == layout_background ? 'paragraph--color paragraph--color__white',
'element--color element--color__yellow' == layout_background ? 'paragraph--color paragraph--color__yellow',
]
%}
{% endif %}
{# The template default set classes. #}
{%
set classes = [
'paragraph',
'paragraph--type--' ~ paragraph.bundle|clean_class,
view_mode ? 'paragraph--view-mode--' ~ view_mode|clean_class,
]
%}
{# Merges Width field with classes. #}
{% set width_field = content.field_wi|render %}
{% if width_field %}
{% set classes = classes|merge(layout_width_classes) %}
{% endif %}
{# Merges Background field with classes. #}
{% set background_field = content.field_bg|render %}
{% if background_field %}
{% set classes = classes|merge(layout_background_classes) %}
{% endif %}
{# Prints div with classes, and content without Width and Background. #}
<div{{ attributes.addClass(classes) }}>
<section>
{{ content|without('field_wi', 'field_bg') }}
</section>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment