Programmatically create vocabulary

×

Error message

  • Deprecated function: Return type of DatabaseStatementBase::execute($args = [], $options = []) should either be compatible with PDOStatement::execute(?array $params = null): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2244 of /var/www/html/drupaldeveloper.in/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /var/www/html/drupaldeveloper.in/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /var/www/html/drupaldeveloper.in/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /var/www/html/drupaldeveloper.in/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /var/www/html/drupaldeveloper.in/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /var/www/html/drupaldeveloper.in/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseCondition::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 1652 of /var/www/html/drupaldeveloper.in/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property MergeQuery::$condition is deprecated in MergeQuery->__construct() (line 1344 of /var/www/html/drupaldeveloper.in/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1865 of /var/www/html/drupaldeveloper.in/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1865 of /var/www/html/drupaldeveloper.in/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1865 of /var/www/html/drupaldeveloper.in/includes/database/query.inc).
  • Deprecated function: Creation of dynamic property DatabaseCondition::$stringVersion is deprecated in DatabaseCondition->compile() (line 1865 of /var/www/html/drupaldeveloper.in/includes/database/query.inc).

In some situation, you module is required to create a taxonomy which the module use to perform other tasks/features it offers. This is when you can write a code to craete the require vocabulary in the module and make it availabel as soon as the module is installed. You can also add terms programatically to that vocabulary!

Using taxonomy_save_vocabulary for Drupal 5.x & 6.x and taxonomy_vocabulary_save for 7.x, you can create the vocabulary programmatically in your module.

Here is a Drupal 6.x example:

<?php
$vocabulary = array(
'name' => t("Currency"), //Human readable name of the vocabulary
'multiple' => 0, //set 1 to allow multiple selection
'required' => 0, //set 1 to make the terms mandatory to be selected
'hierarchy' => 0, //set 1 to allow and create hierarchy of the terms within the vocabulary
'relations' => 0, //set 1 to set and allow relation amongst multiple terms
'module' => 'mymodule', //provide the module name in which the vocabulary is defined and which is calling this function
'nodes' => array('mymodule' => 1), //set the node to which this vocabulary will be attached to
'weight' => -9, //set the weight to display the vocabulary in the list
);

taxonomy_save_vocabulary($vocabulary); //saving the $vocabulary array will create a vocabulary named "Currency". Note the t() in the 'name', this will allow to translate the vocabulary name to different languages.
?>

The advantage of creating the vocabulary programmatically is to eliminate to configure the vocabulary manually after installing the module. This is like carrying everything with you what ever you need.

Programmatically create vocabulary | Drupal Developer

Error

The website encountered an unexpected error. Please try again later.