function ContentEntityBase::onChange

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

File

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

Class

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

Namespace

Drupal\Core\Entity

Code

public function onChange($name) {
    // Check if the changed name is the value of any entity keys and if any of
    // those values are currently cached, if so, reset it. Exclude the bundle
    // from that check, as it ready only and must not change, unsetting it could
    // lead to recursions.
    foreach (array_keys($this->getEntityType()
        ->getKeys(), $name, TRUE) as $key) {
        if ($key != 'bundle') {
            if (isset($this->entityKeys[$key])) {
                unset($this->entityKeys[$key]);
            }
            elseif (isset($this->translatableEntityKeys[$key][$this->activeLangcode])) {
                unset($this->translatableEntityKeys[$key][$this->activeLangcode]);
            }
            // If the revision identifier field is being populated with the original
            // value, we need to make sure the "new revision" flag is reset
            // accordingly.
            if ($key === 'revision' && $this->getRevisionId() == $this->getLoadedRevisionId() && !$this->isNew()) {
                $this->newRevision = FALSE;
            }
        }
    }
    switch ($name) {
        case $this->langcodeKey:
            if ($this->isDefaultTranslation()) {
                // Update the default internal language cache.
                $this->setDefaultLangcode();
                if (isset($this->translations[$this->defaultLangcode])) {
                    $message = new FormattableMarkup('A translation already exists for the specified language (@langcode).', [
                        '@langcode' => $this->defaultLangcode,
                    ]);
                    throw new \InvalidArgumentException($message);
                }
                $this->updateFieldLangcodes($this->defaultLangcode);
            }
            else {
                // @todo Allow the translation language to be changed. See
                //   https://www.drupal.org/node/2443989.
                $items = $this->get($this->langcodeKey);
                if ($items->value != $this->activeLangcode) {
                    $items->setValue($this->activeLangcode, FALSE);
                    $message = new FormattableMarkup('The translation language cannot be changed (@langcode).', [
                        '@langcode' => $this->activeLangcode,
                    ]);
                    throw new \LogicException($message);
                }
            }
            break;
        case $this->defaultLangcodeKey:
            // @todo Use a standard method to make the default_langcode field
            //   read-only. See https://www.drupal.org/node/2443991.
            if (isset($this->values[$this->defaultLangcodeKey]) && $this->get($this->defaultLangcodeKey)->value != $this->isDefaultTranslation()) {
                $this->get($this->defaultLangcodeKey)
                    ->setValue($this->isDefaultTranslation(), FALSE);
                $message = new FormattableMarkup('The default translation flag cannot be changed (@langcode).', [
                    '@langcode' => $this->activeLangcode,
                ]);
                throw new \LogicException($message);
            }
            break;
        case $this->revisionTranslationAffectedKey:
            // If the revision translation affected flag is being set then enforce
            // its value.
            $this->setRevisionTranslationAffectedEnforced(TRUE);
            break;
    }
}

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