Do you want to know how to Drupal?

Let's Drupal

How to check if the current page using admin theme in Drupal?

To check this we need to use router.admin_context service

if (\Drupal::service('router.admin_context')->isAdminRoute()) {
   // Do something here.
}

We also can check a specific route

$route_provider = \Drupal::service('router.route_provider');
$route = $route_provider->getRouteByName('user.page');
if (\Drupal::service('router.admin_context')->isAdminRoute($route)) {
   // Do something here.
}

 


How to load a configuration file from module in Drupal 9?

In some cases we want to load a configuration file from optional / install folders. For example to compare it with active config and apply new changes.
To load a configuration from install folder user the next code snippet.

use Drupal\Core\Config\ExtensionInstallStorage;

$field_storage = 'field.storage.group_content.grequest_status';
$install_storage = new ExtensionInstallStorage(
      \Drupal::service('config.storage'),
      InstallStorage::CONFIG_INSTALL_DIRECTORY,
      InstallStorage::DEFAULT_COLLECTION,
      TRUE,
      NULL
);

From optional folder

use Drupal\Core\Config\ExtensionInstallStorage;

$name = 'views.view.group_pending_members';

$optional_storage = new ExtensionInstallStorage(
      \Drupal::service('config.storage'),
      InstallStorage::CONFIG_OPTIONAL_DIRECTORY,
      InstallStorage::DEFAULT_COLLECTION,
      TRUE,
      NULL
);

and schema configuration

use Drupal\Core\Config\ExtensionInstallStorage;

$name = 'views.view.group_pending_members';

$optional_storage = new ExtensionInstallStorage(
      \Drupal::service('config.storage'),
      InstallStorage::CONFIG_SCHEMA_DIRECTORY,
      InstallStorage::DEFAULT_COLLECTION,
      TRUE,
      NULL
);

 


How to detect the very first login of the user in Drupal 9?

For this we need implement user_login hook and then we check the last access field.

use Drupal\user\Entity\User;

/**
 * Implements hook_user_login().
 */
function mymodule_user_login(User $user) {
  if (empty($user->getLastAccessedTime())) {
    // Do something here.
  }
}

 


How to redirect a user after login in Drupal 9?

Let's imagine we want to send a user to custom route after login in our example to user edit form. How do we do it ?

We need to implement user_login hook

use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\user\Entity\User;

/**
 * Implements hook_user_login().
 */
function mymodule_user_login(User $user) {
    $url = Url::fromRoute('entity.user.edit_form', [
       'user' => $user->id(),
    ]);
    $redirect = new RedirectResponse($url->toString());
    $redirect->send();
}


 


How to show a message in Drupal 9?

Sometimes we need to inform a user about a result of some operations. For example we have submitted the form and we want to show a success message or an error

We need to user service "messenger" service, which will store the message in the session and fetch during the next request.

Get instance of messenger service

# 1. Standard way to get messenger service, in case you want to pass it using dependency injection.
$messenger = \Drupal::service("messenger");

# 2. Fast way to use helper function.

$messenger = \Drupal::messenger();

Also in the form object or controller we don't have include it using dependency injection. We can us directly 

$messenger = $this->messenger();

Methods

1. Add message. Allows to send a message of any type, the second parameter accepts the type

$meesenger->addMessage(t('Your message.'), MessengerInterface::TYPE_WARNING);

But in most of the cases it is better to use directly specific method

 

2. Add status

$messenger->addStatus(t('Your message'));

 

3. Add warning

$messenger->addWarning(t('Your message'));

 

4. Add error

$messenger->addError(t('Your message'));