Skip to content

Instantly share code, notes, and snippets.

@djevans
Last active March 22, 2016 16:36
Show Gist options
  • Save djevans/0705aef99cbdd51ea63f to your computer and use it in GitHub Desktop.
Save djevans/0705aef99cbdd51ea63f to your computer and use it in GitHub Desktop.
Setting multi-valued reference fields programmatically in Drupal 8.
<?php
use Drupal\node\Entity\Node;
// node 1 has 'field_ref', which is a multi-valued field currently referencing nodes 2 and 3.
$node = Node::load(1);
$node->field_ref[] = 4;
$node->save(); // 'field_ref' now contains [2,3,4].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment