Skip to content

Form elements

Tagify provides two render elements for custom forms. Both attach the Tagify library and the Gin or Claro styles when one of those themes is active.

entity_autocomplete_tagify

An entity autocomplete input rendered as Tagify. It is a text field that queries entity reference selection handlers, like core's entity_autocomplete element, and returns the selected entity IDs.

Property Required Description
#target_type yes Entity type ID to reference.
#selection_handler no Selection handler plugin, for example default.
#selection_settings no Settings passed to the selection handler, such as target_bundles. An info_label key adds the info label.
#autocreate no bundle and optional uid for entities created from new tags. Omit to disable autocreate.
#match_operator yes STARTS_WITH or CONTAINS.
#suggestions_dropdown yes 0 opens the list on click, 1 after the first character.
#max_items no Number of suggestions shown.
#show_entity_id no 1 adds the entity ID to each tag.
#parent_selection no 1 allows selecting parent terms in hierarchical vocabularies.
#identifier no Unique name, needed when a form has several Tagify elements.
$form['related'] = [
  '#type' => 'entity_autocomplete_tagify',
  '#title' => $this->t('Related articles'),
  '#target_type' => 'node',
  '#tags' => TRUE,
  '#default_value' => $entities,
  '#selection_handler' => 'default',
  '#selection_settings' => [
    'target_bundles' => ['article', 'page'],
    'info_label' => '[node:type]',
  ],
  '#autocreate' => [
    'bundle' => 'article',
  ],
  '#max_items' => 10,
  '#suggestions_dropdown' => 1,
  '#match_operator' => 'CONTAINS',
  '#show_entity_id' => 0,
  '#identifier' => 'related',
  '#parent_selection' => 1,
];

select_tagify

A select element rendered as Tagify. It takes the same properties as core's select element (#options, #multiple, #default_value, #required, and so on), including nested option groups. It also accepts #match_operator (default CONTAINS) and #match_limit (default 20).

$form['color'] = [
  '#type' => 'select_tagify',
  '#title' => $this->t('Colors'),
  '#multiple' => TRUE,
  '#options' => [
    'red' => $this->t('Red'),
    'green' => $this->t('Green'),
    'blue' => $this->t('Blue'),
  ],
];