3

I am not sure if this is the right way to implement this, so I hope you can help me. I am trying to reference a custom content entity in Drupal 8 with the condition that only the entities created by the current user should show.

Basically CUSTOM ENTITY 1 -> CUSTOM ENTITY 2 (created by the user)

I found a way to do this with views, but I am wondering how to achieve this programmatically.

Please note that I have already managed to get a list of ids and display it as a drop down, but this is not what i want.

I would like to show the reference as autocomplete .

This is what i got so far:

$fields['dishes'] = BaseFieldDefinition::create('entity_reference')
        ->setLabel(t('Dishes'))
        ->setDescription(t('Select the dishes to add  to this menu'))
        ->setSetting('target_type', 'dish')
        ->setSetting('handler', 'default')
        ->setTranslatable(TRUE)
        ->setCardinality(-1)
        ->setDisplayOptions('view', [
            'label' => 'hidden',
            'type' => 'entity_reference_label',
            'weight' => -1,
        ])
        ->setDisplayOptions('form', [
            'type' => 'entity_reference_autocomplete',
            'weight' => -1,
            'settings' => [
                'match_operator' => 'CONTAINS',
                'size' => '60',
                'autocomplete_type' => 'tags',
                'placeholder' => '',
            ],
        ])
        ->setDisplayConfigurable('form', FALSE)
        ->setDisplayConfigurable('view', TRUE);

Is there a way to add conditions to this ? Or customise the query Drupal does to get the referenced entity?

soipo
  • 495
  • 5
  • 18
  • You can create a custom `EntityReferenceSelection` plugin to modify the query used to filter out the entities. You can read [this article](https://fivejars.com/blog/change-entity-autocomplete-selection-rules-drupal-8) to learn on how to do it. – Juanmi Sosso Sep 22 '21 at 15:01

1 Answers1

-1

Try this:

`

$fields['subgroup_id'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Dalinama grupėms'))
  ->setDescription(t('Allocation groups.'))
  ->setSetting('target_type', 'distribution_group')
  ->setSetting('handler', 'views')
  ->setSetting('handler_settings', [
    'view' => [
      'view_name' => 'entity_reference_views_filter',
      'display_name' => 'entity_reference_1',
    ],
  ])
  ->setRequired(TRUE)
  ->setCardinality(1)
  ->setDisplayOptions('view', array(
    'label' => 'above',
    'type' => 'list_default',
    'weight' => -4,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'options_select',
    'settings' => array(
      'match_operator' => 'CONTAINS',
      'size' => 60,
      'placeholder' => '',
    ),
    'weight' => -2,
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

`

init90
  • 11
  • 1
  • Even if your code does work, without explaining what you did and your reasoning behind it it does not constitute as a very solid answer. – Ziyad Edher Jul 15 '17 at 13:13
  • It seems you are using view reference, which is the same way i am doing, I was wondering if there is a way to query and inject into the autocomplete the value of my query in stead of using views – soipo Jul 17 '17 at 15:11
  • This helped me. There is also a similar post https://drupal.stackexchange.com/questions/194804/how-to-create-entity-reference-field-from-custom-entity-to-specific-content-type – Daniel Oct 09 '19 at 11:47