logo

Blog

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

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.
			'subject' => $username,  				// required.
			'comment_body' => $msg,            		// optional…
				  

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

6 September

List of currency code with country code in PHP Array
By: admin | Published On: Mon, 09/06/2021 - 13:35
		$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' => 'XOF',
			'BM' => 'BMD',
			'BT' => 'BTN',
			'BO' => 'BOB',
			'…
				  

5 September

Programatically submit data into Webform in Drupal 8
By: alam | Published On: Sun, 09/05/2021 - 22:35

Folowing code you can use to save data in webform in Drupal 8

In following code used webform id as "contact_us",

		$webform_id = 'contact_us';	// here use can use that webform id where to save data
		$webform = Webform::load($webform_id);
		// Create webform submission.
		$values = [
		'webform_id' => $webform->id(),
		'data' => [
		'name' => $name,
		'phone' => $phone,
		'email' => $email,
		],
		];

		/** @var \Drupal\webform\WebformSubmissionInterface…
				  

30 August

Message Format in Drupal 8
By: admin | Published On: Mon, 08/30/2021 - 12:29

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.'));
		}


16 August

To remove index.php from URL in Drupal 8
By: alam | Published On: Mon, 08/16/2021 - 13:49

By default adding /index.php/ in URL so how to remove index.php from URLs in Drupal 8

I have added the following code in .htaccess file then the issue has been resolved

RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

 

Other method you can try to stop indexing this type of URLs in Google or other search engine

Add the following code in robots.txt

Disallow: /index.php/*