Taxonomy references field in users

Created on 11 May 2022, over 2 years ago
Updated 22 May 2024, 6 months ago

Problem/Motivation

Need to import users from CSV and each user has a taxonomy reference as field.

Proposed resolution

It would be nice to import taxonomy reference from ID of the taxonomy term or from the value checking if that value already exist. Else the module should be allowed to create the term from the value inserted in the csv.

✨ Feature request
Status

Active

Version

2.0

Component

Code

Created by

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡¦Ukraine Ruslan Piskarov Kiev, Ukraine

    It can be resolved by custom module.

    Apply this patch https://www.drupal.org/project/user_csv_import/issues/3380676 ✨ Allows altering a user during importing Needs review .

    Add code like:

    /**
     * Implements hook_user_csv_import_create_user_alter().
     */
    function custom_module_user_csv_import_create_user_alter(UserInterface &$user, $values): void {
      if ($user->hasField('field_department')
        && $values['field_department'] !== '') {
        $tid = custom_module_create_terms('person_department', [$values['field_department']]);
        $user->set('field_department', $tid);
        $user->save();
      }
    }
    
    /**
     * Create the terms.
     *
     * @param $vid
     * @param array $values
     *
     * @return array
     * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
     * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
     * @throws \Drupal\Core\Entity\EntityStorageException
     */
    function custom_module_create_terms($vid, array $values = []): array {
      $terms = [];
    
      foreach ($values as $value) {
        if ($value != '') {
          $term = custom_module_get_tid_by_name($value, $vid);
          if ($term === FALSE) {
            $term = \Drupal::entityTypeManager()
              ->getStorage('taxonomy_term')
              ->create([
                'parent' => [],
                'name' => $value,
                'vid' => $vid,
              ]);
    
            $term->save();
          }
          $terms[] = $term;
        }
      }
    
      return $terms;
    }
    
    /**
     * Find term by name and vid.
     *
     * @param $name
     * @param $vid
     *
     * @return false
     * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
     * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
     */
    function custom_module_get_tid_by_name($name = NULL, $vid = NULL) {
      $term = custom_module_get_term_by_name($name, $vid);
    
      return !empty($term) ? $term->id() : FALSE;
    }
    
    /**
     * Get term by name.
     *
     * @param $name
     * @param $vid
     *
     * @return false|mixed
     * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
     * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
     */
    function custom_module_get_term_by_name($name = NULL, $vid = NULL): mixed {
      $properties = [];
      if (!empty($name)) {
        $properties['name'] = $name;
      }
      if (!empty($vid)) {
        $properties['vid'] = $vid;
      }
    
      $terms = \Drupal::entityTypeManager()
        ->getStorage('taxonomy_term')
        ->loadByProperties($properties);
      $term = reset($terms);
    
      return !empty($term) ? $term : FALSE;
    }
    
Production build 0.71.5 2024