0

When I load an entity, its protected 'values' property contains all of its field values.

But when I create a new entity with MyEntityClass::create($array_of_values), its 'values' property is not set until I have saved it.

Is there a way to get that set, for the purposes of debug output?

1 Answer 1

1

The code comments in ContentEntityBase say that $values always hold the original, unchanged values of the entity:

/**
 * The plain data values of the contained fields.
 *
 * This always holds the original, unchanged values of the entity. The values
 * are keyed by language code, whereas LanguageInterface::LANGCODE_DEFAULT
 * is used for values in default language.
 *
 * @todo: Add methods for getting original fields and for determining
 * changes.
 * @todo: Provide a better way for defining default values.
 *
 * @var array
 */
protected $values = [];

Quick glance through the code suggests that the public method updateOriginalValues() would do the trick:

/**
 * Updates the original values with the interim changes.
 */
public function updateOriginalValues() {
  if (!$this->fields) {
    return;
  }
  foreach ($this->getFieldDefinitions() as $name => $definition) {
    if (!$definition->isComputed() && !empty($this->fields[$name])) {
      foreach ($this->fields[$name] as $langcode => $item) {
        $item->filterEmptyItems();
        $this->values[$name][$langcode] = $item->getValue();
      }
    }
  }
}
1
  • Calling $entity->updateOriginalValues() works perfectly! Thanks!
    – joachim
    Sep 14, 2017 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.