An optional argument $exception_on_invalid of type boolean has been added to Drupal\Core\Entity\FieldableEntityInterface. The value of $exception_on_invalid defaults to TRUE, which keeps consistent with existing functionality that an exception is thrown on an invalid field name. If $exception_on_invalid is explicitly set to FALSE, then get() will return NULL instead of throwing an exception.
In addition, the method signature in the interface will change to include the $exception_on_invalid argument, as well as a string typehint to the $field_name parameter.
The default behavior will change in Drupal 13.0.0, where $exception_on_invalid will have a default value of FALSE, so calling get() with an invalid field name and without a second argument value will return NULL.
Before
try {
$value = $entity->get('field_might_not_exist')->value;
}
catch (\InvalidArgumentException) {
$value = NULL;
}
OR
$value = $entity->hasField('field_might_not_exist') ? $entity->get('field_might_not_exist')->value : NULL
After
$value = $entity->get('field_might_not_exist', FALSE)?->value;
Future (Drupal 13)
$value = $entity->get('field_might_not_exist')?->value;
OR
try {
$value = $entity->get('field_might_not_exist', TRUE)->value;
}
catch (\InvalidArgumentException) {
$value = NULL;
}