Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dreambubbler/a89b9ac5ff34facd60fc7d4c8afecc9b to your computer and use it in GitHub Desktop.

Select an option

Save dreambubbler/a89b9ac5ff34facd60fc7d4c8afecc9b to your computer and use it in GitHub Desktop.

Revisions

  1. dreambubbler revised this gist Apr 11, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ExampleAttachTermsProgrammatically.php
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@
    */

    // Example 1: attaching a single term
    $node = Node::load($nid);
    $node = \Drupal\node\Entity\Node::load($nid);

    // Attach only one term
    $tid = 1; // The ID of the term to attach.
    @@ -22,7 +22,7 @@
    // End of Example 1 />

    // Example 2: attaching multiple terms
    $node2 = Node::load($nid2);
    $node2 = \Drupal\node\Entity\Node::load($nid2);
    // To attach multiple terms, the term IDs must be in an array.
    $multiple_tids = array(1, 2, 3); // Each is Term ID of an existing term.

  2. dreambubbler revised this gist Apr 11, 2017. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions ExampleAttachTermsProgrammatically.php
    Original file line number Diff line number Diff line change
    @@ -6,25 +6,26 @@
    * Must know:
    * - field_example_name: the full name of the term reference field
    * - tid: the term ID(s) to attach
    *
    * Keep in mind that this example uses Node::load()
    * but you can use any Entity::load()
    * e.g. User::load(), Term::load(), etc.
    */

    // Example 1: attaching a single term

    // Work with a full node object.
    $node = Node::load($nid);

    // Attach only one term
    $tid = 1; // The ID of the term to attach.
    $node->set('field_example_name', $tid);
    $node->save();
    // End of Example 1
    // End of Example 1 />

    // Example 2: attaching multiple terms
    // Work with a full node object.
    $node2 = Node::load($nid2);
    // To attach multiple terms, the term IDs must be in an array.
    $multiple_tids = array(1, 2, 3); // Each is Term ID of an existing term.

    $node2->set('field_example_name', $multiple_tids); // Note that field_example_name must allow multiple terms.
    $node2->save();
    // End of Example 2
    // End of Example 2 />
  3. dreambubbler created this gist Apr 11, 2017.
    30 changes: 30 additions & 0 deletions ExampleAttachTermsProgrammatically.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    <?php
    use Drupal\node\Entity\Node;

    /**
    * Before attaching a term(s) to a term reference field,
    * Must know:
    * - field_example_name: the full name of the term reference field
    * - tid: the term ID(s) to attach
    */

    // Example 1: attaching a single term

    // Work with a full node object.
    $node = Node::load($nid);

    // Attach only one term
    $tid = 1; // The ID of the term to attach.
    $node->set('field_example_name', $tid);
    $node->save();
    // End of Example 1

    // Example 2: attaching multiple terms
    // Work with a full node object.
    $node2 = Node::load($nid2);
    // To attach multiple terms, the term IDs must be in an array.
    $multiple_tids = array(1, 2, 3); // Each is Term ID of an existing term.

    $node2->set('field_example_name', $multiple_tids); // Note that field_example_name must allow multiple terms.
    $node2->save();
    // End of Example 2