4

I was out of Drupal for a few years, and PHP a couple years, due to having several years of jobs that didn't use Drupal between two that did. And I'm noticing a lot of functions that were reliable are now gone.

The particular case I am currently looking at is variable_get() occurrences replaced by \Drupal::state()->get().

However, I tried using it, and it's far from a drop-in replacement. In a class in a module I was working on, I tried changing $lm = variable_get('user_import_line_max'); to $line_max = \Drupal::state()->get('user_import_line_max'); but this complained that it can't be used inside a class (despite the fact the function could).

How do I use \Drupal methods in a class?

3
  • 1
    Welcome to Drupal Answers! This site is set up for answering one question at a time.
    – mona lisa
    Commented Apr 18, 2023 at 0:36
  • 1
    What does "this complained that it can't be used inside a class" mean, technically? Are you talking about the documentation which suggests it is an unsustainable practice, or, some kind of an error message?
    – mona lisa
    Commented Apr 18, 2023 at 0:39
  • @cilefen I've already started refactoring my code so I don't get the error anymore, but it was specifically an error when I first tried it, along the lines of "\Drupal::state()->get() cannot be used inside of a class".
    – lilHar
    Commented Apr 18, 2023 at 13:56

2 Answers 2

8

\Drupal::state() is a static helper function intended for use in procedural code. It returns the singleton "State" object (called a "service") from the service container. \Drupal::state()->get() is a chained method call that then calls the get method on the state service.

In object-oriented code the advice is to use dependency injection patterns, the most common type of which is constructor injection, to obtain service container objects. A strong argument for using dependency injection is that it facilitates unit testing your classes. The Drupal API documentation I linked to in the above paragraph explains using the service container in some detail.

As a sample you could look at core's DbUpdateController.php. There you can see how the State service is first injected:

And how it later is used inside the controller:

1
  • 2
    The added sample is a more complex DI pattern using the factory method create(). See en.wikipedia.org/wiki/Factory_method_pattern. In Drupal this is only necessary for controllers or plugins, which are not registered as service in the container.
    – 4uk4
    Commented Apr 18, 2023 at 10:02
4

To add to Cliefen's very good answer, you can view the documentation for the \Drupal class here: https://api.drupal.org/api/drupal/core%21lib%21Drupal.php/class/Drupal/10

It provides a list of all functions that can be called statically off the class.

4
  • One thing I'm still trying to figure out is what that leading slash does in \Drupal, as I haven't really seen that before. Is it literally just part of the name, like the dollar sign in JQuery (which I loath due to it interfering with me writing PHP code) or is it actually doing something?
    – lilHar
    Commented Apr 18, 2023 at 13:58
  • 2
    This is about namespaces. See php.net/manual/en/language.namespaces.rules.php. \Drupal is a fully-qualified class name while Drupal is not. The latter works only if you add use Drupal; to a class or in procedural code.
    – 4uk4
    Commented Apr 18, 2023 at 14:42
  • 1
    The back slash at the start indicates that the class is in the global namespace. So \Drupal is in the global namespace. An example module for example could declare it's own class named Drupal in a separate namespace, such as \Drupal\example\Drupal, and these would not conflict, as they are in different namespaces.
    – Jaypan
    Commented Apr 18, 2023 at 15:16
  • Ah, so that's why I never came across it before, as I tend to avoid coding with globals and Drupal didn't use to use it. Thanks for the clarification!
    – lilHar
    Commented Apr 19, 2023 at 14:56

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.