SeanB: Introducing YAML Bundles: The easiest way to maintain your content types!

Image removed.

Let’s face it — manually creating and maintaining a lot of content types in Drupal can be a real pain. The repetitive nature of tasks, inconsistencies in field creation, and the time-consuming process of updating settings across multiple content types are familiar struggles for developers working in teams.

At TwelveBricks, we maintain sites with very different content models. Too much time was spent setting up and maintaining those models, so we finally decided to do something about it!

Enter YAML Bundles: A Developer’s new best friend

YAML Bundles is a pragmatic approach to streamlining content type management through YAML-based Drupal plugins. It allows developers to define fields and content types from custom modules, making it a lot easier to add or update fields and content types.

The module defines 3 important plugin types:

  • Field types
    By defining common field types and their form/display settings (like widgets and formatters), you can remove repetitive configuration from the bundle definitions.
  • Entity types
    You can define common settings for entity types to remove even more repetitive configurations from the bundle definitions. We have also integrated support for a bunch of other contrib modules we often use, to save even more time.
  • Entity bundles
    You can use the defined fields in entity bundles, complete with customizable form and display settings that can be overridden on a field-by-field basis. The default settings of the entity types can also be overridden if you need to.

The module ships with defaults for the most common field and entity types, but you can easily override them. We have tests, which we also used to document all the available plugin settings and options. You can check out the documentation in the plugin definitions of the yaml_bundles_test module.

YAML Bundles’ Drush Tools

To use the power of YAML bundles, we’ve added a couple of helpful Drush commands that will revolutionize your content type management.

drush yaml-bundles:create-bundles
This command uses the defined plugins to (re)create all the specified bundles, fields, and settings. Whether you’re starting from scratch or optimizing an existing configuration, this command ensures the seamless generation of all your content types with a single command.

drush yaml-bundles:create-bundle
You don’t have to use the bundle plugins if you don’t want to. For those who prefer a more hands-on approach, this command offers the flexibility to create a new content type directly through Drush.

drush yaml-bundles:create-field
This command simplifies the process of adding new fields to existing content types using default plugin configurations. With the ability to customize form and view displays, this command makes adding fields to different content types much easier.

Integrating the modules we all love

YAML Bundles comes with out-of-the-box support for several popular Drupal modules, including:

  • Pathauto: Define path aliases for entities and bundles effortlessly.
  • Content Translation: Translate fields, labels, and settings into multiple languages.
  • Content Moderation: Integrate content types into existing workflows.
  • Field Group: Group fields for your content type directly from the YAML plugins.
  • Simple Sitemap: Ensure your entity/bundle is included in the sitemap.
  • Maxlength: Define maximum lengths for text fields if needed.
  • Layout Builder: Utilize layout builder for your entity/bundle.
  • Search API: Index entities/bundles and defined fields using the powerful Search API.

Let’s define a content type

To define default settings and fields for an entity type in your custom module, create a [mymodule].yaml_bundles.entity_type.yml file. In this file, you can create all the defaults you need for the entity type.

# Add the default settings and fields for node types.
node:
description: ''
langcode: en

# Make sure our plugin gets preference over the yaml_bundle plugin.
weight: 1

# Default content type settings.
new_revision: true
preview_mode: 0
display_submitted: false

# Enable content translation.
content_translation:
enabled: true
bundle_settings:
untranslatable_fields_hide: '1'
language_alterable: true
default_langcode: current_interface

# Add the content to the sitemap by default.
sitemap:
index: true
priority: 0.5
changefreq: 'weekly'
include_images: false

# Enable layout builder.
layout_builder: true

# Enable content moderation.
workflow: content

# Add the content to the default search index.
search_indexes:
- default

# Create the full and teaser view displays.
view_displays:
- teaser

# Add the default fields.
fields:
langcode:
label: Language
weight: -100
form_displays:
- default
title:
label: Title
search: true
search_boost: 20
weight: -98
form_displays:
- default
field_meta_description:
label: Meta description
type: text_plain
search: true
search_boost: 20
maxlength: 160
maxlength_label: 'The content is limited to @limit characters, remaining: <strong>@remaining</strong>'
weight: -25
form_displays:
- default

# Translate the default fields.
translations:
nl:
fields:
langcode:
label: Taal
title:
label: Titel
field_meta_description:
label: Metabeschrijving
maxlength_label: 'De inhoud is beperkt tot @limit tekens, resterend: <strong>@remaining</strong>'
de:
fields:
langcode:
label: Sprache
title:
label: Titel
field_meta_description:
label: Meta-Beschreibung
maxlength_label: 'Der Inhalt ist auf @limit-Zeichen beschränkt, verbleibend: <strong>@remaining</strong>'

To define a content type in your custom module, create a [mymodule].yaml_bundles.bundle.yml file. In this file, you can create all the bundle definitions you need for the bundle.

node.news:
label: News
description: A description for the news type.
langcode: en

# Add generic node type settings.
help: Help text for the news bundle.

# Enable a custom path alias for the bundle. Requires the pathauto module to
# be enabled.
path: 'news/[node:title]'

# Configure the simple_sitemap settings for the bundle. Requires the
# simple_sitemap module to be enabled.
sitemap:
priority: 0.5

# Configure the search API index boost for the bundle. Requires the
# search_indexes to be configured and the search_api module to be enabled.
boost: 1.5

# Configure the fields for the bundle. For base fields, the field only needs
# a label. For custom fields, the type needs to be specified. The type
# configuration from the yaml_bundles.field_type plugins will be merged with
# the field configuration to allow the definition to be relatively simple.
# Generic defaults for a field type can be configured in the
# yaml_bundles.field_type plugins.
#
# See yaml_bundles.yaml_bundles.field_type.yml for the list of supported
# field types and their configuration properties.
fields:

field_date:
type: datetime
label: Date
required: false
search: true
search_boost: 1
cardinality: 1
field_default_value:
-
default_date_type: now
default_date: now
form_displays:
- default
view_displays:
- full
- teaser

field_body:
type: text_long
label: Text
required: true
search: true
search_boost: 1
form_displays:
- default
view_displays:
- full

field_image:
type: image
label: Image
required: true
search: true
form_displays:
- default
view_displays:
- full
- teaser

field_category:
type: list_string
label: Category
required: false
search: true
options:
category_1: Category 1
category_2: Category 2
form_displays:
- default
view_displays:
- full
- teaser

field_link:
type: link
label: Link
required: true
search: true
cardinality: 2
form_displays:
- default
view_displays:
- full
- teaser

That’s all folks!

We’re excited about the potential YAML Bundles brings to the Drupal ecosystem, and we can’t wait for you to experience the difference it makes in your projects. Please check it out and let us know what you think!

Download YAML Bundles | Documentation | Example plugins

Sponsored by TwelveBricks — The easiest CMS for your business

Building a new website can be a time-consuming and expensive process, but it doesn’t have to be. TwelveBricks offers an affordable and easy-to-use (Award-winning!) Content Management System (CMS) for a fixed monthly price.

We’ve optimized the workflow for editors so they can fully focus on their content without worrying about the appearance. With built-in analytics and extensive SEO tools, optimizing content becomes even easier. Each website can be delivered in just 2 days!

If you’re looking for a new website and don’t want to wait for months, spend a fortune, or compromise on quality, check out our website at https://www.twelvebricks.com/en.

Image removed.
PubDate

Tags