0

I have a taxonomy vocabulary called, school and the machine name is field_school_name. The vocabulary has a field called school id (machine name:field_school_id). I am able to add a verb successfully using the following code. How do I add the field value programatically? I want to add '1234' as the school id associated with the verb 'ABCD' I created.

$new_term = \Drupal\taxonomy\Entity\Term::create([
    'vid' => 'field_school_name',
    'name' => 'ABCD',
]);

$new_term->enforceIsNew();
$new_term->save();
2
  • where are you writing this code? I mean which hook or where this snippet is written Commented Nov 29, 2018 at 4:28
  • @ShreyaShetty It is inside a hook Commented Nov 29, 2018 at 4:59

1 Answer 1

0

Should just need to do:

$new_term = \Drupal\taxonomy\Entity\Term::create([
 'vid' => 'field_school_name',
 'name' => 'ABCD',
 'field_school_id' => '1234'
]);
2
  • That worked! if the field_school_id is a reference entity how do I assign the value? Commented Nov 29, 2018 at 14:23
  • You'd need to give it the entity ID of the entity you want to reference and the entity API should take care of the rest
    – Leigh
    Commented Nov 29, 2018 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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