Latest Articles
Our latest news updates and insightful blog posts, designed to empower you with the knowledge and strategies you need to succeed.
Show Related Content by Category Using Views Drupal 8 or 9
Follow these step to make block using views to show the related content according to category of the current content page.
Edit the views and open Advance section
- Add the 'Content: Has taxonomy term ID' in CONTEXTUAL FILTERS.
- Check 'Provide default value' option and then select the 'Taxonomy term ID from URL'.
…
Set default value user name in user login form using form alter drupal 8
To set a default value for the username field in a user login form using a form alter hook in Drupal 8, you can use the following code:
/** * Implements hook_form_alter(). */ function yourmodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { if ($form_id === 'user_login_form') { // Set the default value for the username field. $form['name']['#default_value'] = 'your_default_username'; } }
Replace 'yourmodule'
wi…
Set current date in custom form element in DRUPAL8
Set current date in custom form element in DRUPAL8
Add this line at top
use Drupal\Core\Datetime\DrupalDateTime;
Add the following code to your form
function HOOK_form_alter(&$form, FormStateInterface $form_state, $form_id) { if ($form_id == 'node_article_form') { $form['field_date']['widget'] = array( '#type' => 'datetime', '#title' => 'Enter Date', '#required' => TRUE, '#default_value' => DrupalDateTime::cre…
Select Query to get users of empty fields
Select those users which name (Custom field) is empty
In the following I have created a custom field (name) in user profile, field machine name as field_name
I want get user list where name field is blank
$uids = \Drupal::entityQuery('user') ->condition('field_name', NULL, 'IS NULL') ->execute(); echo count($uids);
If you want to get more user through user id
foreach($uids as $uid){ $users = User::load($uid); $users->get('na…
Return error on Zoom API with custom_questions in Drupal 8
Prob:
in Drupal 8 I have created custom code to post data in Zoom using ZOOM API.
I have added custom question in Zoom Dashboard as below
Zoom API was working but when I added a custom question in Event then is showing an error as below…
Render Field Variable In Node Twig Template (Node.html.twig) Drupal 8
Render Field Variable In Node Twig Template (Node.html.twig) Drupal 8
Show Node Created Date
If you want to show node created date on template you can use code as below
{{ node.getCreatedTime|date("d/m/Y")}}
You change date format as your requirement
Display Title of node
{{ node.title.value }}
Print node url
{{ url }}
Show Term Reference Field
To display reference term name the you can use code as below
{{ node.f…
Redirect website to HTTPS Drupal
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}%{REQUEST_URI} [L…
Redirect Method in DRUPAL8
To redirect on node add form or node creation form
$form_state->setRedirectUrl(Url::fromUri('internal:' . '/node/add/business'));
If you want to pass argument with URL
Example:
$params['query'] = [ 'name' => $name, 'id' => $id, ]; $form_state->setRedirectUrl(Url::fromUri('internal:' . '/node/add/business', $params));
Method to redirect on node id url
$url = Url::fromRoute('entity.node.canonical…
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;