Created
August 8, 2019 09:08
-
-
Save henideepak/4a1462a963eb0c0df239708fd3dcfc9c to your computer and use it in GitHub Desktop.
Drupal 8 user insert give default role
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Impliment user insert give default role. | |
*/ | |
function hook_user_insert(Drupal\Core\Entity\EntityInterface $entity) { | |
$entity->addRole('role_name'); | |
$entity->save(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Impliment to give customer default role | |
*/ | |
function hook_user_insert(Drupal\Core\Entity\EntityInterface $entity) { | |
$connection = \Drupal::database(); | |
$query = $connection->select('users_field_data', 'u'); | |
$query->condition('u.uid', 0, '<>'); | |
$query->fields('u', ['uid']); | |
$query->join('user__roles', 'r', 'u.uid = r.entity_id'); | |
$query->condition('u.status', 1); | |
$query->condition('r.roles_target_id', 'consultant'); | |
$result = $query->execute()->fetchAll(); | |
$query1 = $connection->select('user__field_consultant', 'c'); | |
$query1->fields('c', ['field_consultant_target_id']); | |
$query1->range(0,1); | |
$query1->orderBy('c.entity_id', 'DESC'); | |
$result1 = $query1->execute()->fetchAll(); | |
$second_last = 0; | |
if (isset($result1) && isset($result1[0]) && isset($result1[0]->field_consultant_target_id) && ($result1[0]->field_consultant_target_id > 0)){ | |
$second_last = $result1[0]->field_consultant_target_id; | |
} | |
$user_list = []; | |
$count = 0; | |
$last = 0; | |
foreach ($result as $key => $consultant_user) { | |
$user_list[$consultant_user->uid] = $consultant_user->uid; | |
if($count == 0){ | |
$last = $consultant_user->uid; | |
$count = 1; | |
} | |
if($second_last == $consultant_user->uid){ | |
$count = 0; | |
} | |
} | |
$entity->addRole('customer'); | |
$entity->field_consultant[] = ['target_id' => $last]; | |
$entity->save(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment