22 November

Get URL by Node id Drupal 8
By: alam | Published On: Mon, 11/22/2021 - 17:31

you have to use following class


You can use folowing code to get URL Alias

		$options = ['absolute' => TRUE];
		$url = \Drupal\Core\Url::fromRoute('entity.node.canonical', ['node' => $nid], $options);
		$nodeurl = $url->toString();

12 November

Custom code to disable or hide Node Attribute or Tabs in Drupal 8 or 9
By: alam | Published On: Fri, 11/12/2021 - 19:36

When you create or edit a node there is some default option or attribute in Node Form so we can disable or hide through custom code by using following code in our custom module.

Node Form Attribute or Tabs in Drupal 8

In my example trying to to through folrm_alter hook. Following code using hide…

1 November

Get field value of reference node on node twig template Drupal 8 and 9
By: admin | Published On: Mon, 11/01/2021 - 16:45

In following example field_course_ref is a field which reference with other node, field_course_type is a field of the reference content type which is a term so we get term name

{{ node.field_course_ref.entity.field_course_type.entity.name.value }}

30 October

In Google Index API get an error private://google_index_api/ is invalid
By: admin | Published On: Sat, 10/30/2021 - 13:33

Google Index API module installed but on configuration screen get following error when upload jason file. "The file could not be uploaded because the destination private://google_index_api/ is invalid"


Google Index API

So I have found the solution

In settings.php…

25 October

Programmatically Login in Drupal 9
By: admin | Published On: Mon, 10/25/2021 - 21:03

You can use the function (user_login_finalize) in your custom code.

Example code to login which you can usein your custom code.

$uid is your user id for which you want to login

	$user = User::load($uid);	

You must be use following code in your namespace. If you don't use then user_login_finalize will not work in Drupal 9

use Drupal\Core\DrupalKernel;

25 October

Disable cache for specific page routing in custom module in Drupal 8 or 9
By: admin | Published On: Mon, 10/25/2021 - 19:10

Example is mentioned below where I have created a path (/mycustompath) so I want disable cache for this page then I have add following syntax with my code in the file (mymodule.routing.yml) or in your routing yml file

    no_cache: 'TRUE' 

Complete example as below

 path: '/mycustompath'
   _title: ''
   _form: '\Drupal\workassign\Form\MyTestForm'
   _permission: 'mymodule custom'
    no_cache: '…

21 October

Print Status or Error Message in Drupal 9
By: admin | Published On: Thu, 10/21/2021 - 20:36

Print message in Drupal 8, We use following method in Drupal 8 to print message but this code is not work in Drupal 8.

    drupal_set_message('Hello world');
    drupal_set_message('Hello world', 'error');
    drupal_set_message('Hello world', 'status');
    drupal_set_message('Hello world', 'warning');

Print message in Drupal 9. Following syntax can use to print message in custom code, Error Message, Status Message, Waring Message


18 October

Templates Example and Template Name Drupal 8 and 9
By: admin | Published On: Mon, 10/18/2021 - 17:51

Drupal loads templates based on certain naming conventions. This allows you to override templates by adding them to your theme and giving them specific names.

HTML Template for Head Tag

The HTML template provides markup for the basic structure of the HTML-page including the ,and tags.

Base template: html.html.twig (base location: core/modules/system/templates/html.html.twig)



18 October

Programatically upload or update files in Node filed Drupal 8
By: alam | Published On: Mon, 10/18/2021 - 16:58

When updating file in existing node

here field name is "field_file" in node.

$node = \Drupal\node\Entity\Node::load($nid);	// $nid is Node ID 

$node->set('field_file' , $fid);	// $fid is file id which have to update in node file field.


If you have to update files in multiple field value

In example "field_file" field has option to upload multiple files then we can use following code to upload/update multiple files in file field.…

13 October

Redirect website to HTTPS Drupal
By: alam | Published On: Wed, 10/13/2021 - 16:37

Forcely redirect the website URL to SSL

If your website is opening with both form With HTTPS and without HTTPS but you want to redirect URL to with HTTPS URL then following step you can use. I have applied this method in Drupal 8 and Drupal 9 website

If SSL enabled in your website then you can use following code in htaccess file

# Redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{…

22 September

Display Fields Value in Drupal commerce product template in Drupal 8
By: admin | Published On: Wed, 09/22/2021 - 19:45

How can display fields value in product template of Drupal commerce in Drupal 8

First have to create template with name "commerce-product.html.twig"

Some examles of fields value which can be use in template to display output

Display user name of product author

{{ product_entity.getOwner.getUsername }}

Display user id of product author

{{ product_entity.getOwnerId }}

Display Product title

{{ product.title }}

15 September

Crreate user programatically in Drupal 8
By: alam | Published On: Wed, 09/15/2021 - 22:31

Using following step and method to create user programatically

Must be use this line in above the code

use Drupal\user\Entity\User;

I have added two extra fields in user account that is mentioned below.

Phone - This is text type field. field_phone (Machine name)

Subscribe - This is multiple select type field has machine name as field_subscribe. Following options in

6 September

List of currency code with country code in PHP Array
By: admin | Published On: Mon, 09/06/2021 - 13:35
		$currency = array(
			'AF' => 'AFN',
			'AL' => 'ALL',
			'DZ' => 'DZD',
			'AS' => 'USD',
			'AD' => 'EUR',
			'AO' => 'AOA',
			'AI' => 'XCD',
			'AQ' => 'XCD',
			'AG' => 'XCD',
			'AR' => 'ARS',
			'AM' => 'AMD',
			'AW' => 'AWG',
			'AU' => 'AUD',
			'AT' => 'EUR',
			'AZ' => 'AZN',
			'BS' => 'BSD',
			'BH' => 'BHD',
			'BD' => 'BDT',
			'BB' => 'BBD',
			'BY' => 'BYR',
			'BE' => 'EUR',
			'BZ' => 'BZD',
			'BJ' => 'XOF',
			'BM' => 'BMD',
			'BT' => 'BTN',
			'BO' => 'BOB',

5 September

Programatically submit data into Webform in Drupal 8
By: alam | Published On: Sun, 09/05/2021 - 22:35

Folowing code you can use to save data in webform in Drupal 8

In following code used webform id as "contact_us",

		$webform_id = 'contact_us';	// here use can use that webform id where to save data
		$webform = Webform::load($webform_id);
		// Create webform submission.
		$values = [
		'webform_id' => $webform->id(),
		'data' => [
		'name' => $name,
		'phone' => $phone,
		'email' => $email,

		/** @var \Drupal\webform\WebformSubmissionInterface…

30 August

Message Format in Drupal 8
By: admin | Published On: Mon, 08/30/2021 - 12:29

Error Message Format


use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormStateInterface;
drupal_set_message(t('Something wrong!'),'error');
		if ($this->validEmailAddress($form_state->getValue('email_address'))) {
			$form_state->setErrorByName('email_address', t('User exist with given Email address.'));

16 August

To remove index.php from URL in Drupal 8
By: alam | Published On: Mon, 08/16/2021 - 13:49

By default adding /index.php/ in URL so how to remove index.php from URLs in Drupal 8

I have added the following code in .htaccess file then the issue has been resolved

RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]


Other method you can try to stop indexing this type of URLs in Google or other search engine

Add the following code in robots.txt

Disallow: /index.php/*

13 July

Custom code on comment publish in Drupal 8
By: alam | Published On: Tue, 07/13/2021 - 16:43

Custom code on comment publish in Drupal 8

If you want to add your custom code on published of comment then we can use hook"hook_comment_update"

"$comment->isPublished()" will check comment in published

For example you want to send mail when comment published or unpublished so you can follow these step

Open your .module file and add the following code, for example your module name is "mymodule" then


function mymodule_comment_update…

7 July

Get URL parameters/argument in custom module Drupal 8
By: alam | Published On: Wed, 07/07/2021 - 14:44

If you created URL with parameters in custom module as given example


In example you need to access the value 179 in your code then follow these step

Use this line at top

Use \Drupal\Core\Routing;

You can use this code so its return all parameters but its return as object so again you need to load that object

You can use that code with named parameters as follows

$value = \Drupal::routeMatch…

5 July

Found error "PHP EXTENSIONS Disabled" with Drupal 9 Installation
By: alam | Published On: Mon, 07/05/2021 - 08:50

I got following error when installation Drupal 9

PHP EXTENSIONS Disabled Drupal 9

Drupal requires you to enable the PHP extensions in the following list (see the system requirements page for more information):

  • gd…

1 July

Page template for Content Type, Node ID
By: admin | Published On: Thu, 07/01/2021 - 12:43
function mythemename_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  // Get Request Object.
  $request = \Drupal::request();
  // If there is HTTP Exception..
  if ($exception = $request->attributes->get('exception')) {
    // Get the status code.
    $status_code = $exception->getStatusCode();
    if (in_array($status_code, array(401, 403, 404))) {
      $suggestions[] = 'page__' . $status_code;
  if ($node = \Drupal::routeMatch()->getParameter('node…