function ContentEntityBase::__set

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

Implements the magic method for setting object properties.

Uses default language always.

File

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

Class

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

Namespace

Drupal\Core\Entity

Code

public function __set($name, $value) {
    // Inline getFieldDefinition() to speed things up.
    if (!isset($this->fieldDefinitions)) {
        $this->getFieldDefinitions();
    }
    // Handle Field API fields.
    if (isset($this->fieldDefinitions[$name])) {
        // Support setting values via property objects.
        if ($value instanceof TypedDataInterface) {
            $value = $value->getValue();
        }
        // If a FieldItemList object already exists, set its value.
        if (isset($this->fields[$name][$this->activeLangcode])) {
            $this->fields[$name][$this->activeLangcode]
                ->setValue($value);
        }
        else {
            $this->getTranslatedField($name, $this->activeLangcode)
                ->setValue($value);
        }
    }
    elseif ($name == 'translations') {
        $this->translations = $value;
    }
    else {
        $this->values[$name] = $value;
    }
}

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