function ContentEntityBase::__construct

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

Overrides EntityBase::__construct

File

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

Class

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

Namespace

Drupal\Core\Entity

Code

public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = []) {
    $this->entityTypeId = $entity_type;
    $this->entityKeys['bundle'] = $bundle ? $bundle : $this->entityTypeId;
    $this->langcodeKey = $this->getEntityType()
        ->getKey('langcode');
    $this->defaultLangcodeKey = $this->getEntityType()
        ->getKey('default_langcode');
    $this->revisionTranslationAffectedKey = $this->getEntityType()
        ->getKey('revision_translation_affected');
    foreach ($values as $key => $value) {
        // If the key matches an existing property set the value to the property
        // to set properties like isDefaultRevision.
        // @todo: Should this be converted somehow?
        if (property_exists($this, $key) && isset($value[LanguageInterface::LANGCODE_DEFAULT])) {
            $this->{$key} = $value[LanguageInterface::LANGCODE_DEFAULT];
        }
    }
    $this->values = $values;
    foreach ($this->getEntityType()
        ->getKeys() as $key => $field_name) {
        if (isset($this->values[$field_name])) {
            if (is_array($this->values[$field_name])) {
                // We store untranslatable fields into an entity key without using a
                // langcode key.
                if (!$this->getFieldDefinition($field_name)
                    ->isTranslatable()) {
                    if (isset($this->values[$field_name][LanguageInterface::LANGCODE_DEFAULT])) {
                        if (is_array($this->values[$field_name][LanguageInterface::LANGCODE_DEFAULT])) {
                            if (isset($this->values[$field_name][LanguageInterface::LANGCODE_DEFAULT][0]['value'])) {
                                $this->entityKeys[$key] = $this->values[$field_name][LanguageInterface::LANGCODE_DEFAULT][0]['value'];
                            }
                        }
                        else {
                            $this->entityKeys[$key] = $this->values[$field_name][LanguageInterface::LANGCODE_DEFAULT];
                        }
                    }
                }
                else {
                    // We save translatable fields such as the publishing status of a node
                    // into an entity key array keyed by langcode as a performance
                    // optimization, so we don't have to go through TypedData when we
                    // need these values.
                    foreach ($this->values[$field_name] as $langcode => $field_value) {
                        if (is_array($this->values[$field_name][$langcode])) {
                            if (isset($this->values[$field_name][$langcode][0]['value'])) {
                                $this->translatableEntityKeys[$key][$langcode] = $this->values[$field_name][$langcode][0]['value'];
                            }
                        }
                        else {
                            $this->translatableEntityKeys[$key][$langcode] = $this->values[$field_name][$langcode];
                        }
                    }
                }
            }
        }
    }
    // Initialize translations. Ensure we have at least an entry for the default
    // language.
    // We determine if the entity is new by checking in the entity values for
    // the presence of the id entity key, as the usage of ::isNew() is not
    // possible in the constructor.
    $data = isset($values[$this->getEntityType()
        ->getKey('id')]) ? [
        'status' => static::TRANSLATION_EXISTING,
    ] : [
        'status' => static::TRANSLATION_CREATED,
    ];
    $this->translations[LanguageInterface::LANGCODE_DEFAULT] = $data;
    $this->setDefaultLangcode();
    if ($translations) {
        foreach ($translations as $langcode) {
            if ($langcode != $this->defaultLangcode && $langcode != LanguageInterface::LANGCODE_DEFAULT) {
                $this->translations[$langcode] = $data;
            }
        }
    }
    if ($this->getEntityType()
        ->isRevisionable()) {
        // Store the loaded revision ID the entity has been loaded with to
        // keep it safe from changes.
        $this->updateLoadedRevisionId();
    }
}

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