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('\Drupal\association\NomPrenom');
The code of class 'NomPrenom' is here:
/**
* @file
* Contains \Drupal\association\NomPrenom.
*/
namespace Drupal\association;
use Drupal\Component\Utility\String;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\Core\TypedData\TypedData;
/**
* A computed property for defining 'Nom Prénom'.
*/
class NomPrenom extends TypedData {
/**
* Cached processed value.
*
* @var string|null
*/
protected $processed = NULL;
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::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 \Drupal\Core\TypedData\TypedDataInterface::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 Drupal\association\NomPrenom::setLangcode()
Why?