Score:4

How do I use \Drupal methods in a class?

ve flag

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?

id flag
Welcome to Drupal Answers! This site is set up for answering one question at a time.
id flag
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?
lilHar avatar
ve flag
@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".
Score:8
id flag

\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:

4uk4 avatar
cn flag
The added sample is a more complex DI pattern using the factory method create(). See https://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.
Score:4
de flag

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.

lilHar avatar
ve flag
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?
4uk4 avatar
cn flag
This is about namespaces. See https://www.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.
Jaypan avatar
de flag
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.
lilHar avatar
ve flag
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!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.