function FieldBlock::entityFieldHasDefaultValue

Same name in other branches
  1. 11.x core/modules/layout_builder/src/Plugin/Block/FieldBlock.php \Drupal\layout_builder\Plugin\Block\FieldBlock::entityFieldHasDefaultValue()

Checks whether there is a default value set on the field.

Return value

bool TRUE if default value set, FALSE otherwise.

1 call to FieldBlock::entityFieldHasDefaultValue()
FieldBlock::blockAccess in core/modules/layout_builder/src/Plugin/Block/FieldBlock.php
Indicates whether the block should be shown.

File

core/modules/layout_builder/src/Plugin/Block/FieldBlock.php, line 418

Class

FieldBlock
Provides a block that renders a field from an entity.

Namespace

Drupal\layout_builder\Plugin\Block

Code

protected function entityFieldHasDefaultValue() : bool {
    $entity = $this->getEntity();
    $field = $entity->get($this->fieldName);
    $definition = $field->getFieldDefinition();
    if ($definition->getDefaultValue($entity)) {
        return TRUE;
    }
    // @todo Remove special handling of image fields after
    // https://www.drupal.org/project/drupal/issues/3005528.
    if ($definition->getType() !== 'image') {
        return FALSE;
    }
    $default_image = $definition->getSetting('default_image');
    // If we are dealing with a configurable field, look in both instance-level
    // and field-level settings.
    if (empty($default_image['uuid']) && $definition instanceof FieldConfigInterface) {
        $default_image = $definition->getFieldStorageDefinition()
            ->getSetting('default_image');
    }
    return !empty($default_image['uuid']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.