I have the following file named background_image_style.module

In my Drupal 8.3.3 site, paragraphs.module is at MySite/web/modules/contrib/paragraphs/paragraphs.module.

I installed the site and all contributed modules with Composer.

composer require drupal/paragraphs
vendor\bin\drupal module:install paragraphs

Where does my hook module go? (I just copied it in where the paragraphs.module is.) Are there any other overhead steps that needs to be done (e.g. editing other files)?

I tried several locations and cleared the cache each time.

     * Implements paragraphs().
    function background_image_style_preprocess_paragraph__banner(&$variables) {
      $paragraph = $variables['paragraph'];
      if (!$paragraph->field_image->isEmpty()) {
        $image = $paragraph->field_image->entity->url();
        $variables['attributes']['style'][] = 'background-image: url("' .                 $image. '");';
        $variables['attributes']['style'][] = 'background-size: cover;';
        $variables['attributes']['style'][] = 'background-position: center         center;';

From Where to Put the Module Files:

Manual import location

If you import a module 'manually', you should place your module in that same default recommended location, HERE:


[Themes: [d8-root]/themes/HERE]

It's a good idea to keep custom and contrib modules separate, hence Drupal Project putting contributed modules into their own folder; following on from that, there's a pretty common convention to place custom modules under [d8-root]/modules/custom/. But you don't have to, you can use whatever hierarchy you like under /modules, Drupal will pick up anything it finds recursively.

After it's in place, don't forget to enable the module, or it won't do anything.

| improve this answer | |

This preprocess hook can go right in the themename.theme file. In the theme, the name of the hook needs to be themename_preprocess_paragraph__banner (where themename is the name of your active theme).

Otherwise it would have to go in a custom module, and the hook name would be the module name instead of the theme name.

| improve this answer | |

The problem was the .yml file missing. A little different from d7 but more or less the same concept.

| improve this answer | |

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.