0

i create a custom Entity,

CustomEntity.php

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);

    $fields['name'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Name'))
      ->setDescription(t('Name'))
      ->setSettings([
        'max_length' => 50,
        'text_processing' => 0,
      ])
      ->setDefaultValue('')
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'string',
        'weight' => -4,
      ])
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => -4,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);



    return $fields;
  }

We can add fields programmatically here for the Main entity and then all bundles ll automatically inherited fields from mother.

So i set a bundle in myModule/config/install:

myModule.customEntity.bundleA.yml

uuid: db129d12-849a-4e68-bb56-d787b4b0cf42
langcode: en
status: true
dependencies: {  }
id: bundleA
label: bundleA

I am looking for add fields programmatically to that bundle .

So i create with the admin interface all fields and have export all config files into my install folder. But it's a lot of files: storage / view / field / form.

Is there another way like the main entity for example ?

0

The amount of config files should not really be the problem.

But if you want programatically then there are three hooks here that matter:

  1. hook_entity_base_field_info is the same as baseFieldDefinitions just in a hook.
  2. hook_entity_bundle_field_info allows defining fields per bundle but you need to define (this is the third hook) how they are stored.

Finally, your entity class might want to use bundleFieldDefinitions to specify per bundle fields.

It's not quite clear what you'd like to do so I can't provide more than just a few API pointers.

  • For 2 fields its ok to add some yml files, but when you need 5 or 10 fields its crazy the number of yml required for install the module. I ll take a look on your comment hook – Kevin Jun 24 '17 at 14:46

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.