1

Am trying to get the value of a Boolean field from a node entity, but it returns null whether the field is checked or not checked

$field_id = 'field_test_boolean';
/** @var Node $entity */
$entity = \Drupal::entityTypeManager()
  ->getStorage('node')
  ->load(220);

dump($entity->get($field_id)->get(0));

Not sure what am doing wrong here!!

2
  • 4
    $entity->get($field_id)->get(0)->getValue()['value'] or $entity->$field_id->value; You should see a 1 which means it's checked and 0 for unchecked. If you don't, then either the node ID is wrong or your field name is wrong.
    – No Sssweat
    Commented Sep 30, 2020 at 8:09
  • You can also do $field->get($field_id)->getString()
    – Beebee
    Commented Sep 30, 2020 at 10:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.