1

The document says Entity::create() takes

"$values (optional) An array of values to set, keyed by property name"

as an arguement.

I also found snippets like

$term = Term::create([
    'name' => $term_name,
    'vid' => $vocabulary_name,
  ]);

and they worked.

But how exactly do you know what kind of values can be set in the array?
In the case above, where did people take the keys 'name' and 'vid' from?

1

If you look at the of baseFieldDefinitions method in class Term:

Here is a list of fields you can set in create Term:

  1. tid : Term ID
  2. uuid : The term UUID.
  3. vid: The vocabulary to which the term is assigned.
  4. langcode : The term language code.
  5. name : Name of term.
  6. description : description of term
  7. weight : weight of term.
  8. parent : Term parent.

Example:

$term = Term::create([
    'name' => 'term name',
    'vid' => 'tags',
    'status' => 1,
    'tid' => 13,
    'uuid' => 1,
    'langcode' => 'en',
    'description' => 'tags description',
    'weight' => -12,
    //'parent' => parent tid
  ]);
  $term->save();
1

Load an existing term

$term = Term::load(1);

or create an empty term to get the vocabulary specific bundle fields

$term = Term::create([
  'vid' => $vocabulary_name,
]);

and dump it as an array:

var_dump($term->toArray());

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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