Latest Articles

Our latest news updates and insightful blog posts, designed to empower you with the knowledge and strategies you need to succeed.

Print Status or Error Message in Drupal 9

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

    $this->messenger()->addMe…

PHPMailer using composer in PHP or Drupal

  • Connect to your account via an SSH client.
  • Open PuTTY and enter your SSH information in the Host Name (or IP address) and Port fields. Then, click Open.
  • Once a command window appears, type in your SSH username and password and hit Enter. Remember that PuTTY will not display the password, so don’t be surprised if it doesn’t appear on the screen.
  • Execute the following command to navigate to the public_html directory:
cd public_html
  • Run the…

Page Template for Views Page

If you want to create template for views page like page.tpl.twig so you follow these step. Whole page template for views page.

  1. Create copy of page.tpl.twig
  2. If your views url is ‘news-and-articles’ then rename file as ‘page--news_and_articles.html.twig’ and upload in your themes folder. If url has dash then replace dash with underscore.

After clear cache current template will work for this views instead of page.tpl.twig.

Page template for Content Type, Node ID

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()->getPara…

node template cheat sheet Drupal 9 and Drupal 10

How to get field value of content type on node.html.twig

Get image field value at node.html.twig at Drupal 9 / 10

{{ file_url(node.field_image.entity.fileuri) }}
{{ node.field_image.alt }}

Get node crated date at node.twig.tpl

{{ node.getCreatedTime|date("d/m/Y")}}

Get term name of reference field in node

In following example field_company_name is a term rerence field in a content type so need to render that reference term name at node t…

Node field twig template multiple images/item

Node field twig template to display multiple images/item. We can theme/design each item for that field

I am using node template as follow,

node--mycontenttype.html.twig

For example my content type name is "banner" then template name as.

node--banner.html.twig

I added field as below and its display all images but Its not able to wrap each images with div so that you can add classes to create slider.

{{ content.field_gallery_images…

Method to Get Username Programmatically Drupal 8

There are several ways to get the user's name programmatically.

$uid = 1;	// For example I have get detail of user ID 1
$account = \Drupal\user\Entity\User::load($uid); // pass your uid

Method 1

drupal_set_message($account->name->value);  

Method 2

drupal_set_message($account->get("name")->value); 

Method 3

drupal_set_message($account->getUsername());

Method 4

drupal_set_message($account->name->ge…

Message Format in Drupal 8

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');
$form_state->setErrorByName('error',$message);	
		if ($this->validEmailAddress($form_state->getValue('email_address'))) {
			$form_state->setErrorByName('email_address', t('User exist with given Email address.'));
		}


Load taxonomy detail by term id in Drupal 10

If you have already term then you can get all other tern detail by given term ID

$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load(TID);
$field_value = $term->FIELD_MACHINENAME->value;
$term_name= $term->name->value;

List of currency code with country code in PHP Array

		$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' =…

List of common Node Variables in node.html.twig in Drupal 10

In Drupal 10, when working with the node.html.twig file, there are several useful variables available for rendering nodes. These variables provide access to the node entity, fields, metadata, and other useful information, making it easier to customize the way content is displayed.

Here’s a list of common Node Variables in node.html.twig in Drupal 10:

Core Node Variables

  1. node:
    • The full node…

Install razorpay library using composer for payment gateway integration

I was doing integrate razorpay payment gateway in Drupal 8 using custom code but we getting error like “Class 'Razorpay\Api\Api' not found” because razorpay library is not installed on server so follow this step to install using putty to run composer command.

In putty enter Server IP and port click on open

T…