Skip to header Skip to main navigation Skip to main content Skip to footer
  • Log in
Home
DrupalVIP
Freelancer service with great care and enthusiasm.
  • Home
  • Products
  • Blog
  • Support
    • Dictionary
    • Drupal Resources
    • External Resources

Working with multiple values

Breadcrumb

  • Home
  • DrupalVIP Support
  • Working with multiple values

This example shows how to set and display multiple values for a computed field.

to populate a multi-valued field, a single value must still be returned, but your function will be called multiple times for each of your values (or "deltas"). 
So it's necessary to populate the full delta-keyed array, but then only return the value requested (for each delta).

function computed_field_FIELD_NAME_compute($entity_type_manager, $entity, $fields, $delta) {
  // Fill in array values with data.  The keys are optional in this case as they are the defaults.
  $values = [
    0 => 'Dog',
    1 => 'Cat',
    2 => 'Mouse',
  ];

  // Return the single value currently requested by the system.
  return $values[$delta];
}

 

getting the value/values

when having single value field this line will return the single value:

$val = $node->your-field-item->getValue();

but if you having a multi-value field, the same line will return an array:

 

To get all values of a multi-value field:

$node = $variables['node'];
$values = array();
foreach ($node->field_name->getValue() as $value) {
  if (!empty($value['value'])) {
    $values[] = $value['value'];
  }
}

or

$node = $variables['node'];
$values = array();
foreach ($node->field_name as $item) {
  if (!empty($item->value)) {
    $values[] = $item->value;
  }
}

 

setting the multi value field:

$node->field_name[LANGUAGE_NONE][] = new_value;

More shortcuts and ways to update multi-value field programmatically

$entity->field_name_muti->value = ['foo', 'bar', 'baz']; 
$entity->field_name_multi->target_id = [1, 2, 3]
$entity->field_name_multi->target_id = [$another_entity1, $another_entity2, $another_entity3]

 

this will also work:

$entity->field_name_muti = ['foo', 'bar', 'baz']; 
$entity->field_name_multi = [1, 2, 3]
$entity->field_name_multi = [$another_entity1, $another_entity2, $another_entity3]

 

Drupal Development

Drupal Development
Code Snippet
Read More
Working with multiple values
Programatically updating a node image field with multiple values.
Various ways of updating field values in Drupal 8 and 9
Tags
Drupal Support
Drupal 10
Multi-Value Field
Entity API
Code Example

The Freelancer Assistance

Drupal Training

drupal training session

Drupal Shared Space

Drupal Shared Space

Site Management

Site Management

Fullstack Service

Fullstack Service

Drupal Development

Drupal Development

Proactive Maintenance

Proactive Maintenance
Review All Services

Buy Hourly Support

 

Drupal platform support for complex websites. The service includes support, training, troubleshooting, fixes, updates, tool creation and module building.
* Minimum order is for 5 hours of support
* For every 20 hours of order you will be entitled to an additional hour of support
* For every 50 hours of order you will be entitled to 5 additional hours of support

 

>> Payment <<

 
cards

מופעל על-ידי paypal

Legal

  • Home
  • Contact
  • Products

Footer menu

  • Home
  • Contact
  • Products

Copyright © 2026 DrupalVIP project of Automatic Frameworks - All rights reserved

Developed and Maintain by Jonathan Ben Hur Freelancer