-2

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 ?

2 Answers 2

1

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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