setting default_value when using in a custom module

Created on 18 May 2025, 4 months ago

Problem/Motivation

Im trying to use entity_autocomplete_tagify in a multistep form, but if I take the contents of the form field and feed this into default_value,

example of what im trying to put into default_value

[@"value":"11","entity_id":11,"info_label":null,"label":"Website Development","editable":false,"input":"we"]
TypeError: Drupal\Core\Entity\EntityRepository::getTranslationFromContext(): Argument #1 ($entity) must be of type Drupal\Core\Entity\EntityInterface, string given, called in /var/www/sites/drupal/multisite/web/modules/contrib/tagify/src/Element/EntityAutocompleteTagify.php on line 247 in Drupal\Core\Entity\EntityRepository->getTranslationFromContext() (line 94 of /var/www/sites/drupal/multisite/web/core/lib/Drupal/Core/Entity/EntityRepository.php). 

To help me get through this a working example prepopulating the field would help, I thought even

$default_value[] = [
        'value' => 11,
        'label' => 'Web development',
        'entity_id' => 11,
        'info_label' => '',
        'editable' => FALSE
    ];

would work, but no joy. any help would be very welcome.

Steps to reproduce


    $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadMultiple([11, 7]);
    $default_tags = [];
    foreach ($terms as $term) {
       $default_tags[] = ['value' => $term->label(), 'entity_id' => $term->id()];
    }

    $form['sector'] = [
      '#type'          => 'entity_autocomplete_tagify',
      '#target_type'   => 'taxonomy_term',
      '#default_value'  => $default_tags,
      '#title'         => $this->t('What industry sectors are your business in'),
      '#required'      => TRUE,
      '#tags' => TRUE,
      '#selection_handler' => 'default',
      '#selection_settings' => [
          'target_bundles' => ['sector'],
          'bundle' => ('sector'),
          'info_label' => '',
      ],
      '#autocreate' => [
          'bundle' => 'sector',
      ],
      '#description' => $this->t('Order from most to least important to your business (max 5)'),
      '#wrapper_attributes' => [
          'id' => 'odt-filter',
          'class' => ['container-inline'],
      ],
      '#max_items' => 5,
      '#suggestions_dropdown' => 1,
      '#match_operator' => 'CONTAINS',
      '#show_entity_id' => 0,
    ];

    $logger->info($this->store->get('sector'));
💬 Support request
Status

Active

Version

1.2

Component

Documentation

Created by

🇦🇺Australia digitalcatalyst

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @digitalcatalyst
  • 🇦🇺Australia digitalcatalyst

    ok, I got this working by stripping the entity_id from the tagify json, using,

           $defvalue = [];
           foreach ($entity_ids as $entity_id) {
              $entity = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($entity_id);
              $translated_entity = \Drupal::service('entity.repository')->getTranslationFromContext($entity);
              array_push($defvalue,$translated_entity);
           }
    

    leaving me $defvalue to use in default_value.

    this seems a lot of boiler plate to reuse the tagify json as a default value, is this expected ?

  • 🇦🇺Australia digitalcatalyst

    im closing this issue as I've found the work through, and dont want to waste peoples time, hopefully my notes in the ticket help others who might hit this issue,cheers.

Production build 0.71.5 2024