logo

Palette - Make it Colorful🎨

Palette - Visual Page Builder, no design degree required.

Live Demo Download Palette

Scroll

How to get value of multiple values field in Drupal?

22/02/2025, by Anonymous (not verified)
Permalink

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']] ;
  }