Score:0

Custom widget with entity reference and another field - not saving

lr flag

I'm trying to create a custom field for my entity (entity A). It will hold custom entity reference, let's say entity B, with another field associated (integer).

I want to create associations entity A -> [ (entityB, 10), (entityB, 5), ...].

I followed this thread. I copied the code, just changed target type for entity B and string to integer.

The field is rendered fine - entity B with autocomplete, search works as expected. An integer field, works as expected.

But the entities are not saved, nor do I see any table in database where they could be saved. Also the isEmpty() function always returns true, like nothing is associated to this field. This is field definition:

    $fields['entityb'] = BaseFieldDefinition::create('enetity_b_type_id')
  ->setLabel(t('Entity b'))
  ->setDescription(t('My field'))
  ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
  ->setDisplayOptions('form', [
      'weight' => 8,
      'type' => 'entity_reference_autocomplete'
  ])
  ->setDisplayOptions('view', [
      'type' => 'string',
      'weight' => 8,
  ])
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

Do I need to define the storage in some yml file? I cannot find any recipe for Drupal for achieving this. Is there any other approach than custom widget?

drupaltest avatar
lr flag
I have found the table. It is created just as defined in schema - bundle, deleted, entity_id, ..., target_id, attribute. But nothing is saved and even when manually filled, not displayed in the entity A.
I sit in a Tesla and translated this thread with Ai:

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.