logo

Blog

13 October

Redirect website to HTTPS Drupal

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

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

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

Now using following code to create user, fields name and…

6 September

List of currency code with country code in PHP Array

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

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

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

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

13 July

Custom code on comment publish in Drupal 8

Published On: Tue, 07/13/2021 - 16:43

Custom code on comment publish in Drupal 8

If you want to add your custom code on published of comment then we can use hook"hook_comment_update"

"$comment->isPublished()" will check comment in published

For example you want to send mail when comment published or unpublished so you can follow these step

Open your .module file and add the following code, for example your module name is "mymodule" then

 

function mymodule_comment_update…
				  

7 July

Get URL parameters/argument in custom module Drupal 8

Published On: Wed, 07/07/2021 - 14:44

If you created URL with parameters in custom module as given example

http://localhost/drup/member/179

In example you need to access the value 179 in your code then follow these step

Use this line at top

Use \Drupal\Core\Routing;

You can use this code so its return all parameters but its return as object so again you need to load that object

You can use that code with named parameters as follows

$value = \Drupal::routeMatch…
				  

5 July

I got following error when installation Drupal 9

PHP EXTENSIONS Disabled Drupal requires you to enable the PHP extensions in the following list (see the system requirements page for more information):

gd

So resolved the issue by following these step

Open the php.ini which is located at c:\xampp\php\ Find "extension=gd" in php.ini so you can found this line but its seen comment so you need to uncomment this line, to uncomment remove the ";" which is prefix…