logo

Blog

6 December

Programmatically add comment drupal 8

Published On: Mon, 12/06/2021 - 16:10

Add this line in head

use Drupal\comment\Entity\Comment;

These are following code format which can use to add comment through custom code in Drupal 8 or 9

		$values = [
			'entity_type' => 'node',            	// required. 
			'entity_id'   => $nid,                	// required.       
			'field_name'  => 'comment', 			// required.
			'uid' => $uid,                         	// required.
			'comment_type' => 'comment',        	// required.
			'…
				  

22 November

Get URL by Node id Drupal 8

Published On: Mon, 11/22/2021 - 17:31

you have to use following class

Drupal\Core\Url

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

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.

In my example trying to to through folrm_alter hook. Following code using hide URL Aliasing for the content type of Article.

	function MODULENAME_form_alter(&$form, FormStateInterface $form_state, $form_id) {
		
		if ($form_id == 'node_article_edit_form' || $form_id == '…
				  

1 November

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

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"

 

So I have found the solution

In settings.php search the following tag

$settings['file_private_path']

I have change them as below.

$settings['file_private_path'] = 'sites/default/files/private';

25 October

Programmatically Login in Drupal 9

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);	
	user_login_finalize($user);

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

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

 options:
    no_cache: 'TRUE' 

Complete example as below

mymodule.form:
 path: '/mycustompath'
 defaults:
   _title: ''
   _form: '\Drupal\workassign\Form\MyTestForm'
 requirements:
   _permission: 'mymodule custom'
 options:
    no_cache: '…
				  

21 October

Print Status or Error Message in Drupal 9

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

    $this->messenger…
				  

18 October

Templates Example and Template Name Drupal 8 and 9

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)

Example

html--[internalviewpath].html.twig 
html--node--[nodeid].html.twig 
html.…
				  

18 October

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.

$node->save();

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.…