Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

User

By editor , 23 May, 2017

Redirect a user after login. Redirection on Drupal 8.

Tags

  • Drupal 8
  • Code
  • Module
  • User

To make a redirection after user login, we can use the hook hook_user_login().
Note : You cannot use $form_state->setRedirectUrl() directly in the form alter, since it will be overwritten by UserForm::submitForm().

By editor , 16 December, 2015

Event on Entity / Node / User ... delete

Tags

  • Drupal 8
  • Code
  • Node
  • User

For this, You must use hook_entity_delete.

Example :
function mymodule_entity_delete(\Drupal\Core\Entity\EntityInterface $entity) {
  $entity_type = $entity->getEntityTypeId();
  $entity_id = $entity->id();

By editor , 9 December, 2015

Login programmatically as a user

Tags

  • Drupal 8
  • Code
  • User

Example:
$user = User::load($uid);
user_login_finalize($user);

By editor , 9 December, 2015

Get all user's roles

Tags

  • Drupal 8
  • User
  • Code

Get All roles:
    $roles = \Drupal\user\Entity\Role::loadMultiple();

By editor , 2 December, 2015

Get the current user in drupal 8

Tags

  • Drupal 8
  • User
  • Code

Example:

    //use Drupal\user\Entity\User;
    $userCurrent = \Drupal::currentUser();
    $user = \Drupal\user\Entity\User::load($userCurrent->id());
    $name = $user->getUsername();

By editor , 28 November, 2015

Get the user IP address : REMOTE_ADDR

Tags

  • Drupal 8
  • Code
  • User

Drupal do not like using the $_SERVER['REMOTE_ADDR']; to get the remote ip (the user ip address).

In Drupal 7, the function ip_address() exist but not in D8.
$ip = ip_address();

By editor , 18 November, 2015

Save user / temporary data on $_SESSION

Tags

  • Drupal 8
  • Code
  • User

Store data on session.

Example:

$session = new \Symfony\Component\HttpFoundation\Session\Session();
$session->set('var_name', $session->get('var_name',0) + 1);
drupal_set_message($session->get('var_name'));

 

By editor , 14 November, 2015

Create a user account programmatically in Drupal 8

Tags

  • Drupal 8
  • Code
  • User

    Here an example for create a user account programmatically in Drupal 8

 

    $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
    $user = \Drupal\user\Entity\User::create();

By editor , 6 November, 2015

How to get the current language in Drupal 8

Tags

  • Drupal 8
  • Code
  • Translation
  • User

Drupal 8

//To get the lanuage code:
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();

//To get the language name:
$language =  \Drupal::languageManager()->getCurrentLanguage()->getName();

User

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer