Latest Articles

Our latest news updates and insightful blog posts, designed to empower you with the knowledge and strategies you need to succeed.

Programmatically get user picture/image in Drupal 8 or 9

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

Programmatically get term list by Vocabulary in Drupal 8

	$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;
	}

Programmatically count the number of products in the cart Drupal Commerce 10

p>In Drupal Commerce 10, you can programmatically count the number of products in the cart. Below is an example of how you can achieve this using Drupal’s Commerce API.

 

use Drupal\commerce_order\Entity\Order;
	$store_id = 1;
    $order_type = 'default';
    $cart_manager = \Drupal::service('commerce_cart.cart_manager');
    $cart_provider = \Drupal::service('commerce_cart.cart_provider');
    $entity_manager = \Drupal::entityTypeManager();
    $store = $entit…

Programmatically add comment drupal 8

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.
			'subject' =…

Programatically upload or update files in Node filed Drupal 8

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.

forea…

Programatically submit data into Webform in Drupal 8

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

Programatically add content to webform DRUPAL

global $user;
$nid = 4; //nid is the node id of your webform.

$node = node_load($nid); 

The values to save. Take case about array indexes! (see below)

$data = array(
    '1' => array('0' => $type),
    '2' => array('0' => $method),
    '5' => array('0' => $volume),
    '6' => array('0' => $comment),
    '7' => array('0' => $phone),
    '8' => array('0' => $length)
);

$submission = (object) array(
    'nid' => $nid,
    'uid' => $u…

Print Status or Error Message in Drupal 9

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()->addMe…

PHPMailer using composer in PHP or Drupal

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

Page Template for Views Page

If you want to create template for views page like page.tpl.twig so you follow these step. Whole page template for views page.

  1. Create copy of page.tpl.twig
  2. If your views url is ‘news-and-articles’ then rename file as ‘page--news_and_articles.html.twig’ and upload in your themes folder. If url has dash then replace dash with underscore.

After clear cache current template will work for this views instead of page.tpl.twig.

Page template for Content Type, Node ID

function mythemename_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  // Get Request Object.
  $request = \Drupal::request();
  // If there is HTTP Exception..
  if ($exception = $request->attributes->get('exception')) {
    // Get the status code.
    $status_code = $exception->getStatusCode();
    if (in_array($status_code, array(401, 403, 404))) {
      $suggestions[] = 'page__' . $status_code;
    }
  }
  if ($node = \Drupal::routeMatch()->getPara…

node template cheat sheet Drupal 9 and Drupal 10

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 node t…