Wednesday, October 12, 2016

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 theClassLoader component will make Drupal module developers carefree especially when it comes to implementingmodule_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.
Courtesy :  https://www.sitepoint.com/symfony-drupal-8/

only show translated menu items into current language (Drupal 8)

function MY_THEME_preprocess_menu(&$variables) {   if ($variables['menu_name'] == 'brancott-header-menu') {    $langu...