Get the taxonomy term ID from its name?

Here’s a Drupal 8/9/+ answer, designed as a static PHP class method:

You can add this method in any class and can call using self operator.

Example: self::getTidByName(“term-name”, “vocabulary machine name”);

This will return term id if given term name matches, else it will return 0 if not matched.

 /**
   * Utility: find term by name and vid.
   *
   * @param string $name
   *   Term name.
   * @param string $vid
   *   Term vid.
   *
   * @return int
   *   Term id, or 0 if none.
   */
  public static function getTidByName($name = NULL, $vid = NULL) {
    if (empty($name) || empty($vid)) {
      return 0;
    }
    $properties = [
      'name' => $name,
      'vid' => $vid,
    ];
    $terms = \Drupal::service('entity_type.manager')->getStorage('taxonomy_term')->loadByProperties($properties);
    $term = reset($terms);
    return !empty($term) ? $term->id() : 0;
  }

With this function, you can create new term if this functions returns 0, Like below

$term =Term::create([ 'name' =>$term_name, 'vid' => $vid, ])->save();

Alter and redirect after saving the Drupal form

<?php

/**
 * @file
 * This is my module.
 */

use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * Implements hook_form_alter().
 */
function MyModule_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'give here form id') {
    $form['actions']['submit']['#submit'][] = '_MyModule_redirect';
  }
}

/**
 * Set the redirect here.
 */
function _MyModule_redirect($form, FormStateInterface $form_state) {
  $form_state->setRedirect($url); //you can redirect using URL

                (Or)
  $form_state->setRedirect('route.id'); //You can redirect using routing
}

Drupal8: How to alter language link in the switcher block

There are more than one ways available to alter the language item link in the language switcher block. We will see what are the ways in the below,

  1. Using hook_preprocess_links__language_block() in the theme file
  2. Using hook_language_switch_links_alter() in the module file
  3. Using contrib module “Disable Language.

hook_preprocess_links__language_block()

Suppose if we want to remove the english language from the drop down, then here is the code, we can alter this code as per our needs.

function cherry_preprocess_links__language_block(&$variables) {
foreach ($variables[‘links’] as $i => $link) {
$linkLanguage = $link[‘link’][‘#options’][‘language’];
if($linkLanguage->get(‘id’) == ‘en’) {
unset($variables[‘links’][$i]);
}
}
}

Using hook_language_switch_links_alter()

Suppose if we want to unset the current language code, then here is the code. We can alter this hook as per our needs.

function my_module_language_switch_links_alter(&$variables) {
foreach ($variables as $i => $link) {
$linkLanguage = $link[‘language’];
if ($currentLanguageCode == $linkLanguage->get(‘id’)) {
unset($variables[$i]);
}
}
}

Using these hooks, we can set active class to the current language item link or to add any extra attributes to pass to twig file.

Drupal8: Content Import from a CSV file into any content type

Hi all,

Today i am going to share a module which help us to import content the easily into the application using a simple CSV file.

Actually, this module was created by friend Dhayanandan and all the credits goes to him only.

He has given complete step by step procedure to import the content into the application.

URL: https://www.drupal.org/project/contentimport

This module has reduce complete manual work in creating content in the admin page. Within a few seconds, you can import more number of contents.

And this module has got more popularity and enhancement request from various people.

So, those who required to import content easily, can give a try and provide your valuable feedback.

 

Drupal8: Add class to body tag

Here is the solution for adding our custom class to body tag using preprocess_html() function.

function hook_preprocess_html(&$variables) {
$variables[‘attributes’][‘class’][] = ‘class-name’;
}

If you want to add class for all node of specify content type, you can do like below

function hook_preprocess_html(&$variables) {
if ($node = \Drupal::request()->attributes->get(‘node’)) {
if($node->getType() == ‘content_type_name’) {
$variables[‘attributes’][‘class’][] = ‘class-name’;
}
}
}

Hope this will helpful to you…

 

« Older entries