Provide a footer to a micro site with Drupal 8

A factory

The Micro Site module allows you to set up a Drupal web factory, from a single Drupal 8 instance, to power a multitude of sites, of a different nature if necessary. Micro Site provides a new content entity that allows you to customize these different site types at will, using the APIs available on Drupal 8, and modify their behavior according to business needs. Let's detail how we can proceed, through a basic example of providing a modular footer on different powered sites, and display it on all the pages that can make up the site.

This operation can be summed up as a little bit of site building, by adding and configuring a few fields, and a little bit of theming by adapting the general template of the pages of a site to be able to display this footer.

Site building

Add field on site type

For example, on a site type called Generic we can create a new Paragraphs field called Site Footer.

field site footer

Then we can fill in the footer of the page when editing the microsite, from a certain number of configured paragraphs (links type to add a list of links, or text type to put free text, or why not a paragraph to load a registration form, etc.).

Footer exemple

In this example we use paragraphs to feed the footer of the micro site, to allow a great modularity for each micro site to customize its footer according to its needs. But we could just as easily have used simpler, more constrained fields to limit the possibilities that could be filled in the footer. It is here to assess the business needs of each type of site that we want to set up.

Theming

We are not going to announce a great innovation, or a thundering trick, for the future. Once the field (or fields) created that will feed the footer of the micro site, all that remains is to display it at the template level page.html.twig, or on a surcharge specific to site: page--site.html.twig or a surcharge more specific to the site type, for example page--site--generic.html.twig.

First, we implement a prepocess function on the site pages to provide the template with the site_footer variable.

/**
 * Implements hook_preprocess_HOOK().
 *
 * Provide site variables to override footer, menu in the page template
 * dedicated to site entities.
 */
function micro_drupal_preprocess_page(&$variables) {
  $negotiator = \Drupal::service('micro_site.negotiator');
  /** @var \Drupal\micro_site\Entity\SiteInterface $site */
  $site = $negotiator->getActiveSite();
  if ($site instanceof SiteInterface) {
    if ($site->hasField(MicroDrupalConstant::SITE_FOOTER)) {
      if (!$site->get(MicroDrupalConstant::SITE_FOOTER)->isEmpty()) {
        $viewBuilder = \Drupal::entityTypeManager()->getViewBuilder('site');
        $field_footer = $site->get(MicroDrupalConstant::SITE_FOOTER);
        $site_footer = $viewBuilder->viewField($field_footer, 'footer');
        $variables['page']['site_footer'] = $site_footer;
      }
    }
  }
}

Then at the template level page--site.html.twig, we display this new variable.

{% if page.site_footer %}
  <!-- #site-footer -->
  <footer id="site-footer" class="clearfix site-footer-wrapper">
    <div class="site-footer-wrapper-inside clearfix">
      <div class="container">
        <div class="row">
          <div class="col-xs-12">
            <div class="site-footer">
              {{ page.site_footer }}
            </div>
          </div>
        </div>
      </div>
    </div>
  </footer>
  <!-- EOF: #site-footer -->
{% endif %}

And it's over. The rest is just a little bit of styling with some magical CSS rules.

What about a sidebar?

Using the Drupal 8 block positioning system, we can place as many blocks as necessary in a sidebar region, and control visibility by site, or by site type, as needed. But this configuration must then be done at the administrator level of the Drupal Master instance and not at the level of each micro site.

Following the same logic, we can very well set up dedicated fields (modular or not) to feed a sidebar present on each micro site, and then allow this time the administration and management, at each level of a micro site, of the content of these sidebars and control their visibility at this level. The implementation will be slightly more complex, to give full control to an administrator of a micro site on the visibility rules of the created content, but the basic principle remains the same: a touch of site building and a zest of theming to customize as much as necessary a micro site.

 

Ajouter un commentaire