Score:-2

Inline Entity Form: alter autocomplete

nl flag

I need to alter the display of the "Add an existing node" autocomplete response:

  • Add the content type and some more information form the content (location), display the result on two lines.
  • Initiate the autocomplte only if al least 6 characters are entered
  • alter the query sort (ORDER BY changed)

Which is the best way to to this ?

Score:1
de flag

The way I found to deal with this(part in your case) is with a view of type Entity Reference, where the fields you set in the view is what the autocomplete field will display and you can use available filters and sorts. Example:

function MYMODULE_inline_entity_form_reference_form_alter(&$reference_form, FormStateInterface $form_state) {
  $form_id = $form_state->getFormObject()->getFormId();
  if ($form_id === 'node_article_edit_form' || $form_id === 'node_article_form') {
    $reference_form["entity_id"]["#selection_handler"] = 'views';
    $reference_form["entity_id"]["#selection_settings"] = [
      "view" => [
        "view_name" => "my_view_name",
        "display_name" => "my_view_display_name",
      ],
    ];
  }
}

I hope it helps someone else

Score:0
nl flag

Thanks @chalo

this helped to apply my custom autocomplete endpoint (instead of using views) Here my working example based on @chalos answer.

$reference_form['entity_id'] = array_merge(
  $reference_form['entity_id'], [
    '#type' => 'textfield',
    '#autocomplete_route_name' => 'cnfpt_internet.autocomplete',
    '#autocomplete_route_parameters' => [
      'field_name' => 'field_video',
      'target_type' => $reference_form['entity_id']['#target_type'],
      'bundle_name' => 'inline_entity_form',
      'selection_handler' => '',
      'selection_settings_key' => '',
      'entity_id' => 0,
    ],
  ]

My problem was that I didn't use the right hook to intercept the native entity autocomplete and replace it by the custom one.

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.