Skip to content

Instantly share code, notes, and snippets.

@sheanhoxie
Last active March 11, 2021 09:44
Show Gist options
  • Select an option

  • Save sheanhoxie/6ee2fb9f70050d981ff7f7253de0424e to your computer and use it in GitHub Desktop.

Select an option

Save sheanhoxie/6ee2fb9f70050d981ff7f7253de0424e to your computer and use it in GitHub Desktop.
Methods for creating, and updating multi-value fields in Drupal 8
Add a single value to an existing multi-value field
---------------------------------------------
$value = 'text';
$profile = Profile::load(uid);
$profile->get('field_name')->appendItem($value);
Add values to a multi-value during entity creation
--------------------------------------------------
$node = $nodeManager->create([
'type' => 'custom_node_type',
]);
$values = [
'multi_value_0' => 421,
'multi_value_1' => t('Sup Dawg'),
'multi_value_2' => $something->id(),
];
$node->get('field_multivalue_name')->appendItem($values);
$node->save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment