Drupal 8 JS event after opening AJAX modal

Drupal provides a few custom events for AJAX modals. Even though the modal itself is provided by jQuery UI library, these events are custom and are provided by Drupal itself.

There are a few events you can use here:

superuser Tue, 27/10/2020 - 08:04
Drupal 8 Commerce add promotion programmatically to the order or shopping cart

Doing this for a particular order is pretty straightforward. All you need to have is the coupon code ID:

superuser Mon, 29/06/2020 - 09:04
Applying Drupal 8 patches in composer.json

Sooner or later you will encounter an issue in Drupal core or contrib project that has been fixed but the update has not been included in an official release.

This article explains how to add patches to your composer.json so they get applied whenever you update or install the project from scratch.

superuser Wed, 19/02/2020 - 00:57
Drupal 8 add inline JavaScript to a page

Separate JS files are good but in certain cases (e.g. tracking scripts) it might be useful to just add a piece of inline code to a specific page.

This can easily be achieved from a custom module by implementing hook_page_attachments(), checking the page you are on and adding the JS code to the HTML head.

superuser Tue, 18/02/2020 - 12:06
Rehash MD5 password in Drupal 8

Below is a simple code snippet that allows you to convert a password hashed using MD5 into the format that can be used by Drupal 8. This can be useful if you are creating users programmatically and need to re-create their password from an MD5 source.

superuser Tue, 17/09/2019 - 12:59
4 most common user roles in Drupal websites

User roles are one of the things that is usually dealt with only while developing the website from scratch or when adding new features. Since standardization is the key to consistent results and more streamlined development process, you should have some sort of a strategy when it comes to user roles.

superuser Wed, 08/03/2017 - 21:48
Programmatically creating custom block types for Panels

The most potent Drupal module for creating very complex layouts is definitely Panels. It integrates with a lot of other parts of your Drupal site, so you can show views, nodes, webforms, blocks, and pretty much anything else you can think of. Page builder is very easy to use and allows administrators visual representation of complex pages.

superuser Fri, 03/03/2017 - 00:37
Custom permissions in Drupal 8

Drupal's permissions system is at the same time easy to use and very powerful. It covers most of the cases where your module provides different functionality for various roles on the site.

In both versions of Drupal this is fairly straightforward to implement through code. There are two parts to this article: defining custom permissions (both static and dynamic), and performing checks to see if the current user has access to them.

superuser Sat, 17/09/2016 - 14:31
Quick tip: getting the SQL built with db_select() in Drupal 7 and Drupal 8

When building more complex queries using db_select() you will often want to see the exact SQL being generated. This is helpful for understanding the query or simply debugging the results.

This article will show you how to see the exact query being generated with db_select() as well as getting how to get all values passed as arguments. The code in this article will be applicable for both Drupal 7 and Drupal 8.

superuser Fri, 02/09/2016 - 15:03
Changing user display name in Drupal 7 and Drupal 8

Every website that displays user information on the front end will use profile fields such as first and last names for representing the members. By default Drupal shows only the username, which is definitely something you will want to change.

Modifying this is relatively simple. You could always choose which fields to use in Views, Rules and other modules, but the main problem is maintenance - the setup will be spread across many different pages and it's not the most optimal solution in the long run.

The right way is to change the way user display names are formatted by the system itself. This article will show you how to manage this for both Drupal 7 and Drupal 8 in your custom code.

superuser Wed, 31/08/2016 - 21:15