Score:0

Custom field type properties populated by fetched data

kr flag

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?

in flag
Have you looked at the Feeds module?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.