Top menu

Drupal 9 Create Block programmatically and insert it in template file

Create the block class file /web/modules/custom/my_module/src/Plugin/Block/HelloBlock.php and add the code below. To insert this block programmatically in a template we can implement a hook_preprocess into .module file of our module

Then into our template file we display the variable

don’t forget to execute : vendor/bin/drush cr


Define a default value in configureFormFields

To add some some default field value whith SonataAdminBundle you should use the getNewInstance function :



How to use a callback with SonataAdminBundle sonata_type_model_autocomplete in configureFormFields

The callback in sonata_type_model_autocomplete configuration is a callable function that can be used to modify the query which is used to retrieve autocomplete items. The callback should receive three parameters – the Admin instance, the property (or properties) defined as searchable and the search value entered by the user.



Add join with custom database table in hook_views_query_alter()

In this example i would add a join to the database table “node” with an other database table “event_submissions” which i created in my custom module, so i will add a hook_views_query_alter() function in mymodule.module file :

This code allows me to get the events that the current user had participated.


Create a class to consume soap web service

There is an example of class to show you how to write code to consume a SOAP based web service.

In the code below a demo of how to instanciate this class and call the RequestSW method :



Alter captcha field with the hook_element_info_alter()

If you want to change the field properties of  the Captcha module or the Image CAPTCHA module you will see that this is not possible with hook_form_alter () because through this function we can not access to the fields of the captcha, the picture below shows an example of what you get when we display the variable $form in […]


Sql script to execute after moving a WordPress Website to New Domain or Location




Advanced example of using the “for” and “loop” in twig

This example illustrate how to use a nested “for” in twig also the use of a loop instruction:

Inside of a for loop block you can access some special variables: Variable Description loop.index The current iteration of the loop. (1 indexed) loop.index0 The current iteration of the loop. (0 indexed) loop.revindex The number of iterations from […]


Coding Standards for Drupal, Symfony and Twig

Twig : Symfony : Drupal : Configuring drupal with netbeans :


How to use the Intl Extension Twig for date

First you should install the php5-intl :

Then add the code below in your “app/config/config.yml” file :

Finaly in you twig file you can use the localizeddate filter to format dates into a localized string representating the date :