Part 4: Utility Components in Drupal (Spotlight on Symfony in Drupal)

We've made it to the final blog post (for now?) in our spotlight on Symfony in Drupal.

Spotlight on Symfony in Drupal

Utility Symfony components used in Drupal

Let's look at the other components.

We're using the term, "utility components", to group together Symfony components that provide useful functionality. It's a great idea to familiarize yourself with these. You can make use of them in your own modules, or bring them into other PHP application code.

Console

For Drupal developers, there are at least 2 readily-available options for building custom command-line utilities or applications: Symfony Console and Drush.

Symfony's Console component is useful for building command-line utilities for PHP applications. Remember the Drupal Console project? While no longer maintained, it used Console under-the-hood.

Drush has several points of extension including creating custom Drush commands. Drush includes the Drupal Code Generator project, a code scaffolding tool which you can extend to create custom commands.

If you're interested in adding Console functionality in Drupal core, check out this issue: Provide core CLI commands for the most common features of Drush.

Yaml

The Yaml component loads and parses YAML files. Drupal's configuration management system uses this one a lot.

Process

The Process component runs commands in a sub-process. It can handle system commands like exec, passthru, system, or git, and handles differences in operating systems for us.

Mime

The Mime component provides useful methods for working with MIME types and sending emails.

Polyfill

Symfony backports features from the latest PHP versions into polyfill components. These are automatically included depending on your configuration and PHP version.

Translation

Symfony's translation tools support the internationalization of an application.

Serializer

The Serializer component handles the encoding and decoding of objects among formats like XML, JSON, and Yaml.

Validator

The Validator component provides more of a design pattern, or an API of sorts that other modules can use to define what types of data they work with. These rules are defined as Constraints, and the code that actually evaluates the logic are called Validators.

Learn more

Explore Symfony in Drupal for yourself:

  • Look in vendor/symfony for Symfony components.
  • Browse core/lib/Drupal/Core and core/lib/Drupal/Component for classes that are original to Drupal. This is where you'll find Drupal's customizations of Symfony components.

Recap

We've taken a high-level look at key Symfony components in Drupal. If this article has sparked your curiosity, dig into the Symfony components documentation. Learn how to extend and alter Drupal. Looking under the hood of both Symfony and Drupal improves our understanding and makes us better developers. And these skills can transfer to other PHP projects that use Symfony.

Related Topics

Add new comment

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <code class> <ul type> <ol start type> <li> <dl> <dt> <dd><h3 id> <p>
  • Lines and paragraphs break automatically.

About us

Drupalize.Me is the best resource for learning Drupal online. We have an extensive library covering multiple versions of Drupal and we are the most accurate and up-to-date Drupal resource. Learn more