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

CommentFileSizeAuthor
#9 tagify-custom-form.gif206.23 KBgxleano

Issue fork tagify-3411891

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

stmh created an issue. See original summary.

gxleano’s picture

Issue summary: View changes
gxleano’s picture

Issue summary: View changes
gxleano’s picture

Assigned: Unassigned » gxleano

gxleano’s picture

Thank 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:

$form['user_reference'] = [
  '#type' => 'entity_autocomplete_tagify',
  '#target_type' => 'user',
  '#title' => $this->t('Users'),
  '#description' => $this->t('Enter a user name.'),
];

Or if you need to use attributes or so, follow the next example, please:

$form['my_element'] = [
  '#type' => 'entity_autocomplete_tagify',
  '#target_type' => 'node',
  '#tags' => TRUE,
  '#default_value' => $array_of_entities,
  '#selection_handler' => 'default',
  '#selection_settings' => [
    'target_bundles' => ['article', 'page'],
    'info_label' => '[node:type]',
   ],
  '#autocreate' => [
    'bundle' => 'article',
    'uid' => <a valid user ID>,
   ],
  '#max_items' => 10,
  '#suggestions_dropdown' => 1,
  '#match_operator' => 'CONTAINS',
  '#show_entity_id' => 0,
  '#identifier' => 'field_name',
];

I will document this part, when the issue will be reviewed.

gxleano’s picture

Assigned: gxleano » Unassigned
Status: Active » Needs review
gxleano’s picture

StatusFileSize
new206.23 KB

Demo from #7 form example.

Tagify custom form element

gxleano’s picture

If 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:

$current_user = \Drupal::currentUser();
$uid = $current_user->id();
$current_user_entity = \Drupal::entityTypeManager()->getStorage('user')->load($uid);
      
$form['user_reference'] = [
  '#type' => 'entity_autocomplete_tagify',
  '#target_type' => 'user',
  '#title' => $this->t('Users'),
  '#description' => $this->t('Enter a user name.'),
  '#default_value' => [$current_user_entity],
];
drebroff’s picture

I have an issue:
Code:

          $form['vocabulary_' . $category->tid] = [
            '#title' => $this->t('You will add these tags'),
            '#prefix' => '<h2>' . $category->name . '</h2>',
            '#type' => 'entity_autocomplete_tagify',
            '#target_type' => 'taxonomy_term',
            '#tags' => TRUE,
            '#selection_handler' => 'default',
            '#selection_settings' => [
              'target_bundles' => ['tags'],
              'bundle' => ('tags'),
            ],
            '#autocreate' => [
              'bundle' => 'tags',
            ],
            '#description' => $this->t('for all documents below simultaneously'),
            '#wrapper_attributes' => [
              'id' => 'odt-filter',
              'class' => ['container-inline'],
            ],
            '#max_items' => 10,
            '#suggestions_dropdown' => 1,
            '#match_operator' => 'CONTAINS',
            '#show_entity_id' => 0,
            '#identifier' => 'vocabulary_' . $category->tid,
          ];

I'm not quite sure what '#identifier' is
version: '1.2.15+3-dev'
php: 8.1.27
drupal: 10.2.2
Errors:

TypeError: preg_match(): Argument #2 ($subject) must be of type string, array given in preg_match() (line 207 of ...
...\core\lib\Drupal\Core\Routing\UrlGenerator.php).

and
Warning: Undefined array key "#placeholder" in Drupal\tagify\Element\EntityAutocompleteTagify::processEntityAutocompleteTagify() (line 152 of ...\modules\contrib\tagify\src\Element\EntityAutocompleteTagify.php)

gxleano’s picture

Hi @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.

gxleano’s picture

So, your example code should works as expected with this new feature.

BTW: The #identifier attribute is used to differentiate between multiple Tagify elements in a form and it will be optional.

DieterHolvoet made their first commit to this issue’s fork.

gxleano’s picture

Status: Needs review » Fixed

It will be included on release 1.2.18

gxleano’s picture

Status: Fixed » Closed (fixed)