Drupal 8

Drupal 8 - Custom field formatter

Submitted by superadmin on Sun, 01/03/2016 - 14:23

Thanks to the Object Oriented approach adopted in Drupal 8,  working with field extension in terms of widgets and formatters have become much easier. Gone are the days where you had to copy huge junks of code to use in your custom formatter. Now you just extend an exisiting formatter and change the bit you want to override.

Creating a new custom formatter is a really straightforward process. I am going to assume you already have a module that you want to place the field formatter in. 

Drupal 8 - ConfigFormBase and overrides caution

Submitted by superadmin on Sun, 12/27/2015 - 18:04

Drupal 8 makes a good attempt to stop overrides provided in places like settings.php from bleeding into configuration forms that will be exported. By default config objects obtained from Drupal::config can contain overrides from settings.php. However in the config object returned by ConfigFormBase::config() overrides are disabled and the data fetched from this object are the raw configurations. This could be dangerous when you want toggle between configuration between dev environment and production i.e. setting the site email address to a development inbox.

Drupal 8 - system settings form

Submitted by superadmin on Sun, 12/27/2015 - 15:22

system_settings_form was a handy function in Drupal 7 that saved forms to variables. This has been replaced by ConfigFormBase in Drupal 8. 

If we want the same behavior in Drupal 8 that was present in system_settings_form, our form class must extend ConfigFormBase. In doing so we must implement the abstract getEditableConfigNames method which returns the config names which are editable.

Also our buildForm method should call it's parent buildForm so that the submit buttons can be added.

Drupal 8 - template changes

Submitted by superadmin on Sun, 12/13/2015 - 14:24

By default templates are compiled and stored in memory. Any template changes will not be reflected until the cache is rebuilt(drush rebuid) which is not workable in a development environment.

To have the template files recompiled when they are changed, debugging must be enabled in the twig engine. This can be done either using Drupal console or sites/defualt/services.yml. If you don't have services.yml you can create it by copying sites/defualt/default.services.yml to sites/defualt/services.yml and changing the following: