0

I have a field on a node which has a default value set, I need to access the default value of that field. But, I don't want to load any nodes of that type and access the field definitions, nor do I want to manually write a db_query and fetch the value directly from the db, I know there is a proper way to do it I just don't know of it.

Drupal 8.6.x

2

Use FieldDefinitionInterface::getDefaultValueLiteral:

$definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', 'page');
if (isset($definitions['field_example'])) {
  $default_value = $definitions['field_example']->getDefaultValueLiteral();
}
0

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.