Search Results

In Drupal 8 custom token can be created in following way :

It is good practice to write code related to tokens in tokens.inc file. Include the tokens.inc file in your module in following way.

module_load_include('inc', 'custom_module', 'custom_module.tokens');

Now in your custom_module.tokens.inc file, extend the hooks => hook_tokens_token_info and hook_tokens in this way.

To do partial match searching in Drupal 8, index the required field as Fulltext NGram and alter the schema.xml in such a way : 

Replace this 

In Drupal 8 a taxonomy term can be loaded in the following way.

$term = \Drupal\taxonomy\Entity\Term::load($termId);

Where $termId is taxonomy term ID. Now to get name of taxonomy term, use getName() function.

\Drupal\taxonomy\Entity\Term::load($termId)->getName();

 

Following regex can be used to determine if the user is using mobile or not.

echo preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);

Global array  $_SERVER["HTTP_USER_AGENT"] can also be printed to get user information.

When we install Drupal 8 website from the beginning we set super admin login credentials. But sometimes we get codebase and database dump of an existing website which was set up by someone else.This video explains how you can log in to existing drupal8 website.

By default meta tag, module  use node summary/body token to populate meta description of the content type having body field. It does not follow the standard practice of meta description of length between 135 to 150 characters. Even if you set summary length between 135 and 150, your meta description might end with incomplete word and provide a bad search engine preview. You can create a custom token which meets both the criteria i.e 1. The length between 135 and 150 and  2. The last word might be incomplete because of the character limit. You can create a custom token in following way: 

In Drupal 8 you can attach libraries to block markup in following way: 

return
[ '#markup' => $markup, '#attached' => [ 'library' => 'custom_module/custom_library' ]
];

 

Form cache in Drupal 8 can be disabled in following ways.

$form_state->setCached(FALSE);

And 

$form['#cache'] = ['max-age' => 0];

 

Field collection can be used in case a field has subfields. For eg Name can have subfields such as First Name, Middle Name, and Last Name. There might be a requirement of loading field collection items programmatically. It can be done in following way:

$fieldCollection = \Drupal\field_collection\Entity\FieldCollectionItem::load($fieldCollectionId);
$firstName = $fieldCollection->get('field_first_name')->getValue();

It is better to use Paragraph module instead of using field collection, becuase it has better features and is being well maintained.

There might be a situation when you have to print a block at a specific location on the webpage. In that case, you can load the block and set up a variable which will be printed in twig file. This can be achieved by following way:

Add the following code in right preprocess in the .theme file.

  $block = Block::load('test_block');
  $variables['test_block'] = \Drupal::entityTypeManager()
    ->getViewBuilder('block')
    ->view($block);

Now in your twig file you print test block in following way {{ test_block }}.