Skip to main content

At this time you should add term in little bit another way (in compare with thisthis answer) First of all in your file begin you should write

use Drupal\taxonomy\Entity\Term;

Because Term class listed in Drupal\taxonomy\Entity. And you don't need to pass taxonomy_term parametr to

Term::create

because only one parametr is needed (array with values) (below listed code for this method in taxonomy module)

public function create(array $values = array()) {
  // Save new terms with no parents by default.
  if (empty($values['parent'])) {
    $values['parent'] = array(0);
  }
  $entity = parent::create($values);
  return $entity;
}

So the final example is

use Drupal\taxonomy\Entity\Term;
$categories_vocabulary = 'blog_categories'; // Vocabulary machine name
$categories = ['test 1', 'test 2', 'test 3', 'test 4']; // List of test terms
foreach ($categories as $category) {
  $term = Term::create(array(
    'parent' => array(),
    'name' => $category,
    'vid' => $categories_vocabulary,
  ))->save();
}

At this time you should add term in little bit another way (in compare with this answer) First of all in your file begin you should write

use Drupal\taxonomy\Entity\Term;

Because Term class listed in Drupal\taxonomy\Entity. And you don't need to pass taxonomy_term parametr to

Term::create

because only one parametr is needed (array with values) (below listed code for this method in taxonomy module)

public function create(array $values = array()) {
  // Save new terms with no parents by default.
  if (empty($values['parent'])) {
    $values['parent'] = array(0);
  }
  $entity = parent::create($values);
  return $entity;
}

So the final example is

use Drupal\taxonomy\Entity\Term;
$categories_vocabulary = 'blog_categories'; // Vocabulary machine name
$categories = ['test 1', 'test 2', 'test 3', 'test 4']; // List of test terms
foreach ($categories as $category) {
  $term = Term::create(array(
    'parent' => array(),
    'name' => $category,
    'vid' => $categories_vocabulary,
))->save();

At this time you should add term in little bit another way (in compare with this answer) First of all in your file begin you should write

use Drupal\taxonomy\Entity\Term;

Because Term class listed in Drupal\taxonomy\Entity. And you don't need to pass taxonomy_term parametr to

Term::create

because only one parametr is needed (array with values) (below listed code for this method in taxonomy module)

public function create(array $values = array()) {
  // Save new terms with no parents by default.
  if (empty($values['parent'])) {
    $values['parent'] = array(0);
  }
  $entity = parent::create($values);
  return $entity;
}

So the final example is

use Drupal\taxonomy\Entity\Term;
$categories_vocabulary = 'blog_categories'; // Vocabulary machine name
$categories = ['test 1', 'test 2', 'test 3', 'test 4']; // List of test terms
foreach ($categories as $category) {
  $term = Term::create(array(
    'parent' => array(),
    'name' => $category,
    'vid' => $categories_vocabulary,
  ))->save();
}
replaced http://drupal.stackexchange.com/ with https://drupal.stackexchange.com/
Source Link

At this time you should add term in little bit another way (in compare with thisthis answer) First of all in your file begin you should write

use Drupal\taxonomy\Entity\Term;

Because Term class listed in Drupal\taxonomy\Entity. And you don't need to pass taxonomy_term parametr to

Term::create

because only one parametr is needed (array with values) (below listed code for this method in taxonomy module)

public function create(array $values = array()) {
  // Save new terms with no parents by default.
  if (empty($values['parent'])) {
    $values['parent'] = array(0);
  }
  $entity = parent::create($values);
  return $entity;
}

So the final example is

use Drupal\taxonomy\Entity\Term;
$categories_vocabulary = 'blog_categories'; // Vocabulary machine name
$categories = ['test 1', 'test 2', 'test 3', 'test 4']; // List of test terms
foreach ($categories as $category) {
  $term = Term::create(array(
    'parent' => array(),
    'name' => $category,
    'vid' => $categories_vocabulary,
))->save();

At this time you should add term in little bit another way (in compare with this answer) First of all in your file begin you should write

use Drupal\taxonomy\Entity\Term;

Because Term class listed in Drupal\taxonomy\Entity. And you don't need to pass taxonomy_term parametr to

Term::create

because only one parametr is needed (array with values) (below listed code for this method in taxonomy module)

public function create(array $values = array()) {
  // Save new terms with no parents by default.
  if (empty($values['parent'])) {
    $values['parent'] = array(0);
  }
  $entity = parent::create($values);
  return $entity;
}

So the final example is

use Drupal\taxonomy\Entity\Term;
$categories_vocabulary = 'blog_categories'; // Vocabulary machine name
$categories = ['test 1', 'test 2', 'test 3', 'test 4']; // List of test terms
foreach ($categories as $category) {
  $term = Term::create(array(
    'parent' => array(),
    'name' => $category,
    'vid' => $categories_vocabulary,
))->save();

At this time you should add term in little bit another way (in compare with this answer) First of all in your file begin you should write

use Drupal\taxonomy\Entity\Term;

Because Term class listed in Drupal\taxonomy\Entity. And you don't need to pass taxonomy_term parametr to

Term::create

because only one parametr is needed (array with values) (below listed code for this method in taxonomy module)

public function create(array $values = array()) {
  // Save new terms with no parents by default.
  if (empty($values['parent'])) {
    $values['parent'] = array(0);
  }
  $entity = parent::create($values);
  return $entity;
}

So the final example is

use Drupal\taxonomy\Entity\Term;
$categories_vocabulary = 'blog_categories'; // Vocabulary machine name
$categories = ['test 1', 'test 2', 'test 3', 'test 4']; // List of test terms
foreach ($categories as $category) {
  $term = Term::create(array(
    'parent' => array(),
    'name' => $category,
    'vid' => $categories_vocabulary,
))->save();
added 5 characters in body
Source Link

At this time you should add term in little bit another way (in compare with this answer) First of all in your file begin you should write

use Drupal\taxonomy\Entity\Term;

Because Term class listed in Drupal\taxonomy\Entity. And you don't need to pass taxonomy_term parametr to

Term::create

because only one parametr is needed (array with values) (below listed code for this method in taxonomy module)

public function create(array $values = array()) {
  // Save new terms with no parents by default.
  if (empty($values['parent'])) {
    $values['parent'] = array(0);
  }
  $entity = parent::create($values);
  return $entity;
}

So the final example is

use Drupal\taxonomy\Entity\Term;
$categories_vocabulary = 'blog_categories'; // Vocabulary vidmachine name
$categories = ['test 1', 'test 2', 'test 3', 'test 4']; // List of test terms
foreach ($categories as $category) {
  $term = Term::create(array(
    'parent' => array(),
    'name' => $category,
    'vid' => $categories_vocabulary,
))->save();
}

At this time you should add term in little bit another way (in compare with this answer) First of all in your file begin you should write

use Drupal\taxonomy\Entity\Term;

Because Term class listed in Drupal\taxonomy\Entity. And you don't need to pass taxonomy_term parametr to

Term::create

because only one parametr is needed (array with values) (below listed code for this method in taxonomy module)

public function create(array $values = array()) {
// Save new terms with no parents by default.
if (empty($values['parent'])) {
  $values['parent'] = array(0);
}
$entity = parent::create($values);
return $entity;
}

So the final example is

use Drupal\taxonomy\Entity\Term;
$categories_vocabulary = 'blog_categories'; // Vocabulary vid
$categories = ['test 1', 'test 2', 'test 3', 'test 4']; // List of test terms
foreach ($categories as $category) {
  $term = Term::create(array(
    'parent' => array(),
    'name' => $category,
    'vid' => $categories_vocabulary,
))->save();
}

At this time you should add term in little bit another way (in compare with this answer) First of all in your file begin you should write

use Drupal\taxonomy\Entity\Term;

Because Term class listed in Drupal\taxonomy\Entity. And you don't need to pass taxonomy_term parametr to

Term::create

because only one parametr is needed (array with values) (below listed code for this method in taxonomy module)

public function create(array $values = array()) {
  // Save new terms with no parents by default.
  if (empty($values['parent'])) {
    $values['parent'] = array(0);
  }
  $entity = parent::create($values);
  return $entity;
}

So the final example is

use Drupal\taxonomy\Entity\Term;
$categories_vocabulary = 'blog_categories'; // Vocabulary machine name
$categories = ['test 1', 'test 2', 'test 3', 'test 4']; // List of test terms
foreach ($categories as $category) {
  $term = Term::create(array(
    'parent' => array(),
    'name' => $category,
    'vid' => $categories_vocabulary,
))->save();
added 432 characters in body
Source Link
Loading
Source Link
Loading