0

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
  • 3
    $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 Sep 30 '20 at 8:09
  • You can also do $field->get($field_id)->getString() – Beebee Sep 30 '20 at 10:45

Your Answer

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

Browse other questions tagged or ask your own question.