You are here

public function ContentEntityBase::__construct in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/ContentEntityBase.php \Drupal\Core\Entity\ContentEntityBase::__construct()

Constructs an Entity object.

Parameters

array $values: An array of values to set, keyed by property name. If the entity type has bundles, the bundle key has to be specified.

string $entity_type: The type of the entity to create.

Overrides Entity::__construct

File

core/lib/Drupal/Core/Entity/ContentEntityBase.php, line 171
Contains \Drupal\Core\Entity\ContentEntityBase.

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 = array()) {
  $this->entityTypeId = $entity_type;
  $this->entityKeys['bundle'] = $bundle ? $bundle : $this->entityTypeId;
  $this->langcodeKey = $this
    ->getEntityType()
    ->getKey('langcode');
  $this->defaultLangcodeKey = $this
    ->getEntityType()
    ->getKey('default_langcode');
  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.
  $data = array(
    'status' => static::TRANSLATION_EXISTING,
  );
  $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;
      }
    }
  }
}