Drupal 8: How to create custom token programmatically

 What is Token:
Token is a small bits of text, which can be placed into a large documents as a simple placeholders
What Token Module Does?
The token module provides an API to provide tokens for other modules. It also provide some additional tokens and provide a browsing facility for all available tokens in UI.
How to create a custom token programatically?

In order to create a custom token, you will first need to create a custom drupal token module which will use the two important parts for this process,

A: Declaring the token (by using hook_token_info()).

hook_token_info(): 

It provides information of available placeholders and Token types

Return Value:

 It returns an array of all available tokens and token type.

B: And providing values for the token (by using hook_tokens()).

hook_tokens(): It provides the replacement value of placeholder tokens.

Parameters:

$type: Machine name of type of token

$tokens: Array of tokens

$data: Array of data objects

$options: Array of options for token replacement

\Drupal\Core\Render\BubbleableMetadata

$bubbleable_metadata: Bubbleable metadata

Return Value: 

It return an associative array of replacement values.

Other Token hooks:

1. hook_token_info_alter():

Parameter:

$data - Array of tokens.

2. hook_tokens_alter()

Parameter:

$replacements - Array of replacements return by hook_tokens().

$context - Array of all keys of hook_tokens().

To create a custom module, first go to your drupal site's module directory and create a new folder there with your custom module name. In this Folder, create a new file with your module's name. Example custom_token.info.yml. In this file implements the following code.


Create Token File :

So, create a file inside your custom module directory and give it the same name as your custom module but with the extension .module. In my case, it will be custom_token.module. In this file, implement the following code:

<?php


/**

* @file

* contains custom_token.module..

*/

use Drupal\Core\Render\BubbleableMetadata;

/*

* Implemets : hook_token_info().

*/

function custom_token_token_info() {

$types['custom_token_type'] = array(

'name' => t('Custom Token Type'),

'description' => t('Define Custom Token Type')

);

$tokens['custom_token_name'] =array(

                  'name' => t('Custom Token Name'),

'description' => t('Define Custom Token Name')

);

return array(

 'types' => $types,

'tokens' => array(

  'custom_token_type' => $tokens

)

);

}

/*

* Implements hook_tokens()

*/

function custom_token_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {

$replacements = [];

if ($type == 'custom_token_type') {

foreach ($tokens as $name => $original) {

switch ($name) {

case 'custom_token_name':

$text = 'My Custom Token';

$replacements[$original] = $text;

break;

}

}

}

return $replacements;

}


The first part of the code that uses the hook_token_info() function is used to declare the tokens as well as 
what function it should perform and make them visible to Drupal.

In the second half of the code, the hook_tokens() function is used to actually make the token perform its desired function.






How to check Deprecated Code in Drupal

 If you are a Drupal developer, and you want to migrate from Drupal 8  to Drupal 9. Then first you want to check "deprecated" code in your Drupal 8 project.

What is deprecated code?

Code in Drupal is marked as "deprecated" when it should no longer be used.

There are a few ways to check if your site is using deprecated code.

1. run drupal-check.

Matt Glaman (Centarro) developed a static PHP analysis tool called drupal-check , which you can run against your codebase to check for deprecated code.

You can install this in your project using Composer as a development dependency like so:

composer require mglaman/drupal-check --dev

You can also install this globally using Composer like so:

composer global require mglaman/drupal-check

For more information about drupal-check. Please look below link

https://github.com/mglaman/drupal-check

2. With drupal contributed module "Upgrade Status"

https://www.drupal.org/project/upgrade_status

If you want to check how Upgrade Status Module work. Please look below link

https://wartalab.blogspot.com/2020/07/upgrading-drupal-8-site-to-drupal-9.html







During drupal 8 & 9 installation fix php opcode caching not enabled

 Opcode Caching (PHP - high CPU)

PHP out of the box is a dynamic language and can lead to heavy CPU usage on web servers. There are multiple types of opcode caching add-ons for PHP available that will convert your .php page into memory (byte code) to provide a major benefit in load time and reduced CPU usage. Each of these will require root access to install and have their own specific configurations that will need some attending before using. Once configured and enabled they can provide a substantial benefit to a slow site and greatly reduce CPU usage from PHP.



Please add this line into your "php.ini" file 

Location :  [opcache]

[opcache]

zend_extension=php_opcache.dll

;Determines if Zend OPCache in enabled

opcache.enable=1

opcache.memory_consumption=128

opcache.interned_strings_buffer=8

opcache.max_accelerated_files=1000

opcache.revalidate_freq=60



Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...