public function ContentEntityBase::onChange in Drupal 9
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/Entity/ContentEntityBase.php \Drupal\Core\Entity\ContentEntityBase::onChange()
Reacts to changes to a field.
Note that this is invoked after any changes have been applied.
Parameters
string $field_name: The name of the field which is changed.
Throws
\InvalidArgumentException When trying to assign a value to the language field that matches an existing translation.
\LogicException When trying to change:
- The language of a translation.
- The value of the flag identifying the default translation object.
Overrides FieldableEntityInterface::onChange
File
- core/
lib/ Drupal/ Core/ Entity/ ContentEntityBase.php, line 781
Class
- ContentEntityBase
- Implements Entity Field API specific enhancements to the Entity class.
Namespace
Drupal\Core\EntityCode
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;
}
}