I wrote a class constructor whose parameter can be an instance of Drupal\user\Entity\User, an integer, or FALSE.

public function __construct($user = FALSE) {
  if ($user && $user instanceof User) {
    $this->account = $user;

  if ($user && is_int($user)) {
    $this->account = User::load($user);

  if (!$user) {
    $this->account = User::load(Drupal::currentUser()->id());


When I want to use the ID from any kind of entity, the value returned by $account->id() is a string, not an integer.

function custom_node_access(NodeInterface $node, $op, AccountInterface $account) {
  $profile = new IgbUser($account->id());

$account is a class instance of Drupal\Core\Session\AccountInterface. When I call $account->id() or $node->id(), the value returned is a string, not an integer as the documentation of that method reports. This means that is_int($account->id()) returns FALSE.

3 Answers 3


The value is in integer (for most content entity types, that is). The variable type however is a string.

That has a simple technical reason, values loaded from the database are always strings, those put into the node object and never explicitly cast to something else. Doing so would be too expensive for what it brings.


As @4k4 mentioned, it can be int or string. If the value type does matter to your code. Just force convert it to integer.

(int) $account->id()

For User and Node this is defined in the class Drupal\Core\Entity\EntityInterface:

   * Gets the identifier.
   * @return string|int|null
   *   The entity identifier, or NULL if the object does not yet have an
   *   identifier.
  public function id();

And as far as I checked, all subclasses inherit this doc.

The methods to retrieve the id are defined later in the subclasses, so you have to consider both possibilities string and int in your code, which should be no problem.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.