paragraphs_title_manager
Paragraphs Title Manager is a Drupal module designed to provide deterministic, configuration-based control over alignment of title-related fields within Paragraph entities. The module focuses exclusively on fields whose machine names contain the substring title. This includes, but is not limited to: title, sub_title, title_one, section_title, and similar naming patterns. This rule is intentionally strict to avoid ambiguous interpretation of non-title fields and to ensure predictable long-term behavior even when labels or UI text change.
Purpose
The module provides a central administrative interface where alignment preferences can be configured globally for all Paragraph types. This eliminates the need for theme-layer overrides, CSS targeting by bundle, or per-template logic. The system is designed for sites that rely heavily on Paragraphs for structured content and where consistent alignment of headings is required across multiple components.
Field Detection Logic
The module evaluates all Paragraph bundles using Drupal’s entity_field.manager. For each Paragraph type, every field definition is inspected. If the field’s machine name contains the literal substring title, it is registered as a title-like field. Detection is automatic and does not require user input. Fields without this substring (e.g., subtitle, heading, label) are ignored by design, in order to prevent false positives. Newly created fields that meet this naming rule automatically appear in the configuration UI without cache rebuilds.
Configuration UI
The configuration page is located at: /admin/config/content/paragraph-title-settings
The UI provides the following functionality:
- A grid-based layout grouping fields by Paragraph bundle.
- A per-field checkbox to enable or disable alignment management for that specific field.
- A per-field alignment selector populated with dynamic alignment options retrieved from the database (left, center, right, justify, start, end, or any custom options present in the
paragraphs_title_manager_alignment_optionstable). - A global “Select All / Deselect All” toggle.
- A global alignment selector that applies a chosen alignment to all currently selected fields.
If a Paragraph type contains no title-like fields, the system displays an internal notice within that section. If zero fields are detected across all bundles, the module displays a full-page notice explaining how title field detection works and what is required for fields to appear.
Permission Model
The configuration screen is protected by the single permission:
Manage Paragraphs Title AlignmentOnly users with this permission can access the UI and modify alignment settings. This ensures editors cannot alter global presentation rules unless explicitly granted access by site administrators.
How Alignment Is Applied
During rendering, the module implements hook_preprocess_paragraph(). For each Paragraph entity, it loads configuration records corresponding to that bundle. If a field is enabled and contains a value, the module generates a CSS class in the form:
ptm-title-align-{alignment}This class is then assigned to the following:
- A per-field Twig variable named
{{ field_name }}_alignment_class. - An array of wrapper-level classes stored in
{{ paragraphs_title_manager_classes }}. - The Paragraph’s wrapper attributes via the
Attributeobject, ensuring they appear in the rendered HTML element representing the Paragraph.
A mobile-specific override class (ptm-title-center-mobile) may be added for specific bundles (e.g., card layouts) when applicable. These classes allow themes to implement alignment consistently without custom logic.
Twig Variables Provided
During preprocessing, the module exposes several variables for use in Twig templates:
Wrapper-level:
{{ paragraphs_title_manager_classes }}Per-field variables (example):
{{ field_title_alignment_class }} {{ sub_title_alignment_class }} {{ title_one_alignment_class }}
These variables contain the computed alignment class and can be applied directly to markup. If a field is disabled or empty, no variable is generated.
Twig Example
<article{{ attributes }}> {% if field_title_alignment_class is defined %} <h2 class="{{ field_title_alignment_class }}">{{ content.field_title }}</h2> {% endif %} {% if sub_title_alignment_class is defined %} <h3 class="{{ sub_title_alignment_class }}">{{ content.sub_title }}</h3> {% endif %} {{ content|without('field_title', 'sub_title') }} </article>
Database Storage
The module stores configuration in the paragraphs_title_manager table. Each row contains:
- pid — Paragraph bundle machine name
- field_name — title-related field machine name
- enabled — integer 1/0 indicating whether alignment is active
- alignment — stored alignment key matching alignment options
All database operations are executed within try/catch blocks to prevent fatal errors during configuration or rendering.
Limitations
- Only fields containing
titlein their machine name are recognized. - Does not modify WYSIWYG/CKEditor headings.
- Does not manage node fields; strictly for Paragraph entities.
- Theme-level CSS with higher specificity may override alignment classes.
- Mobile override support is limited to predefined bundle checks.
This module is intended to provide predictable, unified, configuration-driven heading alignment without requiring per-site theme overrides, custom preprocess logic, or ad-hoc CSS targeting.