Skip to main content
improved formatting
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

Get Current User Drupal 8 How do I get the currently logged-in user?

I have a web site running on drupalDrupal 8 whithwith a login module. I I want a $variable with the uid ofto get the current user ID for the currently logged-in user to later execute a database query later.

So i have write thisThis is the code I am using.

  $userCurrent = \Drupal::currentUser();
  $user = \Drupal\user\Entity\User::load($userCurrent->id());
  $uid= $user->get('uid')->value;
  echo $uid; //return '220'

it work $uid = '220'

$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$uid = $user->get('uid')->value;
echo $uid;

but inIn the file controller of mya custom module, this code workworks only between all using and mythe class controller like this :

<?php
namespace Drupal\CoopMatha_adh_apport\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Node\Entity\Node;
use Drupal\CoopMatha_adh_apport\Repository;
use Drupal\matha_tools\Controller\ListeController;
use Drupal\matha_tools\Controller;


$userCurrent = \Drupal::currentUser();
$user = \Drupal\user\Entity\User::load($userCurrent->id());
$uid= $user->get('uid')->value;
echo $uid;  


class RapportsController extends ControllerBase
{    
      //I want $uid here

        public function filtrer($firstLetter='', $nbr_ligne='') 
        {

          ... some code ...

        }
}
 

My problem is that i want to got the value of $uid (for example '220') ofuses the current user in my controller class for execute my query conditions but if i put just myfollowing code to get the current user in my class or trying to get $uid, the $uid return nothing.

namespace Drupal\CoopMatha_adh_apport\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Node\Entity\Node;
use Drupal\CoopMatha_adh_apport\Repository;
use Drupal\matha_tools\Controller\ListeController;
use Drupal\matha_tools\Controller;

$userCurrent = \Drupal::currentUser();
$user = \Drupal\user\Entity\User::load($userCurrent->id());
$uid= $user->get('uid')->value;
echo $uid;  

class RapportsController extends ControllerBase {    

  // I want the user ID here.
  public function filtrer($firstLetter='', $nbr_ligne='') {
    // …
  }

}

So i want just a solution forHow do I get the $uid of the currentcurrently logged-in user insideID in the default controller class.?

Get Current User Drupal 8

I have a web site on drupal 8 whith a login module. I want a $variable with the uid of the current user for execute a query later.

So i have write this code

  $userCurrent = \Drupal::currentUser();
  $user = \Drupal\user\Entity\User::load($userCurrent->id());
  $uid= $user->get('uid')->value;
  echo $uid; //return '220'

it work $uid = '220'

but in the file controller of my custom module this code work only between all using and my class controller like this :

<?php
namespace Drupal\CoopMatha_adh_apport\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Node\Entity\Node;
use Drupal\CoopMatha_adh_apport\Repository;
use Drupal\matha_tools\Controller\ListeController;
use Drupal\matha_tools\Controller;


$userCurrent = \Drupal::currentUser();
$user = \Drupal\user\Entity\User::load($userCurrent->id());
$uid= $user->get('uid')->value;
echo $uid;  


class RapportsController extends ControllerBase
{    
      //I want $uid here

        public function filtrer($firstLetter='', $nbr_ligne='') 
        {

          ... some code ...

        }
}
 

My problem is that i want to got the value of $uid (for example '220') of the current user in my controller class for execute my query conditions but if i put just my code to get the current user in my class or trying to get $uid, the $uid return nothing.

So i want just a solution for get the $uid of the current user inside the default controller class.

How do I get the currently logged-in user?

I have a site running on Drupal 8 with a login module. I want to get the user ID for the currently logged-in user to later execute a database query.

This is the code I am using.

$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$uid = $user->get('uid')->value;
echo $uid;

In the controller of a custom module, this code works only between all using and the class controller uses the following code.

namespace Drupal\CoopMatha_adh_apport\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Node\Entity\Node;
use Drupal\CoopMatha_adh_apport\Repository;
use Drupal\matha_tools\Controller\ListeController;
use Drupal\matha_tools\Controller;

$userCurrent = \Drupal::currentUser();
$user = \Drupal\user\Entity\User::load($userCurrent->id());
$uid= $user->get('uid')->value;
echo $uid;  

class RapportsController extends ControllerBase {    

  // I want the user ID here.
  public function filtrer($firstLetter='', $nbr_ligne='') {
    // …
  }

}

How do I get the currently logged-in user ID in the controller class?

added 549 characters in body
Source Link
user80337
user80337

I have write this for get the value "code_adherent" wherea web site on drupal 8 whith a login module. I want a $variable with the uid =of the current uid user for execute a query later.

So i have write this code

use Drupal\Core\Controller\ControllerBase;
use Drupal\Node\Entity\Node;
use Drupal\CoopMatha_adh_apport\Repository;
use Drupal\matha_tools\Controller\ListeController;
use Drupal\matha_tools\Controller;

  $userCurrent = \Drupal::currentUser();
  $user = \Drupal\user\Entity\User::load($userCurrent->id());
  $name = $user->getUsername();
  $uid= $user->get('uid')->value;
 
  $queryecho =$uid; db_select('user__field_code_adherent_coop',//return 'ca');'220'

it work $uid = '220'

but in the file controller of my custom module this code work only between all using and my class controller like this :

<?php
namespace Drupal\CoopMatha_adh_apport\Controller;

use $query->fields('ca',Drupal\Core\Controller\ControllerBase;
use ['field_code_adherent_coop_value']);Drupal\Node\Entity\Node;
use Drupal\CoopMatha_adh_apport\Repository;
use $query->condition('ca.entity_id',Drupal\matha_tools\Controller\ListeController;
use $uid,Drupal\matha_tools\Controller;


$userCurrent '='= \Drupal::currentUser();
  $column_name$user = $query\Drupal\user\Entity\User::load($userCurrent->execute>id());
$uid= $user->fetchField>get('uid');->value;
echo $uid; echo $column_name;

it's work but now if i want to get $column_name in my class i got no value

 

class RapportsController extends ControllerBase
{    
   ... Some Code ...
//I want $uid here

   echo $column_name;    public //function Nothingfiltrer($firstLetter='', $nbr_ligne='') 
        {

          ... Somesome Codecode ...

        }
}
 

Any ideaMy problem is that i want to got the value of $uid ?(for example '220') of the current user in my controller class for execute my query conditions but if i put just my code to get the current user in my class or trying to get $uid, the $uid return nothing.

So i want just a solution for get the $uid of the current user inside the default controller class.

I have write this for get the value "code_adherent" where the uid = current uid user

use Drupal\Core\Controller\ControllerBase;
use Drupal\Node\Entity\Node;
use Drupal\CoopMatha_adh_apport\Repository;
use Drupal\matha_tools\Controller\ListeController;
use Drupal\matha_tools\Controller;

  $userCurrent = \Drupal::currentUser();
  $user = \Drupal\user\Entity\User::load($userCurrent->id());
  $name = $user->getUsername();
  $uid= $user->get('uid')->value;
 
  $query = db_select('user__field_code_adherent_coop', 'ca');
  $query->fields('ca', ['field_code_adherent_coop_value']);
  $query->condition('ca.entity_id', $uid, '=');
  $column_name = $query->execute()->fetchField();
  echo $column_name;

it's work but now if i want to get $column_name in my class i got no value

class RapportsController extends ControllerBase
{
   ... Some Code ...
  
   echo $column_name;     // Nothing

   ... Some Code ...

}

Any idea ?

I have a web site on drupal 8 whith a login module. I want a $variable with the uid of the current user for execute a query later.

So i have write this code

  $userCurrent = \Drupal::currentUser();
  $user = \Drupal\user\Entity\User::load($userCurrent->id());
  $uid= $user->get('uid')->value;
  echo $uid; //return '220'

it work $uid = '220'

but in the file controller of my custom module this code work only between all using and my class controller like this :

<?php
namespace Drupal\CoopMatha_adh_apport\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Node\Entity\Node;
use Drupal\CoopMatha_adh_apport\Repository;
use Drupal\matha_tools\Controller\ListeController;
use Drupal\matha_tools\Controller;


$userCurrent = \Drupal::currentUser();
$user = \Drupal\user\Entity\User::load($userCurrent->id());
$uid= $user->get('uid')->value;
echo $uid;  
 

class RapportsController extends ControllerBase
{    
      //I want $uid here

        public function filtrer($firstLetter='', $nbr_ligne='') 
        {

          ... some code ...

        }
}
 

My problem is that i want to got the value of $uid (for example '220') of the current user in my controller class for execute my query conditions but if i put just my code to get the current user in my class or trying to get $uid, the $uid return nothing.

So i want just a solution for get the $uid of the current user inside the default controller class.

deleted 1 character in body
Source Link
user80337
user80337

I have write this for get the value "code_adherent" where the uid = current uid user

use Drupal\Core\Controller\ControllerBase;
use Drupal\Node\Entity\Node;
use Drupal\CoopMatha_adh_apport\Repository;
use Drupal\matha_tools\Controller\ListeController;
use Drupal\matha_tools\Controller;

  $userCurrent = \Drupal::currentUser();
  $user = \Drupal\user\Entity\User::load($userCurrent->id());
  $name = $user->getUsername();
  $uid= $user->get('uid')->value;

  $query = db_select('user__field_code_adherent_coop', 'ca');
  $query->fields('ca', ['field_code_adherent_coop_value']);
  $query->condition('ca.entity_id', '203'$uid, '=');
  $column_name = $query->execute()->fetchField();
  echo $column_name;

it's work but now if i want to get $column_name in my class i got no value

class RapportsController extends ControllerBase
{
   ... Some Code ...
  
   echo $column_name;     // Nothing

   ... Some Code ...

}

Any idea ?

I have write this for get the value "code_adherent" where the uid = current uid user

use Drupal\Core\Controller\ControllerBase;
use Drupal\Node\Entity\Node;
use Drupal\CoopMatha_adh_apport\Repository;
use Drupal\matha_tools\Controller\ListeController;
use Drupal\matha_tools\Controller;

  $userCurrent = \Drupal::currentUser();
  $user = \Drupal\user\Entity\User::load($userCurrent->id());
  $name = $user->getUsername();
  $uid= $user->get('uid')->value;

  $query = db_select('user__field_code_adherent_coop', 'ca');
  $query->fields('ca', ['field_code_adherent_coop_value']);
  $query->condition('ca.entity_id', '203', '=');
  $column_name = $query->execute()->fetchField();
  echo $column_name;

it's work but now if i want to get $column_name in my class i got no value

class RapportsController extends ControllerBase
{
   ... Some Code ...
  
   echo $column_name;     // Nothing

   ... Some Code ...

}

Any idea ?

I have write this for get the value "code_adherent" where the uid = current uid user

use Drupal\Core\Controller\ControllerBase;
use Drupal\Node\Entity\Node;
use Drupal\CoopMatha_adh_apport\Repository;
use Drupal\matha_tools\Controller\ListeController;
use Drupal\matha_tools\Controller;

  $userCurrent = \Drupal::currentUser();
  $user = \Drupal\user\Entity\User::load($userCurrent->id());
  $name = $user->getUsername();
  $uid= $user->get('uid')->value;

  $query = db_select('user__field_code_adherent_coop', 'ca');
  $query->fields('ca', ['field_code_adherent_coop_value']);
  $query->condition('ca.entity_id', $uid, '=');
  $column_name = $query->execute()->fetchField();
  echo $column_name;

it's work but now if i want to get $column_name in my class i got no value

class RapportsController extends ControllerBase
{
   ... Some Code ...
  
   echo $column_name;     // Nothing

   ... Some Code ...

}

Any idea ?

Source Link
user80337
user80337
Loading