With D8, the appropriate method for adding fields during install with a custom module is with the config system and .yml files in a config/install directory.

I have a module for user theming I like to use for new D8 site installs, it adds hooks, templates, fields.

With a minimal install, this module provides the field and storage config for the user.user.user_picture field, as well as the entity_form_display and entity_view_display for compact and token view modes for the user.

I also include some custom fields such as first_name and last_name.

Obviously, with a standard install, this module will get the errors that the user.user.user_picture config already exists among others.

I would really prefer that the config overwrites the display config for the view modes, but would take them just being ignored on install if it already exists.

Is there a method following the Drupal config system to get conditional, or overriding, (synchronized) config during install? Or do I need to use the features module?

Is there a definitive approach for this?

  • 1
    What you might be able to do is add a hook_install() in your .module and then load the relevant config entity that you want to change e.g. $storage = \Drupal::entityTypeManager()->getStorage('entity_form_display'); $view_display = $storage->load('user.user.default'); then you can alter the settings within that and then $view_display->save(); to save. As a way to get round the error – Leigh Mar 1 at 16:40
  • A worthwhile approach, wondering if I shouldn't also do hook_schema() and add any fields to the user table via \Drupal::database()->insert... Unless I am missing a method to call config .yml files in hook_install()? – Prestosaurus Mar 3 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.