I have a custom field type, org_person, with an autocomplete set up on it to search our endpoint of "people". It saves something like John Smith (12345) to the value property ($properties['value']).
The endpoint looks something like this:
[{id, name, title, ...}, {id, name, title, ...},...]
My goal is to, on entity save (and cron), query my endpoint and save the 30+ properties to the database.
I need to be able to filter and sort on these values in views which is why computing this data in real-time is off the table.
As I understand it, I need to set the class of the property to use for generating the property value. Something like:
$properties['name'] = DataDefinition::create('any')
->setLabel(new TranslatableMarkup('Name'))
->setComputed(TRUE)
->setReadOnly(TRUE)
->setClass('\Drupal\my_module\TypedDataName');
Then in my TypedDataName Class, I get the value like:
public function getValue() {
if ($this->processed !== NULL) {
return $this->processed;
}
$values = $this->getParent()->getValue();
$submitted_value = $values['value'];
$id = EntityAutocomplete::extractEntityIdFromAutocompleteInput($submitted_value);
// Runs fetch here to get 'name' property
$response = $this->_fetchName($id);
$value = $response[0]["name"];
$this->processed = $value;
return $this->processed;
}
This works, although it seems like it's running on display instead of pulling from the database.
Also, if I added another property, like title (or the rest of them), it's going to run this fetch on every property.
How can this be done with a single fetch to populate the 30+ properties on save?