Problem/Motivation
Currently there is no easy way to use Tagify as autocomplete inside a custom form for a entity_reference form-element.
Here's an example of how I use the existing entity_reference type:
$form['user_reference'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'user',
'#title' => t('User'),
'#description' => t('Enter a user name or email address.'),
'#default_value' => $current_user, // optional, a user object for pre-populating the field
];
It would be great to use tagify for this problem scenario, which is currently not possible, as the Tagify settings are stored inside a key/value store and retrieved in the autocomplete-handler, which is hard to mimic w/o a fully fledged field-widget.
Slightly related to #3352158
Comments
Comment #2
stmh commentedhttps://www.drupal.org/project/tagify/issues/3411891#
Comment #3
gxleano commentedComment #4
gxleano commentedComment #5
gxleano commentedComment #7
gxleano commentedThank you, Stephan, for bringing up this topic.
I've refactored the Tagify element in order to facilitate the custom integration.
You could try to check if everything works fine for you with something like:
Or if you need to use attributes or so, follow the next example, please:
I will document this part, when the issue will be reviewed.
Comment #8
gxleano commentedComment #9
gxleano commentedDemo from #7 form example.
Comment #10
gxleano commentedIf you also want to have current user as default value, you'd need to pass an array of entities to '#default_value', in this case with current:
Comment #11
drebroff commentedI have an issue:
Code:
I'm not quite sure what '#identifier' is
version: '1.2.15+3-dev'
php: 8.1.27
drupal: 10.2.2
Errors:
and
Warning: Undefined array key "#placeholder" in Drupal\tagify\Element\EntityAutocompleteTagify::processEntityAutocompleteTagify() (line 152 of ...\modules\contrib\tagify\src\Element\EntityAutocompleteTagify.php)Comment #12
gxleano commentedHi @drebroff,
It's quite tricky to get this working in the current version of the module, for that reason Stephan created this "feature request" issue. Perhaps you could try forking this issue, where we are facilitating the creation of form elements with Tagify as a new feature.
Once the feature created in this issue is released, the creation of Tagify form elements will be much easier, as described in the comments.
Comment #13
gxleano commentedSo, your example code should works as expected with this new feature.
BTW: The
#identifierattribute is used to differentiate between multiple Tagify elements in a form and it will be optional.Comment #16
gxleano commentedIt will be included on release
1.2.18Comment #17
gxleano commented