---
title: "Using and Customizing the Paragraphs module in Drupal"
description: "Customize layouts, enhance UX and create dynamic content easily with the Drupal Paragraphs module. Explore the benefits of this versatile module and learn how to customize it and extend it."
url: https://www.specbee.com/blogs/using-and-customizing-paragraphs-module-in-drupal
type: Article
author: Sakthi
categories: "Code Corner"
published: 2023-06-27
updated: 2026-11-13
---

# Using and Customizing the Paragraphs module in Drupal

You know a CMS is impressive when even people without technical knowledge or those who are just starting to build websites can easily create and edit content. Drupal offers many modules that can make it simpler for site builders/content editors to work with their content and make their content work for them. One such powerful and flexible contributed module is the Paragraphs module in Drupal.

The Paragraphs module enables site builders to build nodes easily regardless of the complexity of the components. It provides a way to break down the content into smaller, more manageable components with their own fields and field settings. Easy customization of the components is done by using Twig templates, CSS, and hooks.

## Why use the Drupal Paragraphs Module

### 1. Flexible content creation

The Paragraphs module gives content creators the flexibility to create content by enabling them to create "paragraphs," which are reusable and structured parts of a piece of information. A text block, image gallery, slideshow, or any other specially defined component is represented by a certain piece or section of material in each paragraph. Content producers can combine and arrange content in a more dynamic and natural way, thanks to this modular approach.

### 2. Enhanced content editing experience

The style and organization of content pieces are now more flexible and under the author's control with the paragraphs module. They offer a more visible and intuitive editing experience by making it simple to add, rearrange, and remove paragraphs inside a content object. Users now have access to rich content and engaging pages without needing to know complicated HTML or special coding.

### 3. Reusability and consistency

By enabling paragraph reuse across various content entities, the paragraphs module fosters content reusability. This enables content creators to generate pre-fabricated paragraphs just once and utilize them across many settings, maintaining consistency in the way content is presented and organized. Furthermore, since edits to a shared paragraph are automatically reflected in all instances when it is used, it makes updating and maintaining content simpler.

### 4. Responsive and mobile-friendly

The responsive design capabilities of Drupal are seamlessly integrated with the paragraphs module. To ensure that the content adjusts to various screen sizes and devices, content authors can specify responsive layouts for paragraphs. This makes it possible to give an excellent [user experience](https://www.specbee.com/blogs/enhancing-user-experience-with-drupals-form-api) on desktop, tablet, and mobile devices.

### 5. Extensibility and customization

The Paragraphs module offers extensive customization and extensibility. Developers can design unique paragraph types with the fields and preferences necessary for their project. The module also works well with other Drupal features and modules, giving developers access to the full potential of the Drupal ecosystem.

### 6. Integration with other modules 

Other Drupal modules like Views, Display Suite, Panels, and Entity Reference function nicely with the Paragraphs module. It effortlessly connects with these modules, enabling more complicated content management procedures, advanced [display](https://www.specbee.com/blogs/customizing-content-display-in-drupal-guide-to-display-modes) and layout options, and improved content linkages.

We will soon discuss some of the usage and customization of paragraphs in detail.

## Creating a Paragraph Type

### Step 1:

A paragraph type can be created with one or more fields by navigating to **/admin/structure/paragraphs\_type** with Label, Paragraph type icon, and Description.

 ![pragraphs_types](https://www.specbee.com/sites/default/files/styles/large/public/migrated/pragraphs_types_0.png.avif?itok=MCO2h2x6)

 

 ### Step 2:

Add fields to the paragraph type similar to adding fields into Content Type.

 ![add_field](https://www.specbee.com/sites/default/files/styles/large/public/migrated/add_field_3.png.avif?itok=HeaZI8RH)

 

 Add the fields and configure the field settings.

### Step 3:

Add the Paragraph Type to the Content Type. Navigate to **/admin/structure/types** and select the particular content type. Add field Paragraphs under Reference revisions.

 ![structure_type](https://www.specbee.com/sites/default/files/styles/large/public/migrated/structure_type_0.png.avif?itok=AhE2v-wZ)

 

 Select the required Paragraph Types for the Content Type. We can **restrict** the Paragraph Type for each Content Type.

 ![content_types](https://www.specbee.com/sites/default/files/styles/large/public/migrated/content_types_1.png.avif?itok=il2is-nH)

 

 Now, we will be able to add Paragraphs in the Content Types.

 ![add_image_overlay](https://www.specbee.com/sites/default/files/styles/large/public/migrated/add_image_overlay_0.png.avif?itok=dXt3QjeZ)

 

 ## Customizing Paragraphs

### Hooks

Use hook\_preprocess\_paragraph\_paragraph\_type() for adding class name to HTML tags, adding a library to a paragraph and rendering views.

**Example:**

```plaintext
$variables['attributes']['class'][] = “classname”;
$variables['#attached']['library'][] = 'techx/custom'; (from libraries.yml file.)
```

1\. Add a custom class name to $variables of paragraph.

```plaintext
function theme_preprocess_paragraph__image_overlay(&$variables) {
    $paragraph = $variables['paragraph'];
    if ($paragraph->hasField('field_overlay')) {
        $overlay = $paragraph->get('field_overlay')->value;
        $variables['attributes']['class'][] = $overlay;
    }
}
```

 ![hook_preprocess](https://www.specbee.com/sites/default/files/styles/large/public/migrated/hook_preprocess_0.png.avif?itok=CSDr7Set)

 

  ![custom_theme_name](https://www.specbee.com/sites/default/files/styles/large/public/migrated/custom_theme_name_0.png.avif?itok=rSjAicum)

 

 Here theme in theme\_preprocess\_paragraph\_\_image\_overlay is a custom theme name.

Adding a Library from **libraries.yml** file to a paragraph:

```plaintext
function techx_preprocess_paragraph__image_overlay(&$variables) {
    $variables['#attached']['library'][] = 'techx/custom';
}
```

2\. Render a view based on user field selection.

```plaintext
function theme_preprocess_paragraph__techx_view(&$variables) {
    $paragraph = $variables['paragraph'];
    $view_id = 'techx';
    $view = \Drupal\views\Views::getView($view_id);
    if($paragraph->get('field_show_teaser')->value == '1'){
        $view_display = 'block_1';
        $view->setDisplay($view_display);
        $output = $view->render();
        $variables['view_block'] = $output;
    }else {
        $view_display = 'block_2';
        $view->setDisplay($view_display);
        $output = $view->render();
        $variables['view_block'] = $output;
    }
}
```

 ![theme_name](https://www.specbee.com/sites/default/files/styles/large/public/migrated/theme_name_0.png.avif?itok=fSce0SxO)

 

 Here theme in theme\_preprocess\_paragraph\_\_techx\_view is a custom theme name.

### Creating nested Paragraphs

Create a base Paragraph and reference that Paragraph in the Parent Paragraph.

 ![manage_fields](https://www.specbee.com/sites/default/files/styles/large/public/migrated/manage_fields_0.png.avif?itok=ABvEPbzG)

 

 ### Twigs with Paragraph

Create a Twig template with the required HTML tag. The twig template should be **paragraph—paragraph-type.html.twig**

1\. Image Overlay example:

```plaintext
{%
set classes = [
'paragraph',
'paragraph--type--' ~ paragraph.bundle|clean_class,
view_mode ? 'paragraph--view-mode--' ~ view_mode|clean_class,
]
%}
<div{{ attributes.addClass(classes) }}>
<div class = "image-overlay-contanier">
<div class = "image-wrapper">
{{ content.field_image }}
</div>
<div class = "image-overlay">
<div class ="text-wrapper">
{{ content.field_title }}
{% if content.field_description %}
{{content.field_description}}
{% endif %}
</div>
</div>
</div>
</div>
```

2\. Rendering View

```plaintext
{%
set classes = [
'paragraph',
'paragraph--type--' ~ paragraph.bundle|clean_class,
view_mode ? 'paragraph--view-mode--' ~ view_mode|clean_class,
]
%}
<div{{ attributes.addClass(classes) }}>
{{ view_block }}
</div>
```

### Translatable 

For multilingual sites, you can configure the Paragraphs module to be translatable with the required fields from /admin/config/regional/content-language. For enabling the translation and making the site multilingual, you need to enable the Language, Content Translation, Configuration Translation, and Interface Translation modules.

Find a detailed article on creating a multilingual site [here](https://www.specbee.com/blogs/drupal-translation-modules-create-multilingual-drupal-websites).

 ![add_css_styles](https://www.specbee.com/sites/default/files/styles/large/public/migrated/add_css_styles_0.png.avif?itok=ZYYRo_2s)

 

 You can add CSS styles to style the Paragraphs based on your requirements.

## Final thoughts

The Paragraph module’s flexible and intuitive interface empowers users to build dynamic and visually appealing websites without the need for complex coding. Its modular approach enables seamless content reuse and customization, fostering efficient content management and saving valuable time. If you’re looking for [Drupal development experts](https://www.specbee.com/services/drupal-web-development) to help you harness the full potential of the Paragraphs module, consider choosing us as your trusted Drupal development company.