Drupal

How can I add body class based on path / page specific class - Drupal 9?

It's a simple 3 line code to achieve adding the body class based on the URL. Sometimes, we need to overwrite the existing styles based on the page. so, we need the page specific class.

Go to your THEME_NAME.theme file & add this below HOOK.

/**
 * Implements hook_preprocess_html().
 */
function THEME_NAME_preprocess_html(&$variables) {
  // Get the current path
  $current_path = \Drupal::service('path.current')->getPath();
  $internal_path = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);

  // Assign it to body class 
  $variables['attributes']['class'][] = str_replace("/", "", $internal_path);
}

Make it simple :)

 

How can I import the configuration files from different site?

How to get and set site uuid drupal 8/9?
 

Problem:

  When you try to import configuration using "drush cim", you will get the message like this "[error]  Drupal\Core\Config\ConfigImporterException: There were errors validating the config synchronization.
  Site UUID in source storage does not match the target storage."

The Configuration files only allow sync between same site to avoid issues importing configuration from one site to another.
We can import configuration from other websites into yours with Fresh drupal installation. Each Drupal installation uuid is unique.

Solution:

Install the fresh Drupal version. then follow the below steps.

Get spam emails from your drupal contact webforms?

Get spam emails from your contact forms?

Mostly you can find the same words in those spam emails, in this module you can set spam words for each fields.
Example for words that common in spam emails: “Eric Jones”, “SEO Ranking”, “Digital marketing”, "SEO" ...etc

I'm sure if you have a contact form, you have already gotten one of his/her/it spam. 

We are receiving daily SPAM from Eric Jones (probably a fake name) from talkwithwebvisitor.com and if you are reading this, probably you are, too.  there are still loads of annoying emails like that which get through. Eric Jones SPAM from talkwithwebvisitor.com

try to Avoid these kind of spam keywords on Drupal webform, Use this module to block using the keywords.

Webform Spam Words in Drupal to Block spam based on keyword strings

Webform Spam Words Drupal (WSW)

Module: https://www.drupal.org/project/webform_spam_words

Avoid Spam words on Webform Drupal
It's a simple module for webform validation with spam keywords. Administrators can provide the access to add spam keywords, Error message and field name.

Installation

  1. Place the module folder in your modules folder or try with composer
  2. Make sure you have the webform module enabled or try with drush en
  3. Activate the module via admin/modules

Usage Steps

Install old version composer / incompatible with composer

ThirstySix

Issue
You are using Composer 2, which some of your plugins seem to be incompatible with. Make sure you update your plugins or report a plugin-issue to ask them to support Composer 2.

Solution
You are using Composer 2, some plugins seem to be incompatible with composer

With specific version 1.10.17:
C:\Users\ThirstySix> composer self-update 1.10.17

composer self-update 1.10.17
 or
C:\Users\ThirstySix> composer self-update --1
composer self-update --1

Drupal 7 global variables vs. Drupal 8 services

Several Drupal 7 global values like global $language and global $user are also now accessed via services in Drupal 8 (and not global variables).

Drupal::languageManager()->getCurrentLanguage() and Drupal::currentUser()

Two ways to get the currentUser data:

1) Procedural code
$user = \Drupal::currentUser();
ksm($user->id());

(or)

2) OO code access the @current_user service, via dependency injection (DI)
$service = \Drupal::service('thirstysix.ex');
ksm($service->DisplayUserId());

How to index data in the database for search?

- Indexing is an important feature of MySQL. It supports different type of indexes like primary key index, unique index, normal index and full-text index. Indexes help to speed up the retrieval of data from MySQL database server.

- Drupal's search engine indexes the text content of the nodes on your site

- The search engine indexes at intervals you choose by setting "cron runs."  Cron is not a part of Drupal. It's a scheduler that resides on your server and runs tasks at intervals, which you specify. 

how does search works in drupal?

Site-wide Boolean keyword search of Nodes and User names

- Indexing isn't instant in the drupal search
- Your new content will not be indexed instantly after it has been added to the site.
- Go to Configuration > Search settings click the "Re-index site" button.
- The correct way to get your site's content indexed is to go to Configuration > Cron. Drupal's Search module requires a cron job to run correctly.

Search Query Types
- AND Searches
- OR Searches

How does memcache store data?

  Memcached's APIs provide a giant hash table distributed across multiple machines. When the table is full, subsequent inserts cause older data to be purged in least recently used (LRU) order.

  The servers keep the values in RAM; if a server runs out of RAM, it discards the oldest values. Therefore, clients must treat Memcached as a transitory cache; they cannot assume that data stored in Memcached is still there when they need it.