You are here

public function TaxonomyManagerHelper::massAddTerms in Taxonomy Manager 2.0.x

Same name and namespace in other branches
  1. 8 src/TaxonomyManagerHelper.php \Drupal\taxonomy_manager\TaxonomyManagerHelper::massAddTerms()

Helper function for mass adding of terms.

Parameters

string $input: The textual input with terms. Each line contains a single term. Child term can be prefixed with a dash '-' (one dash for each level). Term names starting with a dash and should not become a child term need to be wrapped in quotes.

int $vid: The vocabulary id.

int $parents: An array of parent term ids for the new inserted terms. Can be 0.

array $term_names_too_long: Return value that is used to indicate that some term names were too long and truncated to 255 characters.

Return value

array An array of the newly inserted term objects

File

src/TaxonomyManagerHelper.php, line 111

Class

TaxonomyManagerHelper
Class for taxonomy manager helper.

Namespace

Drupal\taxonomy_manager

Code

public function massAddTerms($input, $vid, $parents, array &$term_names_too_long = []) {
  $new_terms = [];
  $terms = explode("\n", str_replace("\r", '', $input));
  $parents = !empty($parents) ? $parents : 0;

  // Stores the current lineage of newly inserted terms.
  $last_parents = [];
  foreach ($terms as $name) {
    if (empty($name)) {
      continue;
    }
    $matches = [];

    // Child term prefixed with one or more dashes.
    if (preg_match('/^(-){1,}/', $name, $matches)) {
      $depth = strlen($matches[0]);
      $name = substr($name, $depth);
      $current_parents = isset($last_parents[$depth - 1]) ? [
        $last_parents[$depth - 1]
          ->id(),
      ] : 0;
    }
    elseif (preg_match('/^\\"(-){1,}.*\\"/', $name, $matches)) {
      $name = substr($name, 1, -1);
      $depth = 0;
      $current_parents = $parents;
    }
    else {
      $depth = 0;
      $current_parents = $parents;
    }

    // Truncate long string names that will cause database exceptions.
    if (strlen($name) > 255) {
      $term_names_too_long[] = $name;
      $name = substr($name, 0, 255);
    }
    $filter_formats = filter_formats();
    $format = array_pop($filter_formats);

    // Set default language.
    $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;

    // Check if site is multilingual.
    if (\Drupal::moduleHandler()
      ->moduleExists('language')) {
      $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vid);
      $lang_setting = $language_configuration
        ->getDefaultLangcode();
      if ($lang_setting == 'site_default') {
        $langcode = $this->languageManager
          ->getDefaultLanguage()
          ->getId();
      }
      elseif ($lang_setting == 'current_interface') {
        $langcode = $this->languageManager
          ->getCurrentLanguage()
          ->getId();
      }
      elseif ($lang_setting == 'authors_default') {
        $langcode = $this->currentUser
          ->getPreferredLangcode();
      }
      elseif ($lang_setting == 'und' || $lang_setting == 'zxx') {
        $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
      }
      else {

        // Fixed value set like 'en'.
        $langcode = $lang_setting;
      }
    }
    $values = [
      'name' => $name,
      // @todo do we need to set a format?
      'format' => $format
        ->id(),
      'vid' => $vid,
      'langcode' => $langcode,
    ];
    if (!empty($current_parents)) {
      foreach ($current_parents as $p) {
        $values['parent'][] = [
          'target_id' => $p,
        ];
      }
    }
    $term = $this->taxonomyTypeManager
      ->create($values);
    $term
      ->save();
    $new_terms[] = $term;
    $last_parents[$depth] = $term;
  }
  return $new_terms;
}