0

I am using Drupal 8 for a project for learning purposes. In my case I want to access to a field which contains multiple values, but I can't find how to do that. For example for a single value field I can access it like this:

kint($variables['node']->field_name->value);

But how can I get all the values of field which allows more than one value?

  • contain more than user you mean more than 1 value? – No Sssweat Mar 3 at 12:38
  • yeep that's it ::) – drupal love Mar 3 at 13:09
2

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;
  }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.