6

I need to implement hook_entity_bundle_info(), and I noticed that the documentation includes a link to Finalize API for creating, overriding, and altering code-defined bundle fields. In short, that issue says that is wrong to use the BaseFieldDefinition class because that class marks the field as a base field, which is not true, since the field is being added to a bundle.

Can the BaseFieldOverride class be used to implement hook_entity_bundle_info()?

Doing so, the example given for hook_entity_bundle_info() would become the following one.

function hook_entity_bundle_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
  // Add a property only to nodes of the 'article' bundle.
  if ($entity_type->id() == 'node' && $bundle == 'article') {
    $fields = [];
    $base_field_definition = BaseFieldDefinition::create('string')
      ->setLabel(t('More text'))
      ->setComputed(TRUE)
      ->setClass('\Drupal\mymodule\EntityComputedMoreText');
    $fields['mymodule_text_more'] = BaseFieldOverride::createFromBaseFieldDefinition($base_field_definition, $bundle);
    return $fields;
  }
}
1

According to the issue you linked the answer is no:

So, in theory, contrib modules can implement non-config-based bundle fields, but we don't provide an implementation class with which to do so.

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.