1

I want to create a standard vocabulary (no extra fields) in Drupal 8.7.5 programatically, using hook_update_N. I've never used this function before. So I'm a complete newbie.

I've modified a simple delete script I was shown, but something tells me it can't possibly be this simple.

Here's my starting point:

<?php

use Drupal\taxonomy\Entity\Vocabulary;

/**
 * create vocabulary testname.
 */
function ent_create_update_8105(&$sandbox) {
  $vid = 'testname';
  $vocabulary = Vocabulary::load($vid);
  if (!$vocabulary) {
    $vocabulary->create();
  }
}

I assume that, once the script is created correctly, I simply run drush entup.

  • 2
    I assume that, once the script is created correctly, I simply run drush entup. entup was removed, run drush updb to perform updates. – Clive Aug 23 at 15:41
2

You should be able to do it like this:

<?php

use Drupal\taxonomy\Entity\Vocabulary;

/**
 * create vocabulary testname.
 */
function ent_create_update_8105(&$sandbox) {
  $vid = 'testname';
  $vocabulary = Vocabulary::load($vid);
  if (!isset($vocabulary)) {
    Vocabulary::create([
      'vid' => $vid,
      'description' => '',
      'name' => 'The vocabulary’s name',
    ])->save();
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.