User http://lobsterr.me/ en How to check if the current user has a role in Drupal? http://lobsterr.me/post/how-check-if-current-user-has-role-drupal <span class="field field--name-title field--type-string field--label-hidden">How to check if the current user has a role in Drupal?</span> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/user/1" typeof="schema:Person" property="schema:name" datatype="">LOBsTerr</span></span> <span class="field field--name-created field--type-created field--label-hidden">03/10/2021</span> <div class="field field--name-f-content field--type-entity-reference-revisions field--label-hidden field__items"> <div class="field__item"> <div class="paragraph paragraph--type--l-text paragraph--view-mode--default"> <div class="clearfix text-formatted field field--name-f-text field--type-text-long field--label-hidden field__item"><pre> <code class="language-php">\Drupal::currentUser()-&gt;hasRole('administrator')</code></pre> <p> </p> </div> </div> </div> </div> <div class="field field--name-f-tags field--type-entity-reference field--label-hidden field__items"> <div class="field__item btn btn-secondary"><a href="/tags/drupal-9" hreflang="en">Drupal 9</a></div> <div class="field__item btn btn-secondary"><a href="/tags/user" hreflang="en">User</a></div> </div> <div class="field field--name-f-related-items field--type-entity-reference field--label-above list-group list-group-flush"> <div class="field__label">Related items</div> <ul class="field field--name-f-related-items field--type-entity-reference field--label-above list-group list-group-flush field__items"> <li class="field__item list-group-item"><a href="/post/how-check-if-current-user-has-permission-drupal" hreflang="en">How to check if the current user has a permission in Drupal</a></li> </ul> </div> <section class="field field--name-f-comments field--type-comment field--label-above comment-wrapper"> </section> Wed, 10 Mar 2021 09:33:46 +0000 LOBsTerr 43 at http://lobsterr.me How to check if the current user has a permission in Drupal http://lobsterr.me/post/how-check-if-current-user-has-permission-drupal <span class="field field--name-title field--type-string field--label-hidden">How to check if the current user has a permission in Drupal</span> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/user/1" typeof="schema:Person" property="schema:name" datatype="">LOBsTerr</span></span> <span class="field field--name-created field--type-created field--label-hidden">03/10/2021</span> <div class="field field--name-f-content field--type-entity-reference-revisions field--label-hidden field__items"> <div class="field__item"> <div class="paragraph paragraph--type--l-text paragraph--view-mode--default"> <div class="clearfix text-formatted field field--name-f-text field--type-text-long field--label-hidden field__item"><pre> <code class="language-php">\Drupal::currentUser()-&gt;hasPermission('permission name');</code></pre> <p> </p> </div> </div> </div> </div> <div class="field field--name-f-tags field--type-entity-reference field--label-hidden field__items"> <div class="field__item btn btn-secondary"><a href="/tags/drupal-9" hreflang="en">Drupal 9</a></div> <div class="field__item btn btn-secondary"><a href="/tags/user" hreflang="en">User</a></div> </div> <div class="field field--name-f-related-items field--type-entity-reference field--label-above list-group list-group-flush"> <div class="field__label">Related items</div> <ul class="field field--name-f-related-items field--type-entity-reference field--label-above list-group list-group-flush field__items"> <li class="field__item list-group-item"><a href="/post/how-check-if-current-user-has-role-drupal" hreflang="en">How to check if the current user has a role in Drupal?</a></li> </ul> </div> <section class="field field--name-f-comments field--type-comment field--label-above comment-wrapper"> </section> Wed, 10 Mar 2021 09:28:39 +0000 LOBsTerr 42 at http://lobsterr.me How to detect the very first login of the user in Drupal 9? http://lobsterr.me/post/how-detect-very-first-login-user-drupal-9 <span class="field field--name-title field--type-string field--label-hidden">How to detect the very first login of the user in Drupal 9?</span> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/user/1" typeof="schema:Person" property="schema:name" datatype="">LOBsTerr</span></span> <span class="field field--name-created field--type-created field--label-hidden">01/29/2021</span> <div class="field field--name-f-content field--type-entity-reference-revisions field--label-hidden field__items"> <div class="field__item"> <div class="paragraph paragraph--type--l-text paragraph--view-mode--default"> <div class="clearfix text-formatted field field--name-f-text field--type-text-long field--label-hidden field__item"><p>For this we need implement user_login hook and then we check the last access field.</p> <pre> <code class="language-php">use Drupal\user\Entity\User; /** * Implements hook_user_login(). */ function mymodule_user_login(User $user) { if (empty($user-&gt;getLastAccessedTime())) { // Do something here. } }</code></pre> <p> </p> </div> </div> </div> </div> <div class="field field--name-f-tags field--type-entity-reference field--label-hidden field__items"> <div class="field__item btn btn-secondary"><a href="/tags/drupal-9" hreflang="en">Drupal 9</a></div> <div class="field__item btn btn-secondary"><a href="/tags/user" hreflang="en">User</a></div> </div> <div class="field field--name-f-related-items field--type-entity-reference field--label-above list-group list-group-flush"> <div class="field__label">Related items</div> <ul class="field field--name-f-related-items field--type-entity-reference field--label-above list-group list-group-flush field__items"> <li class="field__item list-group-item"><a href="/post/how-redirect-user-after-login-drupal-9" hreflang="en">How to redirect a user after login in Drupal 9?</a></li> </ul> </div> <section class="field field--name-f-comments field--type-comment field--label-above comment-wrapper"> </section> Fri, 29 Jan 2021 15:51:21 +0000 LOBsTerr 33 at http://lobsterr.me How to redirect a user after login in Drupal 9? http://lobsterr.me/post/how-redirect-user-after-login-drupal-9 <span class="field field--name-title field--type-string field--label-hidden">How to redirect a user after login in Drupal 9?</span> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/user/1" typeof="schema:Person" property="schema:name" datatype="">LOBsTerr</span></span> <span class="field field--name-created field--type-created field--label-hidden">01/29/2021</span> <div class="field field--name-f-content field--type-entity-reference-revisions field--label-hidden field__items"> <div class="field__item"> <div class="paragraph paragraph--type--l-text paragraph--view-mode--default"> <div class="clearfix text-formatted field field--name-f-text field--type-text-long field--label-hidden field__item"><p>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 ?</p> <p>We need to implement user_login hook</p> <pre> <code class="language-php">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' =&gt; $user-&gt;id(), ]); $redirect = new RedirectResponse($url-&gt;toString()); $redirect-&gt;send(); }</code></pre> <p><br />  </p> </div> </div> </div> </div> <div class="field field--name-f-tags field--type-entity-reference field--label-hidden field__items"> <div class="field__item btn btn-secondary"><a href="/tags/drupal-9" hreflang="en">Drupal 9</a></div> <div class="field__item btn btn-secondary"><a href="/tags/user" hreflang="en">User</a></div> </div> <div class="field field--name-f-related-items field--type-entity-reference field--label-above list-group list-group-flush"> <div class="field__label">Related items</div> <ul class="field field--name-f-related-items field--type-entity-reference field--label-above list-group list-group-flush field__items"> <li class="field__item list-group-item"><a href="/post/how-detect-very-first-login-user-drupal-9" hreflang="en">How to detect the very first login of the user in Drupal 9?</a></li> </ul> </div> <section class="field field--name-f-comments field--type-comment field--label-above comment-wrapper"> </section> Fri, 29 Jan 2021 15:47:25 +0000 LOBsTerr 32 at http://lobsterr.me How to add / remove user role programmatically in Drupal 8 ? http://lobsterr.me/post/how-add-remove-user-role-programmatically-drupal-8 <span class="field field--name-title field--type-string field--label-hidden">How to add / remove user role programmatically in Drupal 8 ?</span> <div class="field field--name-f-intro field--type-string-long field--label-hidden field__item"><br /> <br /> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/user/1" typeof="schema:Person" property="schema:name" datatype="">LOBsTerr</span></span> <span class="field field--name-created field--type-created field--label-hidden">11/07/2019</span> <div class="field field--name-f-content field--type-entity-reference-revisions field--label-hidden field__items"> <div class="field__item"> <div class="paragraph paragraph--type--l-text paragraph--view-mode--default"> <div class="clearfix text-formatted field field--name-f-text field--type-text-long field--label-hidden field__item"><p>User access system based on roles and in some cases we want to assign role based on specific business requirements. </p> <p>For this we need to load a user first:</p> <pre> <code class="language-php">&lt;?php use Drupal\user\Entity\User; // pass the correct user id here. User:load(4); </code></pre> <p>or we can load current user</p> <pre> <code class="language-php">&lt;?php use Drupal\user\Entity\User; User::load(\Drupal::currentUser()-&gt;id());</code></pre> <p>Then we add a user role </p> <pre> <code class="language-php">// pass machine name of the user. $user-&gt;addRole('administrator'); $user-&gt;save();</code></pre> <p>To remove the role we can use this code</p> <pre> <code class="language-php">// pass machine name of the user. $user-&gt;removeRole('administrator'); $user-&gt;save();</code></pre> </div> </div> </div> </div> <div class="field field--name-f-tags field--type-entity-reference field--label-hidden field__items"> <div class="field__item btn btn-secondary"><a href="/tags/drupal-9" hreflang="en">Drupal 9</a></div> <div class="field__item btn btn-secondary"><a href="/tags/user" hreflang="en">User</a></div> <div class="field__item btn btn-secondary"><a href="/tags/role" hreflang="en">Role</a></div> </div> <section class="field field--name-f-comments field--type-comment field--label-above comment-wrapper"> </section> Thu, 07 Nov 2019 09:56:51 +0000 LOBsTerr 17 at http://lobsterr.me