Skip to content

Entity field kits

Kits in src/Kits/Field/Entity/ that reference content entities. The autocomplete kits extend the abstract EntityAutoCompleteKit. Selection settings and filters are stored under #selection_settings; setFilter() also sets #selection_handler to default:entity_by_field if no handler is set yet.

EntityAutoCompleteKit

Abstract base for entity_autocomplete kits. Subclasses set the static $targetType. setCondition() takes a Condition object from the Query module (drupal/query) and converts it to a selection filter.

Class Drupal\formfactorykits\Kits\Field\Entity\EntityAutoCompleteKit (src/Kits/Field/Entity/EntityAutoCompleteKit.php), abstract
Extends FieldKit
Factory method none (abstract)
Form API #type entity_autocomplete
Default ID none (pass one)
Traits DefaultValueTrait, PrefixTrait, SuffixTrait, ValueTrait

Setters

Method From
setCondition(Drupal\query\Common\Condition $condition) EntityAutoCompleteKit
setDefaultValue(array\|string\|null $value) DefaultValueTrait (trait)
setFilter(string $key, array\|string $value) EntityAutoCompleteKit
setPrefix(array\|string\|null $value) PrefixTrait (trait)
setSelectionHandler(string $pluginID) EntityAutoCompleteKit
setSelectionSetting(string $key, array\|string\|null $value) EntityAutoCompleteKit
setSelectionSettings(array $settings) EntityAutoCompleteKit
setSuffix(array\|string\|null $value) SuffixTrait (trait)
setTargetType(string $type) EntityAutoCompleteKit
setValue(array\|string\|null $value) ValueTrait (trait)

NodeAutoCompleteKit

Autocomplete for nodes. Default title t('Node').

Class Drupal\formfactorykits\Kits\Field\Entity\NodeAutoCompleteKit (src/Kits/Field/Entity/NodeAutoCompleteKit.php)
Extends EntityAutoCompleteKit
Factory method $kits->nodeAutoComplete()
Form API #type entity_autocomplete
Default ID node_autocomplete
Traits DefaultValueTrait, DescriptionTrait, PrefixTrait, SuffixTrait, TitleTrait, ValueTrait

Setters

Method From
setCondition(Drupal\query\Common\Condition $condition) EntityAutoCompleteKit
setDefaultValue(array\|string\|null $value) DefaultValueTrait (trait)
setDescription(string $description) DescriptionTrait (trait)
setFilter(string $key, array\|string $value) EntityAutoCompleteKit
setPrefix(array\|string\|null $value) PrefixTrait (trait)
setSelectionHandler(string $pluginID) EntityAutoCompleteKit
setSelectionSetting(string $key, array\|string\|null $value) EntityAutoCompleteKit
setSelectionSettings(array $settings) EntityAutoCompleteKit
setSuffix(array\|string\|null $value) SuffixTrait (trait)
setTargetBundle(string $bundle) NodeAutoCompleteKit
setTargetBundles(array $bundles) NodeAutoCompleteKit
setTargetType(string $type) EntityAutoCompleteKit
setTitle(string $title) TitleTrait (trait)
setValue(array\|string\|null $value) ValueTrait (trait)

Example

$kit = $kits->nodeAutoComplete('related')->setTargetBundle('article');

Generated render array ([$kit->getID() => $kit->getArray()])

[
  'related' => [
    '#target_type' => 'node',
    '#title' => t('Node'),
    '#type' => 'entity_autocomplete',
    '#selection_settings' => [
      'target_bundles' => ['article'],
    ],
  ],
]

UserAutoCompleteKit

Autocomplete for users. Default title t('User').

Class Drupal\formfactorykits\Kits\Field\Entity\UserAutoCompleteKit (src/Kits/Field/Entity/UserAutoCompleteKit.php)
Extends EntityAutoCompleteKit
Factory method $kits->userAutoComplete()
Form API #type entity_autocomplete
Default ID user_autocomplete
Traits DefaultValueTrait, DescriptionTrait, PrefixTrait, SuffixTrait, TitleTrait, ValueTrait

Setters

Method From
setCondition(Drupal\query\Common\Condition $condition) EntityAutoCompleteKit
setDefaultValue(array\|string\|null $value) DefaultValueTrait (trait)
setDescription(string $description) DescriptionTrait (trait)
setFilter(string $key, array\|string $value) EntityAutoCompleteKit
setPrefix(array\|string\|null $value) PrefixTrait (trait)
setSelectionHandler(string $pluginID) EntityAutoCompleteKit
setSelectionSetting(string $key, array\|string\|null $value) EntityAutoCompleteKit
setSelectionSettings(array $settings) EntityAutoCompleteKit
setSuffix(array\|string\|null $value) SuffixTrait (trait)
setTargetType(string $type) EntityAutoCompleteKit
setTitle(string $title) TitleTrait (trait)
setValue(array\|string\|null $value) ValueTrait (trait)

Example

$kit = $kits->userAutoComplete('owner');

Generated render array ([$kit->getID() => $kit->getArray()])

[
  'owner' => [
    '#target_type' => 'user',
    '#title' => t('User'),
    '#type' => 'entity_autocomplete',
  ],
]

TaxonomyTermAutoCompleteKit

Autocomplete for taxonomy terms. If no title is set, getArray() loads the vocabulary's name from config (taxonomy.vocabulary.<vid>) and uses it as the title. Note that setVocabulary() stores the vocabulary ID as a #vid property; it does not restrict the autocomplete to that vocabulary. Use setSelectionSetting('target_bundles', [...]) for that.

Class Drupal\formfactorykits\Kits\Field\Entity\TaxonomyTermAutoCompleteKit (src/Kits/Field/Entity/TaxonomyTermAutoCompleteKit.php)
Extends EntityAutoCompleteKit
Factory method $kits->taxonomyTermAutoComplete()
Form API #type entity_autocomplete
Default ID entity_autocomplete
Traits ConfigFactoryTrait, DefaultValueTrait, DescriptionTrait, PrefixTrait, SuffixTrait, TitleTrait, ValueTrait

Setters

Method From
setCondition(Drupal\query\Common\Condition $condition) EntityAutoCompleteKit
setDefaultValue(array\|string\|null $value) DefaultValueTrait (trait)
setDescription(string $description) DescriptionTrait (trait)
setFilter(string $key, array\|string $value) EntityAutoCompleteKit
setPrefix(array\|string\|null $value) PrefixTrait (trait)
setSelectionHandler(string $pluginID) EntityAutoCompleteKit
setSelectionSetting(string $key, array\|string\|null $value) EntityAutoCompleteKit
setSelectionSettings(array $settings) EntityAutoCompleteKit
setSuffix(array\|string\|null $value) SuffixTrait (trait)
setTargetType(string $type) EntityAutoCompleteKit
setTitle(string $title) TitleTrait (trait)
setValue(array\|string\|null $value) ValueTrait (trait)
setVocabulary(string $vid) TaxonomyTermAutoCompleteKit

Example

$kit = $kits->taxonomyTermAutoComplete('tags')->setTitle('Tags')->setSelectionSetting('target_bundles', ['tags']);

Generated render array ([$kit->getID() => $kit->getArray()])

[
  'tags' => [
    '#target_type' => 'taxonomy_term',
    '#type' => 'entity_autocomplete',
    '#title' => 'Tags',
    '#selection_settings' => [
      'target_bundles' => ['tags'],
    ],
  ],
]

TaxonomyTermSelectKit

A select (extends SelectKit) filled with the terms of a vocabulary. loadTaxonomyVocabulary($vid) runs an entity query, loads the terms and appends one option per term; if no title is set it uses the vocabulary name, with a trailing "s" trimmed for single-value selects.

Class Drupal\formfactorykits\Kits\Field\Entity\TaxonomyTermSelectKit (src/Kits/Field/Entity/TaxonomyTermSelectKit.php)
Extends SelectKit
Factory method $kits->taxonomyTermSelect()
Form API #type select
Default ID term
Traits ConfigFactoryTrait, DefaultValueTrait, DescriptionTrait, EntityTrait, OptionsTrait, PrefixTrait, StringTranslationTrait, SuffixTrait, TitleTrait, ValueTrait

Setters

Method From
appendOption(Drupal\formfactorykits\Kits\Field\Common\OptionKit\|array $mixed) OptionsTrait (trait)
loadTaxonomyVocabulary(string $vid) TaxonomyTermSelectKit
setDefaultValue(array\|string\|null $value) DefaultValueTrait (trait)
setDescription(string $description) DescriptionTrait (trait)
setMultiple(bool $isMultiple = TRUE) TaxonomyTermSelectKit
setOptions(array $options) OptionsTrait (trait)
setPrefix(array\|string\|null $value) PrefixTrait (trait)
setStringTranslation(Drupal\Core\StringTranslation\TranslationInterface $translation) OptionsTrait (trait)
setSuffix(array\|string\|null $value) SuffixTrait (trait)
setTitle(string $title) TitleTrait (trait)
setTrimLoadedName(bool $trim = TRUE) TaxonomyTermSelectKit
setValue(array\|string\|null $value) ValueTrait (trait)

loadTaxonomyVocabulary() needs a bootstrapped Drupal (entity type manager and config factory), so the example below does not call it.

Example

$kit = $kits->taxonomyTermSelect('category')->setTitle('Category');

Generated render array ([$kit->getID() => $kit->getArray()])

[
  'category' => [
    '#empty_option' => '',
    '#type' => 'select',
    '#title' => 'Category',
  ],
]