logo

Blog

4 September

Get node id of current node page Drupal 10

Published On: Wed, 09/04/2024 - 12:11
	$node = \Drupal::routeMatch()->getParameter('node');
	if ($node instanceof \Drupal\node\NodeInterface) {
	  // You can get nid and anything else you need from the node object.
	  $nid = $node->id();
	}

25 August

PHPMailer using composer in PHP or Drupal

Published On: Sun, 08/25/2024 - 21:08
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 following command to install…

20 August

node template cheat sheet Drupal 9 and Drupal 10

Published On: Tue, 08/20/2024 - 22:36
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…

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

 

\Drupal::service('file_url_generator')->generateAbsoluteString($uri);

 

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

Problem:

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
		$query->condition('status',1);
		$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…