function ContentEntityBase::getEntityKey

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/ContentEntityBase.php \Drupal\Core\Entity\ContentEntityBase::getEntityKey()
  2. 10 core/lib/Drupal/Core/Entity/ContentEntityBase.php \Drupal\Core\Entity\ContentEntityBase::getEntityKey()
  3. 11.x core/lib/Drupal/Core/Entity/ContentEntityBase.php \Drupal\Core\Entity\ContentEntityBase::getEntityKey()

Gets the value of the given entity key, if defined.

Parameters

string $key: Name of the entity key, for example id, revision or bundle.

Return value

mixed The value of the entity key, NULL if not defined.

7 calls to ContentEntityBase::getEntityKey()
ContentEntityBase::bundle in core/lib/Drupal/Core/Entity/ContentEntityBase.php
Gets the bundle of the entity.
ContentEntityBase::getRevisionId in core/lib/Drupal/Core/Entity/ContentEntityBase.php
ContentEntityBase::id in core/lib/Drupal/Core/Entity/ContentEntityBase.php
Gets the identifier.
ContentEntityBase::label in core/lib/Drupal/Core/Entity/ContentEntityBase.php
Gets the label of the entity.
ContentEntityBase::uuid in core/lib/Drupal/Core/Entity/ContentEntityBase.php
Gets the entity UUID (Universally Unique Identifier).

... See full list

1 method overrides ContentEntityBase::getEntityKey()
EntityTest::getEntityKey in core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php
Gets the value of the given entity key, if defined.

File

core/lib/Drupal/Core/Entity/ContentEntityBase.php, line 1289

Class

ContentEntityBase
Implements Entity Field API specific enhancements to the Entity class.

Namespace

Drupal\Core\Entity

Code

protected function getEntityKey($key) {
    // If the value is known already, return it.
    if (isset($this->entityKeys[$key])) {
        return $this->entityKeys[$key];
    }
    if (isset($this->translatableEntityKeys[$key][$this->activeLangcode])) {
        return $this->translatableEntityKeys[$key][$this->activeLangcode];
    }
    // Otherwise fetch the value by creating a field object.
    $value = NULL;
    if ($this->getEntityType()
        ->hasKey($key)) {
        $field_name = $this->getEntityType()
            ->getKey($key);
        $definition = $this->getFieldDefinition($field_name);
        $property = $definition->getFieldStorageDefinition()
            ->getMainPropertyName();
        $value = $this->get($field_name)->{$property};
        // Put it in the right array, depending on whether it is translatable.
        if ($definition->isTranslatable()) {
            $this->translatableEntityKeys[$key][$this->activeLangcode] = $value;
        }
        else {
            $this->entityKeys[$key] = $value;
        }
    }
    else {
        $this->entityKeys[$key] = $value;
    }
    return $value;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.