23 April

Load taxonomy detail by term id in Drupal 10

Published On: Tue, 04/23/2024 - 13:29

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;

15 April

Cheat Sheet for using Twig Tweak in Drupal 10

Published On: Mon, 04/15/2024 - 17:52

1. **Render a View**:

- Use `drupal_view('view_name', 'display_id')` to embed a view.

- Replace `'view_name'` with the actual view name and `'display_id'` with the desired display (default is `'default'`).

- Example: `{{ drupal_view('who_s_new', 'block_1') }}.

2. **Get the Result of a View**:

- Retrieve the result of a view using `drupal_view_result('view_name', 'display_id')`.

- Parameters are the same as for rendering a view.

1 February

Functions file_create_url() not working in Drupal 10

Published On: Thu, 02/01/2024 - 11:44

Functions file_create_url() was using to create url of images and files drupal 9 but now its not working in Drupal 10 so its deprecated now you can use code as follow to create url in Drupal 10




Used in Drupal 9 and before Using Drupal 10 file_create_url($uri) \Drupal::service('file_url_generator')->generateAbsoluteString($uri)…

13 December

entityQuery not working return error in Drupal 10

Published On: Wed, 12/13/2023 - 21:49


I am using entityQuery to get Node by using following method

		$query = \Drupal::entityQuery('node');
		$query->condition('type', 'tours'); // Limit the type of node to check
		$nids = $query->execute();

but its return following error

Drupal\Core\Entity\Query\QueryException: Entity queries must explicitly set whether the query should be access checked or not. See Drupal\Core\Entity\Query\…

30 October

How to remove trailing slash from URL Drupal 10

Published On: Mon, 10/30/2023 - 16:26

You can remove trailing slashes from URLs using the .htaccess file and mod_rewrite

Add the following code to your .htaccess file to Remove Trailing slashes from all URL of website.


RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]

This code will remove the trailing slash from URLs and issue a 301 redirect, so if someone accesses a URL with a trailing slash, they will be redirected to the same URL without the trailing slash.

Make sure you have the…

21 September

Get error undefined method File::url() in Drupal 9

Published On: Thu, 09/21/2023 - 16:44

I was trying to create a custom banner based on user uploaded image in node then I got following error in Drupal 9 but it was workiing in Drupal 8  

Following code I was using

$img_url = $file->url();

Getting this error

Error: Call to undefined method Drupal\file\Entity\File::url() in theme_preprocess_page() 

I found the solution its working fine in simply use createFileUrl(); instead of url();


31 July

To update the text field value of a content type in Drupal 9 using a batch process, you can follow these steps:

Create a custom module: If you don't have one already, create a custom module in the 'modules/custom' directory of your Drupal installation.

Define a form to receive the user input: Create a form in your custom module that takes the new value for the text field as input from the user. This form will be used to trigger the batch process.

Implement the batch…

27 July

How render term field on node template in drupal 9

Published On: Thu, 07/27/2023 - 16:56

In Drupal 9, you can render a term field on a node template using the field_view_field() function. This function takes the following parameters:

Entity type: The type of the entity (in this case, it's "node"). Entity: The node object that contains the term field. Field name: The machine name of the term field you want to render. Display mode: The view mode you want to use to render the field (e.g., "full", "teaser", "default", etc.).

Here's an example of how to render a term…

23 June

To set a default value for the username field in a user login form using a form alter hook in Drupal 8, you can use the following code:

 * Implements hook_form_alter().
function yourmodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id === 'user_login_form') {
    // Set the default value for the username field.
    $form['name']['#default_value'] = 'your_default_username';

Replace 'yourmodule' with the…

1 June

What is the purpose of Ctools in Drupal 9

Published On: Thu, 06/01/2023 - 17:53

In Drupal 9, Ctools (Chaos tools) is a suite of developer tools and APIs that provide various functionalities to enhance and simplify module development. Ctools is not specific to Drupal 9 and has been widely used in previous versions as well.

The primary purpose of Ctools is to provide a set of reusable utilities and APIs that help developers build complex and interactive features in Drupal. Some of the key features and purposes of Ctools in Drupal 9 include: