1

I created a vocabulary using the following code.

$vocab = entity_create('taxonomy_vocabulary', array(
  'name' => 'vocab_name',
  'vid' => 'vocab_id',
  'description' => 'description'
));
$vocab->save();

I need to attach a custom field to it. I created the field with BaseFieldDefinition::create('string'), bu I couldn't figure out how to attach the created field to $vocab.

How can I achieve it?

1

Assuming that you want that to be default configuration as part of a module, @larowlan's answer is correct.

Sometimes things do need to be created in code/dynamcally. Look at node_add_body_field() as an example that adds a configurable body field to a node type. Note that it does not create the necessary FieldStorageConfig entity, as that is default configuration again, but if you search for FieldStorageConfig::create() in the code base, you should find dozens of examples on how to do that. Preferably look in the module of the field type you want to use, as that will likely contain examples with the necessary settings.

3

Create them in the ui, export them to yaml using the single config export ui, put the yaml files in your_module/config/install - done

1
  • sorry i shud have been more clear, thats how i am doing it now but want to do it programatically. looking at @Berdir solution now. – Naveed Quadri Dec 8 '14 at 6:45
2

Posting the complete solution here, hoping it might be helpful.

//create a vocabulary

entity_create('taxonomy_vocabulary', array(
            'name' => 'taxonomy test',
            'vid' => 'taxonomy_test',
            'description' => 'some description'
                ))->save();

//add term to the vocabulary
entity_create('taxonomy_term', array(
                    'name' => 'term test',
                    'vid' => 'taxonomy_test',
                        ))->save();
//attach a field to teh vocabulary

 \Drupal\field\Entity\FieldStorageConfig::create(array(
      'field_name' => 'taxonomy_test_field',
      'entity_type' => 'taxonomy_term',
      'type' => 'boolean',
      'settings' => array(
        'on_label' => 'on',
        'off_label' => 'off',
      ),
    ))->save();

 \Drupal\field\Entity\FieldConfig::create(array(
      'field_name' => 'taxonomy_test_field',
      'entity_type' => 'taxonomy_term',
      'bundle' => 'taxonomy_test',
      'label' => 'Field Label',
      'required' => TRUE,
    ))->save();
2
  • You also need to set up the view and form display if you want that field to be shown when viewing/editing, again, see node_add_body_field(). For the right type and settings to use there, set up the field as you want it in the UI and then check the exported view/form displays. – Berdir Dec 10 '14 at 8:52
  • yeah i checked that. i only posted the code for how to attach the field. this field will be in hidden state without display settings – Naveed Quadri Dec 10 '14 at 13:17
0
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;

$vicId = "commitee_list";
$vocName = "Commitee List";
$vocDescription = "Used for commitee list.";

$vocabulary = Vocabulary::load($vocId);
if (!$vocabulary) {
  $vocabulary = Vocabulary::create(array(
        'vid' => $vocId,            
        'description' => $vocDescription,
        'name' => $vocName,
  ));
  $status = $vocabulary->save();

// Attach a field to the vocabulary.
  FieldStorageConfig::create(array(      
  'field_name' => 'field_committee_id',
  'entity_type' => 'taxonomy_term',
  'type' => 'string',
  'settings' => array(
    'max_length' => 255,
    'is_ascii' => FALSE,
    'case_sensitive' => FALSE,
  ),
  'module' => 'core',
  'locked' => FALSE,
  'module' => 'core',
  'cardinality' => 1      
))->save();

   FieldConfig::create(array(      
  'field_name' => 'field_committee_id',
  'entity_type' => 'taxonomy_term',
  'bundle' => $vocId,
  'label' => 'Committee ID',
  'required' => TRUE,
))->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.