1

I looked around, but am not exactly sure how to ask this question.

Is there a means of not erroring out on "function getValue() on null". Basically I'd like to return nothing and move on if getValue is being called on something that doesn't exist.

0

1 Answer 1

3

You can check if a field is empty or not before asking for getValue() with:

if (!$entity->get('field_name')->isEmpty()) {
  $value = $entity->get('field_name')->getValue();
}
3
  • Is there a way to do this within an array so something like this. Node::create([ 'field_supervisor_uid' => $user->get('field_supervisor')->first()->getValue()['target_id'],])->save(); Apr 3, 2018 at 20:04
  • Nevermind dumb question. Just handled it as a variable and then passed it in... Apr 3, 2018 at 20:19
  • The problem is ->first()->getValue() is going to error because you're accessing something that does not exist
    – Kevin
    Apr 3, 2018 at 20:19

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.