The autocomplete field widgets provided by the Entity reference module have been refactored into a generic 'entity_autocomplete' Form API element which can be used in regular form structures (i.e. not only in entity forms).
Usage examples:
Provide a basic autocomplete element that matches node titles from all bundles:
$form['my_element'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'node',
'#default_value' => $entity, // The #default_value can be either an entity object or an array of entity objects.
);
If we want to restrict the matches to a single or a set of bundles, we can use the 'target_bundles' selection setting:
$form['my_element'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'node',
'#selection_handler' => 'default', // Optional. The default selection handler is pre-populated to 'default'.
'#selection_settings' => array(
'target_bundles' => array('article', 'page'),
),
);
If we want to allow an input of multiple entity labels into the element (commonly known as "tagging" fields), we set the '#tags' property to TRUE (its default value is FALSE):
$form['my_element'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'node',
'#tags' => TRUE,
);
If we want to allow an input of an entity label that does not exist yet but can be created "on the fly" on form submission, the '#autocreate' property can be used:
$form['my_element'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'taxonomy_term',
'#autocreate' => array(
'bundle' => 'tags', // Required. The bundle name for the new entity.
'uid' => <a valid user ID>, // Optional. The user ID for the new entity, if the target entity type implements \Drupal\user\EntityOwnerInterface. Defaults to the current logged-in user.
),
);
Other changes:
- The
entity_reference.autocompleteservice has been moved out of the Entity reference module and renamed toentity.autocomplete_matcher. - The
AutocompleteandAutocomplete (Tags style)field widgets have also been moved outside the Entity reference module. - The
autocomplete_typewidget setting which was previously needed as part of the autocomplete path has been removed.
Comments
Custom block example
Here is an example how to use the entity reference as a field in a custom block.
For particular user as default_value
User entities reference example, filtering by roles
After reading the code a bit, I have a working example that builds a entity reference field for users, filtered by one or more roles:
not working
$roles, $allowed_roles
$roles, $allowed_roles
Peter Lindstrom
LiquidCMS - Content Solution Experts
Using a view to filter results
If looking to use a View to filter results, this works for 8.5.5
Works like a charm
It is hard to find a working example of code using Views' entity reference plugin.
Thank you for this example.
Sorry to reply to an old post
Sorry to reply to an old post, but this was helpful, mostly. One thing I noticed is that when you save an entity using a views selection handler, it doesn't appear to apply after save.
For example if you save an entity, the edit form shows the following format (content title (id)) in the field on edit, instead of what you originally put in (Content Title)
I am using this on a custom field so users type in a persons name based on a view, but when the entity is reloaded/edited, the user (uid) shows up in the box instead of what was originally typed in (User Name (id))
Any way to fix that?
Thank you! This is just what
Thank you! This is just what I was looking for (and surprised not to find in any official documentation).
Restrict nodes based on node's custom field
I have a content type ‘Article’ which has a checkbox field. Right now using the below code I am able to show all the nodes of the ‘article’ type.
How can I restrict to only nodes of articles which have the checkbox checked?
Can Views be referenced using
Can Views be referenced using this form element? I've tried (using
'#target_type' => 'view',) and the filtering/selection works fine but the return value isn't a URL, but a string formatted asentity:node/VIEW_NAMEinstead