Create Taxonomy Vocabulary programmatically on Drupal 8 | Drupal 8

Create Taxonomy Vocabulary programmatically on Drupal 8

Submitted by editor on Sun, 12/06/2015 - 19:52
Question

How to create a taxonomy Vocabulary programmatically on Drupal 8 ?

Exaple:

    $vid = "new_voc_id";
    $name = "New Name";
    $vocabularies = \Drupal\taxonomy\Entity\Vocabulary::loadMultiple();
    if (!isset($vocabularies[$vid])) {
      $vocabulary = \Drupal\taxonomy\Entity\Vocabulary::create(array(
            'vid' => $vid,

            //'machine_name' => $vid,
            'description' => '',
            'name' => $name,
      ));
      $vocabulary->save();
    }
    else {
      // Vocabulary Already exist
      $query = \Drupal::entityQuery('taxonomy_term');
      $query->condition('vid', $vid);
      $tids = $query->execute();
    }

Comments

shreeniwas (not verified)

Wed, 12/30/2015 - 15:58

Hi, Can you please  let me know the filepath and file name where the above code is to be replaced with. 

alok (not verified)

Sat, 07/09/2016 - 21:17

nice .helpfull  .

do you have any link for complete reference changes in vocab from D7 and D8 

 

Brian Wagner (not verified)

Tue, 06/27/2017 - 21:52

machine_name is not used

Need to use:

  • name
  • vid
  • description

 

https://api.drupal.org/api/drupal/core!modules!taxonomy!src!Entity!Vocabulary.php/class/Vocabulary/8.2.x

Well, that is right about the reality and strong thought about them. <a href="http://papersowl.me/">View website</a> to know and get more knowledge about the matter.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.