1

I am trying to add two fields to one of the node bundles.

I define the fields in this function:

function concertbeheer_entity_bundle_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle, array $base_field_definitions){
    if($entity_type->id()=="node" && $bundle == "concert")
    {
        $concert_fields['laatste_uitvoering'] = \Drupal\Core\Field\BaseFieldDefinition::create('datetime')
                ->setLabel('Laatste uitvoering')
                ->setRequired(TRUE)
                ->setDisplayConfigurable('view', TRUE)
                ->setDisplayConfigurable('form', FALSE)
                ->setDisplayOptions('view', array(
                    'label'=>'inline',
                    'type' => 'datetime_plain',
                    'weight' => '999',
                ));
        $concert_fields['eerste_uitvoering'] = \Drupal\Core\Field\BaseFieldDefinition::create('datetime')
                ->setLabel('Eerste uitvoering')
                ->setRequired(TRUE)
                ->setDisplayConfigurable('view', TRUE)
                ->setDisplayConfigurable('form', FALSE)
                ->setDisplayOptions('view', array(
                    'label'=>'inline',
                    'type' => 'datetime_plain',
                    'weight' => '999',
                ));

        return $concert_fields;

    }


}

but the documentation on https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_entity_bundle_field_info/8.2.x says I also need to provide a fieldStorageDefinition through hook_entity_field_storage_info()

I can actually configure the visibility of the fields but when I save something to them it isn't saved in DB. Probably because of the lack of a FieldStorageDefinition

But I don't know how to get started on that and, as usual, there seems to be no documentation available on how to do it

2

This code example would set up a basic storage definition:

function mymodule_entity_field_storage_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
  if ($entity_type->id() == 'node') {
    $definitions['my_bundle_field'] = \Drupal\Core\Field\BaseFieldDefinition::create('string')
      ->setName('my_bundle_field')
      ->setLabel(t('My new bundle field'))
      ->setTargetEntityTypeId($entity_type->id());
    return $definitions;
  }
}

You can look up examples in field.storage.*.yml files from a config export.

If you don't find the right examples there, you can configure a field in UI and then export the storage of this field to get a blueprint for the storage definition you need in this hook.

|improve this answer|||||
  • Very odd but I get an error that the class Drupal\entity_test\FieldStorageDefinition cannot be found. Seems like a bug in D8 if you ask me... – DragonEye Nov 12 '16 at 7:09
  • Yes, this seems to be an issue drupal.org/node/2280639. You can try to use BaseFieldDefinition, see the edited answer. – 4k4 Nov 12 '16 at 10:08
  • 1
    Why don't you install the bundle fields with yaml files? – 4k4 Nov 12 '16 at 10:10
  • Because probably he/she works on a module and not possible to upgrade a bundle with yaml? You are just allowed to install field when the module installed (or the field dependencies) but there's no way to do it without re-installation of the module, which is not acceptable. – golddragon007 Jun 14 '17 at 15:36
  • @golddragon007, this is about whether to install the field storage of a bundle field with a hook or a yaml file. The hook solution had some issues and is not used very often. That's the question about, why just don't use a yaml file. What you are refering to is a different topic, how to add fields to existing bundles after you have a storage. For this you need to modify the config entries of the existing bundle, view mode and form mode. – 4k4 Jun 14 '17 at 16:05

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.