What we cover

Drupal is a content management system that enables developers and content creators to build highly customizable and efficient websites. One of the standout features introduced in the more recent versions of Drupal is Single Directory Components (SDC). These components allow for a more streamlined way to handle directories within Drupal, improving both usability and maintainability.

Not sure how to use them? Let’s explore what Single Directory Components are, their features and benefits, and how to use them to simplify your Drupal development process.

What are Single Directory Components (SDC)?

Single Directory Components (SDC) have been available with many other tech stacks, but now they’re a new and useful addition to Drupal. By consolidating all the necessary files and configurations into a single directory structure, SDC helps streamline the management process for developers and site administrators alike.

The primary aim of SDC is to enhance Drupal modularity. Traditionally, Drupal has been known for its complex structure, which could sometimes lead to confusion, especially for those who are new to the platform. Single Directory Components simplify this by organizing modules, themes, and configuration files in a way that maintains cohesion without sacrificing functionality.

By allowing developers to encapsulate related functionalities within a singular component, SDC paves the way for more efficient deployment, easier maintenance, and streamlined updates. This development has triggered a significant shift in how developers approach Drupal's architecture and component usage.

Moreover, the introduction of SDC aligns with the growing trend towards microservices and component-based architectures in web development. As organizations seek to build more agile and responsive applications, the ability to manage components in a single directory allows for quicker iterations and more flexible scaling. This means that teams can focus on enhancing specific functionalities without the overhead of navigating through a labyrinth of directories and files, thus fostering a more productive development environment.

Additionally, SDC promotes better collaboration among team members. With a clear and concise directory structure, developers can easily understand the relationships between different components, making it simpler to onboard new team members. This clarity not only enhances teamwork but also reduces the potential for errors during development and deployment, ensuring that projects can progress smoothly and efficiently.

TLDR? Check out our Drupal SDC Talk!

Short on time? Check out our latest talk from DrupalCon where we cover how we use Single Directory Components, the benefits of using it, and future predictions our team has based on the latest advancements in Drupal’s SDC:

Features & Benefits of Single Directory Components

What do Single Directory Components help you with during the development process? Let’s delve into each of the key features and benefits. 

Organized File Structure

With SDC, all files related to a specific component are stored together. This eliminates the need for developers to navigate through various directories to find assets like stylesheets or JavaScript files.

Modular Development

SDC promotes modularity, allowing you to build reusable components that can be easily integrated across multiple pages or sections of the website. This approach reduces code duplication and promotes maintainability.

Faster Development

By consolidating all the necessary files in one place, developers can work more efficiently. There’s less context switching, and making updates to a component becomes faster and more intuitive. Reusing code you’ve already created is also much easier with SDC now that you have an organized database containing all the previous code you’ve written.

Enhanced Collaboration

Since the structure is clear and organized, teams can collaborate more effectively. Designers, front-end developers, and back-end developers can easily find the necessary files to make adjustments or enhancements.

Improved Scalability

As projects grow, SDC makes it easier to scale by keeping components self-contained. Adding new components or updating existing ones becomes more straightforward, improving the scalability of your Drupal site.

Improved Version Control

Single Directory Components facilitate better version control practices. Since all related files are located in one place, developers can easily manage changes and track the history of modifications. This is particularly beneficial in larger teams where multiple developers may be making concurrent changes. The ability to revert to previous versions of a component becomes more straightforward, allowing teams to maintain stability while still innovating.

Steps for Using Single Directory Components

Now that we’ve covered what Single Directory Components are and their benefits, let’s dig into how exactly you can implement this into your development environment.

Step 1: Enable the Single Directory Component Module

First things first, you need to enable the SDC module in Drupal. This module is part of the core experimental modules of Drupal 10. 

Step 2: Create the Directory Structure

Establish a single directory for the SDC. This should include subdirectories for each essential part: assets, configuration, and any additional resources.

Step 3: Structure & Organize Your Directory Folders

Inside your directory, create individual folders for each component. Each folder should contain the necessary files:

Twig Template (.html.twig): The markup for the component.

Example:

Twig HTML Example

CSS/SCSS (.css or .scss): Styles specific to the component.

Example:

CSS Code Example

JavaScript (.js): Any behavior or interaction logic.

YAML File (.libraries.yml): To declare styles and scripts for the component.

Example:

YAML File Example

Here's an example of what the overall file structure looks like for a Single Directory Component:

Single Directory Component Example

Step 4: Register Your Component

To make your component accessible to Drupal, you’ll need to register it. In your theme’s .info.yml file, add the path to your components directory.

Step 5: Add Component-specific CSS and JavaScript

In your .libraries.yml file, register the styles and scripts for each component. In addition, make sure to attach the library to your component using Twig. 

Step 6: Test & Iterate

Once you have set up your components, test them across the site. Since everything is modular and organized, iterating on design or functionality will be a smoother process.

Key Takeaways on Single Directory Components

Utilizing Single Directory Components is a Drupal feature that helps you organize files and other assets for your website, streamlining the development process as a result. It’s also a better way for businesses to manage their website, make updates, and collaborate among their team. 

Of course, with all new features, this may not be something that you need to switch to right away or need at all for your environment. But, it could be a useful tool to play and experiment with during new builds or projects!

Need help with SDC and other Drupal features for your current project?

Let the Hounder team take the reins! Schedule a time with us that works for you and we can determine how we can help you make your website a success.

Share this post

Read more Hounder blogs