I have a custom entity (Person) with these fields: ID, nom, prenom, telephoneportable, estactive, userID, created, changed. I created it with baseFieldDefinitions.
I want to have a computed field nomprenom = nom .” “.prenom
Here is the corresponding code:
$fields['nomprenom'] = BaseFieldDefinition::create('string') ->setLabel(t('Nom et Prénom')) ->setComputed(TRUE) ->setClass('DrupalassociationNomPrenom');
The code of class ‘NomPrenom’ is here:
/** * @file * Contains DrupalassociationNomPrenom. */ namespace Drupalassociation; use DrupalComponentUtilityString; use DrupalCoreTypedDataDataDefinitionInterface; use DrupalCoreTypedDataTypedDataInterface; use DrupalCoreTypedDataTypedData; /** * A computed property for defining 'Nom Prénom'. */ class NomPrenom extends TypedData { /** * Cached processed value. * * @var string|null */ protected $processed = NULL; /** * Implements DrupalCoreTypedDataTypedDataInterface::getValue(). */ public function getValue($langcode = NULL) { if ($this->processed !== NULL) { return $this->processed; } $this->processed = $this->get('nom')->value ." ". $this->get('prenom')->value; return $this->processed; } /** * Implements DrupalCoreTypedDataTypedDataInterface::setValue(). */ public function setValue($value, $notify = TRUE) { $this->processed = $value; // Notify the parent of any changes. if ($notify && isset($this->parent)) { $this->parent->onChange($this->name); } } }
When I try to add a new Person, I get a PHP Fatal error:
Call to undefined method DrupalassociationNomPrenom::setLangcode()
Why?