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 term programmatically on Drupal 8

By editor , 2 December, 2015
Question

How to add a taxonomy term programmatically in drupal 8 ?

Example : Create a terom of the vocabulary 'test_vocabulary'

//Minimum
$term = \Drupal\taxonomy\Entity\Term::create([
          'vid' => 'test_vocabulary',
          'name' => 'My tag',
    ]);
$term->save();

//With options
$term = \Drupal\taxonomy\Entity\Term::create([
          'vid' => 'test_vocabulary',
          'langcode' => 'en',
          'name' => 'My tag',
          'description' => [
            'value' => '<p>My description.</p>',
            'format' => 'full_html',
          ],
          'weight' => -1,
          'parent' => array(0),
    ]);
 $term->save();

//Add term with a custom field. Example : 'field_url'
$term = Term::create([
            'vid' => 'test_vocabulary',
            'name' => 'The Name',
            'field_url' => [$url],
      ]);
$term->save();

//Check Taxonomy name exist
    $query = \Drupal::entityQuery('taxonomy_term');
    $query->condition('vid', "test_vocabulary");
    $query->condition('name', "My tag");
    $tids = $query->execute();

Thanks to : https://gist.github.com/facine/35bb291811c146b6fc9e

Tags

  • Drupal 8
  • Taxonomy
  • Code
  • 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.

Juston (not verified)

9 years 11 months ago

Running Check in Drush

I ran the check in Drush, but the first line throws "\Drupal::$container is not initialized yet".

What am I missing?

  • Reply

Mohsin (not verified)

9 years 9 months ago

How do i use this code

Hello there,

Can you please guide where to put this code so that it can be executed. I do not know I have to create a separate PHP file or I need to use in theme_name.them file or I have to create a custom module to use this.

Please guide me.

Thanks in advance.

  • Reply

editor

9 years 9 months ago

In reply to How do i use this code by Mohsin (not verified)

Create a custom module

For that you must create a custom module. Here a guide to create a custom module. You can also do in your template.php but it's not recommended.

  • Reply

luis fc (not verified)

9 years 4 months ago

In reply to How do i use this code by Mohsin (not verified)

Run drush php-script

Just create a php file inside your drupal installation and run "drush php-script your_file.php"

  • Reply

ravi (not verified)

9 years 3 months ago

Insert custom field programatically

Hi,

how to insert custom field value to taxonomy to newly created taxonomy.

  • Reply

Ben (not verified)

8 years 3 months ago

deprecated message

Using Term::create() as shown above generates the error,

PHP Deprecated:  Automatically creating the first item for computed fields is deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.0. Use \Drupal\Core\TypedData\ComputedItemListTrait instead.

  • 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