Last active
March 11, 2021 09:44
-
-
Save sheanhoxie/6ee2fb9f70050d981ff7f7253de0424e to your computer and use it in GitHub Desktop.
Methods for creating, and updating multi-value fields in Drupal 8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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