1

I'm on Drupal 8 and I created a custom entity custom_entity_one following the Drupal 8 examples, the content_entity_example module. Now, in another custom entity, custom_entity_two I'm trying to create a BaseFieldDefinition like so:

$fields['cid'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('custom_entity_one ID'))
  ->setSettings(array(
    'target_type' => 'custom_entity_one',
    'multiple' => TRUE,
    'handler' => 'default'
  ))
  ->setDisplayOptions('view', array(
    'label' => 'above',
    'type' => 'id',
    'weight' => -1,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'entity_reference_autocomplete',
    'settings' => array(
      'match_operator' => 'CONTAINS',
      'size' => 60,
      'placeholder' => '',
    ),
    'weight' => -1,
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

In the add form of the custom_entity_two, the reference field appears, however, it doesn't find any value, even though I have multiple entities of type custom_entity_one.

I thought that it will look for the label propriety of the entity, but it doesn't seem so. What I am missing ? Should I create a custom handler ? I'm not very sure how.

2

Yes, I was right, right after I posted the question I checked if I'm missing the entity_key label, and I was. I added it in the entity annotation definition, like so:

entity_keys = {
  "id" = "id",
  "uuid" = "uuid",
  "label" = "title",
},
  • Yeah, see also drupal.stackexchange.com/questions/210610/…, pretty much the same question. – Berdir Aug 16 '16 at 19:49
  • @Berdir, yes, sorry for that, I couldn't find it on my first search, this question then may be deleted if it's not contributing to the community. – Canutza Aug 17 '16 at 6:07
  • 1
    No worries. I just added that as a reference, has a bit more infos. drupal answers/stack exchange has no rule against asking duplicate questions, although they are sometimes closed as duplicate (but never deleted). having the same question asked in slightly different ways might allow more users to find it. – Berdir Aug 17 '16 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.