1

In my module i programmatically add user fields for the website owner instead of instructing them to do so. Here's an example of the code i used which runs in mymodule.install:

function openid_connect_entity_base_field_info(EntityTypeInterface $entity_type) {


if ($entity_type->id() === 'user') {
    $fields['field_birthdate'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Birthdate'))
      ->setDescription(t('Add a description'))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE)
      ->setDisplayOptions('form', array(
        'type' => 'string_text_field',
        'weight' => 30,
        'default_value' => 0,
      ))
      ->setDisplayConfigurable('form', TRUE);
  }

return fields;
}

but if you add a field manually all the programmatically added fields will be deleted as shown:

programmatically added fields before adding one manually

programmatically added fields before adding one manually

Adding a field manually

enter image description here

And boom all the programmatically fields are gone!

enter image description here

If this is a bug in the drupal 8 framework where do i report this?

7
  • Where are you running that code?
    – Clive
    Jan 4, 2017 at 12:04
  • @Clive in mymodule.install
    – Callum
    Jan 4, 2017 at 12:04
  • I meant function...hook_install probably? How are you actually attaching the fields to the entity type? It'd be useful to see that code
    – Clive
    Jan 4, 2017 at 12:12
  • 1
    I imagine just moving the hook to the .module file instead of .install will fix it. It's not an installation hook
    – Clive
    Jan 4, 2017 at 12:22
  • 1
    huzzah! it works, add your suggestion as an answer
    – Callum
    Jan 4, 2017 at 12:51

1 Answer 1

1

You just need to move the hook implementation into the .module file.

It works on install in the .install file because that file is included in the request, and when Drupal looks for hook implementations, it can find it.

Wen you're not installing, e.g. when adding fields through the UI, the .install file is not included so the hook won't be found, and Drupal will consolidate the info it has by removing those fields.

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.