Scroll
How to get value of multiple values field in Drupal 8?
How to get value of multiple values field in Drupal 8?
How to get value of multiple values field in Drupal 8?
1 answer
votes: 1369
Answer
Drupal fields are represented by the ItemList
class (whether they're multi-valued or not). The convenience method getValue()
iterates through each item's getValue()
method to build an array representation of the data value.
$manager=\Drupal::entityManager();
$field_definition = $manager->getFieldDefinitions("node","room")["field_facilities"];
$facilities=$field_definition->getFieldStorageDefinition()->toArray()["settings"]["allowed_values"];
$details=array();
foreach ($nodes as $node) {
$facs=$node->get("field_facilities")->getValue();
foreach ($facs as $key => $f)
$details[]= $facilities[$f['value']] ;
}