Skip to main content
Drupal: sharing configuration between multiple sites

Developing a website is a difficult and repetitive task. Most websites have common information such as teasers, page templates, sections, profiles, compact profiles, and dashboards.

We will save a significant amount of time if we can import most configurations, either during development or during the installation process of Drupal or a module.

The Drupal team has looked into these issues and provides us with basic tools to solve them. The module is called config and is part of the core; it is installed by default in the standard profile. With this module, we can export configurations and import them later. 

In order to better understand how it works, let's define a work situation:

Scenario :

We have the following layout:

which contains 3 regions

  • un region pour le menu
  • a region for the call-to-action button
  • a region for profile information.

Profile information appears on hover:

We want to have this profile configuration when installing the module.

We create the layout : hot_models/src/Plugin/Layout/Sections/Menus/HotModelsHotlockMenu.php

Au niveau interface, nous creons un rendu specifique 'hot_models_hotlock_menu__user' and we load this rendering if the user is logged in:

    /**
     * The user's compact rendering is added if they are logged in.
     */
    $uid = \Drupal::currentUser()->id();
    if ($uid) {
      $user = \Drupal\user\Entity\User::load($uid);
      $viewEntity = \Drupal::entityTypeManager()->getViewBuilder('user');
      $build['user_compact'] = $viewEntity->view($user, 'hot_models_hotlock_menu__user');
    }

On a basic Drupal installation, the ‘hot_models_hotlock_menu__user’ mode does not exist, so it would need to be created during installation and the necessary components added.

To achieve this, we must export the display model.'hot_models_hotlock_menu__user':

The name of this configuration is available at the bottom of the export page. We need to create a file with this name and add the configuration content to it:

In this case, the file is named : core.entity_view_display.user.user.hot_models_hotlock_menu__user.yml

The file must be in  hot_models/config/install

There is an important section in the configuration content. : "dependencies:" which generally contains two entries, config: which contains the configurations that are mandatory and "module:" which contains the required modules.

In our case, we have:

...
dependencies:
  config:
    - core.entity_view_mode.user.hot_models_hotlock_menu__user
    - field.field.user.user.user_picture
    - image.style.thumbnail
  module:
    - hot_models
    - image
    - layout_builder
    - user
...

On that point, we must import. 'core.entity_view_mode.user.hot_models_hotlock_menu__user' because the latter does not exist on Drupal, but the rest exists on a standard Drupal installation.

The next configuration : (core.entity_view_mode.user.hot_models_hotlock_menu__user.yml )

uuid: 6fa85a79-970e-4d1c-9b29-9a3ab6eab2e4
langcode: fr
status: true
dependencies:
  module:
    - user
id: user.hot_models_hotlock_menu__user
label: 'User model flat (hot_models module)'
targetEntityType: user
cache: true

NB: Since this configuration is linked to the module, it must be deleted when the module is uninstalled.  enforced at the level of the key dependencies :

...
status: true
dependencies:
  config:
    - core.entity_view_mode.user.hot_models_hotlock_menu__user
    - field.field.user.user.user_picture
    - image.style.thumbnail
  module:
    - hot_models
    - image
    - layout_builder
    - user
  enforced:
    module:
      - hot_models
third_party_settings:
  layout_builder:
...

And there you have it, when you install your module, you will have this block that will be automatically configured.

Ressources :

Profile picture for user admin Stephane K

Écrit le

Il y'a 2 years
Modifié
Il y'a 2 weeks
Loading ...
WhatsApp
Support Habeuk : +237 694 900 622
WhatsApp Send