Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Create Taxonomy Vocabulary programmatically on Drupal 8

By editor , 6 December, 2015
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();
    }

Tags

  • Drupal 8
  • Code
  • Taxonomy
  • Add new comment

Comments6

Plain text

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

shreeniwas (not verified)

10 years 11 months ago

File for above code

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

  • Reply

editor

10 years 6 months ago

In reply to File for above code by shreeniwas (not verified)

You must create a new module…

You must create a new module like this.

  • Reply

alok (not verified)

10 years 4 months ago

nice .helpfull  …

nice .helpfull  .

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

 

  • Reply

Brian Wagner (not verified)

9 years 5 months ago

machine_name is not used

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

  • Reply

editor

9 years 5 months ago

In reply to machine_name is not used by Brian Wagner (not verified)

Thank you

Thank you !!!

  • Reply

Watson (not verified)

9 years 3 months ago

In reply to Thank you by editor

Well, that is right about…

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.

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer