Latest Articles
Our latest news updates and insightful blog posts, designed to empower you with the knowledge and strategies you need to succeed.
Redirect domain to WWW in Wordpress
Set site address correctly in your admin Settings -> General section
You can see in following example. You can set domain with www or without www.
Query for user reference field Drupal 8
$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); $resul…
Query according to field condition in DRUPAL 8
Code to query for getting node according to field condition in DRUPAL 8
$query = \Drupal::entityQuery('node') ->condition('status', NODE_PUBLISHED) ->condition('type', 'custom_type'); $and = $query->andConditionGroup(); $and->condition('custom_taxonomy', 2); $query->condition($and); $and = $query->andConditionGroup(); $and->condition('custom_taxonomy', 8); $query->condition($and); $result = $query->execute();
Programmatically Login in Drupal 9
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;
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…