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();
  • where are you writing this code? I mean which hook or where this snippet is written – Shreya Shetty Nov 29 '18 at 4:28
  • @ShreyaShetty It is inside a hook – Akhil Mohandas Nov 29 '18 at 4:59
0

Should just need to do:

$new_term = \Drupal\taxonomy\Entity\Term::create([
 'vid' => 'field_school_name',
 'name' => 'ABCD',
 'field_school_id' => '1234'
]);
  • That worked! if the field_school_id is a reference entity how do I assign the value? – Akhil Mohandas Nov 29 '18 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 Nov 29 '18 at 14:28

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.