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


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.


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 […]


Remove additional_settings from node add/edit form

When printing a form in Drupal, it has the right to display an area to define more advanced settings. This useful area when a director and totally useless for visitors (see dangerous): There are certainly several ways to remedy this problem, but I suggest you mine which seems clean, just change the forms no longer […]


How to get the URL for an image style derivative

To get the URL of an image after applying a particular style on the original picture, it is very simple just use the function below :

Here is an example to use this function :

So we get in the variable $ picture_path the url of the image on which we apply the […]


How to insert a block into a node or template in Drupal 7


$block = module_invoke(‘simplenews’, ‘block_view’, ’24’);


How do I redirect a form after has been submitted


OR :



Get drupal installed document root

To get the complete base path where the Drupal site is installed you can use:

On a Ubuntu system you will get something like:

If you only use “base_path()”, you will obtain only:


Add attribute to field form




Remove the “grippie” div from textarea

To delete the following div which appears below your texterea

You should add the code below in the form_alter hook