You are here

public function ContentImport::getTermReference in Content Import 8

Same name and namespace in other branches
  1. 8.3 src/Form/ContentImport.php \Drupal\contentimport\Form\ContentImport::getTermReference()
  2. 8.4 src/Form/ContentImport.php \Drupal\contentimport\Form\ContentImport::getTermReference()

To get Reference field ids.

1 call to ContentImport::getTermReference()
ContentImport::createNode in src/Form/ContentImport.php
To import data as Content type nodes.

File

src/Form/ContentImport.php, line 108
Contains \Drupal\contentimport\Form\ContentImport.

Class

ContentImport
Configure Content Import settings for this site.

Namespace

Drupal\contentimport\Form

Code

public function getTermReference($voc, $terms) {
  $vocName = strtolower($voc);
  $vid = preg_replace('@[^a-z0-9_]+@', '_', $vocName);
  $vocabularies = \Drupal\taxonomy\Entity\Vocabulary::loadMultiple();

  /** Create Vocabulary if it is not exists **/
  if (!isset($vocabularies[$vid])) {
    ContentImport::createVoc($vid, $voc);
  }
  $termArray = explode(',', $terms);
  $termIds = [];
  foreach ($termArray as $term) {
    $term_id = ContentImport::getTermId($term, $vid);
    if (empty($term_id)) {
      $term_id = ContentImport::createTerm($voc, $term, $vid);
    }
    $termIds[]['target_id'] = $term_id;
  }
  return $termIds;
}