logo

Blog

22 September

Step to Configure Gmail Id in Microsoft Outlook
By: admin | Published On: Thu, 09/22/2022 - 23:00
  • Go to Gmail from your browser, then select the Google apps icon in the upper right corner of the screen.

1

  • Select your account.

21 September

Get or Set config value programmatically Drupal 8
By: alam | Published On: Wed, 09/21/2022 - 13:56

Get value of variable from configuration form

You can get config value in any custom code using following method

Example 1

$phone = \Drupal::config('socialmedia.settings')->get('phone');

In above example "socialmedia.settings" is const which has been defined in configuration form and "phone" is variable where stored value

Will get value which have beed set through configuration form.

Example 2

$slogan = \Drupal::config('…
				  

26 August

Method to Get Username Programmatically Drupal 8
By: admin | Published On: Fri, 08/26/2022 - 19:46

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…
				  

13 August

Programmatically get term list by Vocabulary in Drupal 8
By: admin | Published On: Sat, 08/13/2022 - 20:37
	$vid = 'Vocabulary machine name';	// Vocabulary machine name for example city, state etc. 
	$terms =\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
	foreach ($terms as $term) {
	 $termlist[$term->tid] = $term->name;
	}

19 January

FTP or FileZilla not working with Airtel Broadband
By: admin | Published On: Wed, 01/19/2022 - 10:22

Sometime we face FTP connection issue with AIrtel Broadband so follow this step to resolved this issue.

Open the IP Address 192.168.1.1 in browser
Login with password given by Airtel
Go to Services Tab

Service Tab Airtel
Click on Left Menu of Firewall then Firewal menu will drop…

28 December

Query for user reference field Drupal 8
By: admin | Published On: Tue, 12/28/2021 - 14:28
    $query = \Drupal::database()->select('node', 'n');
    $query->fields('n', ['nid','type']);
	$query->condition('type', array('basic'), 'IN');		// Basic is content type
	$query->leftJoin('node__field_user_ref', 'UID', "n.nid = UID.entity_id");	// field_user_ref is entity field for user reference 
	$query->condition('UID.field_user_ref_target_id', NULL, 'IS NOT NULL');
    $pager = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(50);
    $results = $pager->…
				  

16 December

Programmatically get user picture/image in Drupal 8 or 9
By: admin | Published On: Thu, 12/16/2021 - 07:53

Following code can be used to get user picture or image by loading user data in Drupal 8

$user = \Drupal\user\Entity\User::load($uid);
$imgpath = $user->get('user_picture')->entity->uri->value;
if(!empty($imgpath)){
	$user_pic = file_create_url($picurl);
}

6 December

Programmatically add comment drupal 8
By: admin | 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
By: alam | 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

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);	
	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

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

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

    $this->messenger…
				  

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)

Example

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

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.

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

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