Symfony in Drupal 8

Share this article

If you are a Drupaler, you must be aware that Drupal 8 is on the way. What you may not know is that it includes a few important Symfony components. Working with Drupal 8.x will give Drupalers an opportunity to become a part of a massive learning curve and is going to tempt them to know more about Symfony. In this article, we discuss the Symfony components that have been added to Drupal 8, covering some third party libraries along the way.

Before we get into the actual article, let us first briefly cover Symfony. It is a PHP based web application framework that follows the MVC architecture. Moreover, it is an open source project that has been released under the MIT license. Please make sure you don’t get it confused with Symfony CMS. If you wish to explore the Symfony framework into greater detail, see its Wiki page or any of the previous SitePoint articles about it.

Now let’s learn about the Symfony components that are being implemented in Drupal 8.

Symfony components in Drupal 8.x

The following are the Symfony components that are going to power the Drupal 8 core to a major extent:

  • HttpKernel and HttpFoundation – These are responsible for streamlining the process of converting a Request into a Response with the help of EventDispatcher. Drupal 8 being moved to Symfony was driven by Web Services and Content Core Initiative (WSCCI) in a motive to transform Drupal from a first-class CMS to a first-class REST server with first-class CMS running on top of it. This initiative is intended to allow Drupal to use web services to offer its content with reduced complexity; considering this as a long term vision, Drupal will be more flexible, robust and easily maintainable CMS.

  • EventDispatcher – Implements the Mediator pattern (that uses encapsulation) in a simple and effective manner especially where code inheritance doesn’t work out, making the application highly extensible. It is more effective in situations where you tend to maintain and/or refactor a program consisting of a huge number of classes due to the fact that it makes the communication amongst the classes quite simple and easy.

  • ClassLoader – Provides tools that autoload classes and caches their location. PHP uses the autoloading mechanism to delegate the loading of a file that defines the class in situations where you reference a class that has not been required or included yet. Symfony comes with autoloaders like PSR-0 Class Loader and MapClassLoader. Implementing the ClassLoader component will make Drupal module developers carefree especially when it comes to implementing module_load_include and other dependencies. Moreover, it allows developers easy calling of classes during run-time.

  • YAML – It parses YAML strings and converts them to PHP arrays and vice versa. This format has been especially designed to hold configuration related information, while being as expressive as XML files and as readable as INI files. It serves as an integral component of Drupal’s CMI (Configuration Management Initiative) that allows our modules to initially define their default configuration settings and later allows the site builder to override the same as-and-when instructed to. This concept of Drupal 8’s CMI which is powered by YAML is a replacement for Features contributed Drupal module which proves to be a robust concept as far as migrating and deploying across environments is concerned.

  • Routing – Allows us to load all routes, and dumps a URL matcher or generator specific to these routes. This also means that it maps an HTTP request to a set of configuration variables. As far as Drupal 8 and above versions are concerned, we define our module’s routes in a YAML configuration file, each of them set to trigger a specific action that has been defined in our module’s classes.

  • DependencyInjection – Primarily used to standardize and centralize the way objects are constructed in our application. Symfony’s DependencyInjection component has been incorporated in Drupal 8 in an intention to write code in such a way that classes can be reused and unit-tested wherever applicable and desired.

  • Twig – Drupal 8 has adopted the Twig template engine. This is of interest to the themers who will probably never think of any other option again for as long as they’re working on Drupal themes. Twig was developed by Fabien Potencier, who also developed the Symfony project, and was fine tuned for integration into Drupal 8.

  • Process – Helps execute commands in sub-processes using the command-line interface. Drupal 8 will use this to handle all activities that are command-line in nature.

  • Serializer – It is used to transform objects into a specific format (eg. XML, YAML, JSON, etc.) and vice versa. To understand it better, let us look at the following schema that a Serializer component follows:

    Additionally, we can use it to accomplish a number of jobs, ranging from configuration to node and entity creation that should be delivered by a REST endpoint.

  • Validator – Helps Drupal validate values. For example: validating form submission, validating entities within Drupal, etc. To accomplish its job, it uses Doctrine Annotations (discussed in Out-of-the-box third party components section).

  • Translation – Provides a standard set of tools to load translation files, generate translated strings as output, and use the generated outcome.

Out-of-the-box third party components

We went through Symfony components that have been included with Drupal 8. In this section, we’ll find out about the 3rd party PHP components that have been implemented in Drupal 8 and are not related to Symfony. They are:

  • Assetic – An Asset Management framework. Assets in Drupal consist of CSS files, JavaScript files, images, other media files, metadata, etc. These assets contain filterable content that can be loaded and dumped. In order to manipulate assets, you need to apply appropriate filters. To understand the filters, in what situations each can be used, how-to use the filters, build, dump, and much more, we recommend you read the Assetic Github page.

  • Composer – A tool specifically designed and developed to manage dependency in PHP allowing us to declare the dependent libraries our project needs and install them for us. Though it deals in packages and libraries, it is not a package manager. Composer is a boon to Drupal 8 due the following:

    • Handles situations very efficiently wherein your project is dependent upon a number of libraries.

    • Tackles situations where there is a nested dependency concept amongst the libraries. For instance; your project is dependent upon a library (say, lib1) and in tern lib1 is dependent upon some other library (say, lib2).

    • It is Composer’s responsibility to choose which version of the package or library needs to be installed unless explicitly told which versions to target.

  • Doctrine – A bundle of tools for use with database records. Drupal 8 uses a part of it called Annotations that exposes additional metadata to Drupal. Efforts have been made to present such tricky functionality in a quite easy and handy way, especially when defining custom entity types. If you wish to explore further on how Doctrine is supposed to be used with Drupal 8, we recommend you visit the documentation page.

  • EasyRDF – A PHP library that has been used with Drupal 8 so as to provide ease in consuming and producing RDF. With the help of EasyRDF, Drupal 8 adds capability to produce metadata in the markup in an easy and convenient way. You may also visit EasyRDF official site to learn more about it.

  • Guzzle – It is a PHP HTTP client and framework that helps Drupal 8 make web requests using REST based web service calls. This makes Drupal 8 web portals more efficient in terms of handling different sorts of web services. To explore further on Guzzle, we recommend reading our previous article on it.

  • PHPUnit – Used in Drupal 8 to standardize unit testing throughout the core as well as help developers write code that is of utmost quality. PHPUnit ensures that any code written in Drupal 8 and in any custom module incorporated in it matches industry standards and works as expected throughout its life-cycle. You may keep an eye on the current status of PHPUnit’s incorporation into Drupal 8 by going here.

  • PSR-3 Logging – A common logging system that is shared by an entire PHP application. Drupal 7 and older versions use watchdog() for this purpose. Switching Drupal logging from watchdog() to a PSR-3 logging framework has made Drupal 8 more robust and scalable in terms of common logging. You may track the current status of this component at Switch watchdog to a PSR-3 logging framework.

Conclusion

The introduction of a few Symfony components has simplified working with Drupal 8. Additionally, including a few of the non-Symfony components has added value to Drupal 8 in terms of its quality, scalability, and performance, making it even more robust and maintainable.

Frequently Asked Questions about Symfony and Drupal 8

What are the benefits of using Symfony in Drupal 8?

Symfony is a set of reusable PHP components and a PHP framework for web projects. The integration of Symfony in Drupal 8 provides several benefits. Firstly, it improves the speed and performance of Drupal websites. Secondly, it provides a structured and standardized way to build a website, making it easier for developers to understand and modify the code. Lastly, Symfony has a large and active community that provides regular updates and security fixes, ensuring the longevity and reliability of your Drupal website.

How does Symfony improve the Drupal 8 architecture?

Symfony brings a more modern, object-oriented approach to Drupal 8. It introduces concepts like Dependency Injection and Event Dispatcher, which allow for better organization and decoupling of code. This makes the code more maintainable and testable. Furthermore, Symfony’s HttpKernel allows Drupal to handle requests and responses in a standardized way, improving interoperability with other PHP projects.

What are some of the Symfony components used in Drupal 8?

Drupal 8 uses several Symfony components, including HttpFoundation, HttpKernel, Routing, EventDispatcher, DependencyInjection, and ClassLoader. These components handle various aspects of the request-response cycle, routing, event management, dependency management, and autoloading of classes, respectively.

How does the use of Symfony affect Drupal 8 module development?

With the integration of Symfony, Drupal 8 module development has become more streamlined and efficient. Developers can now use Symfony’s service container for dependency injection, making their modules more modular and testable. Also, the use of Symfony’s routing system allows for more flexible and powerful URL routing.

How does Symfony contribute to Drupal 8’s performance?

Symfony’s components are highly optimized for performance. For instance, the HttpKernel component allows Drupal to handle requests and responses in a standardized way, reducing overhead and improving response times. Additionally, Symfony’s EventDispatcher allows for efficient event management, reducing the amount of unnecessary code execution.

How does Symfony enhance Drupal 8’s security?

Symfony has a robust security component that provides a comprehensive security framework for your applications. It provides features like authentication, authorization, data encryption, and protection against common web vulnerabilities. By leveraging this component, Drupal 8 can provide a more secure platform for your web applications.

How does the integration of Symfony affect Drupal 8’s compatibility with other technologies?

Symfony is built with interoperability in mind. It follows PHP-FIG standards and uses Composer for package management, making it compatible with a wide range of PHP projects and libraries. This means that Drupal 8, with Symfony at its core, can easily integrate with other technologies and take advantage of a vast ecosystem of PHP packages.

How does Symfony help in Drupal 8’s testing and debugging?

Symfony provides several tools and components that aid in testing and debugging. For instance, the Debug and VarDumper components provide helpful tools for debugging your applications. Also, Symfony’s BrowserKit and DomCrawler components can be used for functional testing of your Drupal 8 website.

How does Symfony contribute to the future-proofing of Drupal 8?

Symfony is a well-maintained project with a large and active community. It follows best practices in web development and keeps up with the latest trends in PHP. By integrating Symfony, Drupal 8 ensures that it stays relevant and continues to provide a robust platform for web development in the future.

How does the use of Symfony affect the learning curve for Drupal 8?

While Symfony does introduce new concepts and practices that may be unfamiliar to Drupal developers, it also provides a more structured and standardized way to build websites. This can actually make it easier to learn and understand Drupal 8. Furthermore, there are plenty of resources and tutorials available for Symfony, which can help developers get up to speed quickly.

Surendra MohanSurendra Mohan
View Author

Surendra Mohan, who has served a few top-notch software organizations in varied roles, is currently a freelance software consultant. He has been working on various cutting-edge technologies like Drupal, Moodle, etc. for over 9 years. He also delivers technical talks at various community events like Drupal Meetups and Drupal Camps. Surendra has also authored the book ‘Administrating Solr’ published by Packt Publishing.

asseticdrupaldrupal8librariesloggingoopPHPphpunitpsr3symfonysymfony2
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week