3

I was wondering what is the correct alternative of .inc files in Drupal 8? That is to say, if I want to create some functions that should be only be loaded when required, like it was with module_load_include() in D7. Though this function is still there in D8, I hear that this is not the best procedure. Is that so?

I have read somewhere, that using PHP traits is considered a better practice here. However, not sure if useing a trait inside a .module file is a good approach.

Any help?

1
  • I like laymannx's post below. You should really read up on traits, then if you want to look at one that is easy to get the idea of in Drupal 8 (since it's already included by most base classes) is StringTranslationTrait. One thing to realize is that your mindset of using .inc files ends up not being so relevant in D8 module architecture. And Traits are handy, but not always the right solution. It really depends on what your .inc file is for. Often, functions developers put in D6 and D7 .inc files are best created in D8 as a service, so that it can be called with the services API.
    – Jaypan
    Commented Jun 26, 2018 at 12:39

2 Answers 2

6

Actually there seems to be nothing wrong about using *.inc files in D8. Have a look at the Update core module. Quite some *.inc files present: cgit.drupalcode.org.


Nevertheless I tend to follow Symfony's best practice examples when it comes to stuff like that.

[Symfony Docs] Organizing Your Business Logic

[Stack Overflow] Where to put model helper functions in Symfony2

Means you'll add a new class under src/Utils. For example:

src/Utils/HelperFunctions.php
src/Utils/FooBar.php

And then wherever you need it, you reference it:

use Drupal\MYMODULE\Utils\HelperFunction;
use Drupal\MYMODULE\Utils\FooBar;

And then you'd call a certain function like so:

$site_color = HelperFunctions::getSiteColor();
$foo_bar    = FooBar::returnFooBar();

And when I'm still unsure, I'll search inside the core and contrib modules how they did it.

3
  • Thanks. Understood. BTW, so you would still use a class and not a trait then? Commented Jun 26, 2018 at 10:32
  • @PratipGhosh – Yupp, class :)
    – leymannx
    Commented Jun 26, 2018 at 10:34
  • 3
    traits are just behaviors in the form of functions that are attached to classes when needed, like StringTranslationTrait. It’s not the same as a .inc file. They cannot be used on their own or loaded and used in procedural code.
    – Kevin
    Commented Jun 26, 2018 at 11:08
3

In reference to the other answer, the main reasons those .inc files still exist in core is that was just not possible to rewrite everything into classes/services, which is definitely the right way to do it in Drupal 8.

Part by part, more and more functions are being converted to services in Drupal 8 and deprecated in favor of them. Instead of static methods, that's what you should do.

There are plenty of tutorials and documentation on how to create those services and use them, see https://www.drupal.org/docs/8/api/services-and-dependency-injection/services-and-dependency-injection-in-drupal-8 for example.

trait can be used when sharing common code between multiple classes that doesn't need to be exposed as it's own service. It's basically just a built-in copy & paste and similar to base classes but more flexible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.