Posts

How to create pager in drupal 8/9 for custom data set

Sometimes we need to create a pager for custom data sets, such as retrieving specific types of nodes and applying a filter on top of them to get the expected data set. Here, I am going to create a custom Drupal module with the name pager_example, which will create a pager for a custom data set. I am going to register a path where we will be showing a page with a pager using a custom data set. For that, I am going to create a routing file with the name pager_example.routing.yml which will have the following snippet. pager_example.subscriptions: path: '/subscriptions' defaults: _controller: '\Drupal\pager_example\Controller\SubscriptionsController::getSubscriptions' requirements: _permission: 'access content' Let's create a Controller for the route mentioned above. namespace Drupal\pager_example\Controller; use Drupal\Core\Controller\ControllerBase; /** * Subscriptions controller. */...

Install drush globally using composer on WSL

With reference to my previous post, Setup development environment for Drupal on windows 10 , I noticed that installing drush on WSL using composer doest work by default, in this post I am going to demonstrate how to install drush globally using composer on WSL. I am assuming you have composer install, in order to install drush we have to follow below mentioned steps: $ composer global require drush/drush Now check drush installed path by running: $ composer config --global home This will print path for drush currently install i.e. /home/my-user-name/.config/composer , now we have to set this in PAHT so that drush command can be run from anywhere, using command replace my-user-name with your own: $ echo 'export PATH="/home/my-user-name/.config/composer/vendor/bin:$PATH"' >> ~/.bashrc $ source ~/.bashrc Now check that drush command is working by running $ drush

Setup development environment for Drupal on windows 10

Being a Drupal developer, I would prefer to work on Mac or Linux (ubuntu) operating system for fast and hassle free development work, but some times you will not get such preferences, may be due to organizations’ norms. Let's say you got a Windows machine for your local development where you have been assigned to a Drupal project. Let's dive into process of installing all required software needed. I am assuming individual have permission to install WSL from organizations administrator team. Following sets of applications will be require in order to setup local environment: Install Windows Subsystem for Linux ( WSL ) Install VS code  along with WSL plugin. Install PHP, MySql, Apache2/Nginx: sudo apt-get install lamp-server^ Git: sudo apt-get install git Composer Drush: composer global require drush/drush That's all you need to start with your develoment You might need to create separate user f...

Drupal views create exposed filter programmatically

Lets assume there is requirement where we need to create a views exposed filter that will filter node content for the selected year. Here I am going to create a module with name custom_views_filter for demonstration Create custom_views_filter.module file and add below code /** * Implements hook_views_data_alter(). */ function custom_views_filter_views_data_alter(array &$data) { $data['node']['year_filter'] = [ 'title' => t('Year'), 'filter' => [ 'title' => t('Year'), 'help' => t('Provides a custom filter for nodes to search yearly.'), 'id' => 'year_views_filter', ], ]; } This will register filter with views plugin, now create views plugin filter. Create folder Plugin/view/filter under src folder and add file with name ContentYearViewsFilter.php , then add below code: ...

Use of Drupal module uninstall validators and lazy services

Sometimes we do not want to uninstall a Drupal module directly, but instead want to perform some checks first. Let's take a scenario where a module creates node types along with some fields on install, later on If a user wishes to uninstall that module (assume some content has already been created on the site using the node type created by this module), then it should warn the user about the data that has already been created on the site using this module. We can use module uninstall validators lazy services, here I am going to take module name as lorem_ipsum for the same. Define lorem_ipsum.service.yml and add below code lorem_ipsum.uninstall_validator: class: Drupal\lorem_ipsum\LoremIpsumUninstallValidator tags: - { name: module_install.uninstall_validator } arguments: ['@string_translation', '@entity_type.manager'] lazy: true Now, create LoremIpsumUninstallValidator.php under 'src' and add b...

Use drush to generate module & drush commands

Image
In this post we are going to learn how to creating module and drush commands using drush. First we will create a module using drush command. Lets run the following drush command and follow the instruction shown in below picture: drush generate module-standard Here we have created a module with name content_processor, now let's create a drush command in same module which will process node content based on provided node type. Run the following drush command to generate command boilerplate code, further we'll modify as per our need. drush generate drush-command-file As we have created a drush command boilerplate code along with module the structure should look like below: Now, let's update the ContentProcessorCommands.php class code by adding below functions: namespace Drupal\content_processor\Commands; use Drush\Commands\DrushCommands; /** * A Drush command file. * * In addition to this...

Setup Drupal 9 with acquia BLT 12

Setup Drupal 9 with Acquia BLT 12: First we will create a drupal 9 project with recommended template, using below command. $ drupal/recommended-project blt-drupal --no-install This will create a project with name blt-drupal, since acquia BLT require a document root to be docroot, so lets update the project's composer.json file accordingly. Open composer.json and replace the occurrence of web to dcroot Now cd into project folder and add acquia/blt into the project $ cd blt-drupal $ composer require acquia/blt Update your database credentials in local.settings.php Finally run: blt setup We are all set to use the site