Score:-1

How to learn the context for using methods provided by the Drupal API

kz flag

I am trying to use the code example provided by this accepted answer – and failing. In this particular case it seems that it is necessary to know two things in order to successfully use this code, that is not included in the answer:

  • The parent class to declare for my class in order to make the method provided by Drupal available.
  • The "use" statement(s) required that make my class load this parent class.

I see a lot of these "partial" answers in this forum and elsewhere. The assumption seems to be that developers just "know" what missing bits to add to make the code examples code work correctly.

However, due to my inexperience, I am unable to figure this out by myself. Can someone capable of doing this tell me how you do it?

ru flag
https://mikemadison.net/blog/2019/12/31/dependency-injection-in-php-and-drupal-8 - but overall I agree with @Patrich Kenny, the answer to this question basically is a good book :-)
No Sssweat avatar
ua flag
Those missing bits that you're talking about its all OOP which you're expected to know for D8+
Free Radical avatar
kz flag
@NoSssweat, I think I understand the principles of OOP. However, doing so does not tell me *what* parent class to extend to get access to the needed method. If you look at the documentation page for the `entityTypeManager`, there is no hint there that the parent class should be `ControllerBase`.
No Sssweat avatar
ua flag
@FreeRadical If you're creating a custom controller, you're almost expected to extend the ControllerBase class as per [documentation](https://www.drupal.org/docs/drupal-apis/routing-system/introductory-drupal-routes-and-controllers-example) and you can [look further more into it](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Controller%21ControllerBase.php/class/ControllerBase/8.2.x) and see that it's not recommended to extend if you plan on doing unit testing. In addition, you can always [search the code base for it](https://i.stack.imgur.com/cFfUv.png).
Free Radical avatar
kz flag
@NoSssweat That is the type of information I am asking for. Your screenshot shows the result of a search in the code base for a specific method. How and where do one search the code base to see the places it is available from?
Score:2
cn flag

I was very lost in the transition from D7 to 8+. I was one of those people who "learned Drupal" (from 5 to 7), and so when version 8 switched to OOP, a lot of my Drupal knowledge no longer applied, and I didn't have a deep background in PHP to help me out.

If you're in a similar situation, the book Drupal 8/9/10 Module Development by Daniel Sipos helped me to see both at a high and low level how things are done now. I would consider myself a hobbyist programmer and I own every book Amazon sells with Drupal in the title, and this is the one that has helped me the most in just understanding what the heck Drupal is doing, particularly all the "new" (as of D8) classes and services stuff.

The Drupalize.me screencasts are also very good, although they cost significantly more than a dev book.

Another thing that can help a lot is having an IDE set up properly (code completion for Drupal classes, automatic checks for Drupal coding standards, maybe PHPStan). I use PHPStorm which costs a little money but prevents a huge number of mistakes. For example, if you paste a code example from this site in there without using the class, the Problems pane will warn you that the class is unknown, and you can right-click the class name to import it automatically.

VSCode is also good and free but maybe a bit harder to configure (it was harder for me anyway, although that could be because I'm used to PHPStorm). Of course, if you're like me, setting up the IDE is also a learning experience, but there are docs on Drupal.org and if you choose PHPStorm you get support from real engineers with your subscription.

Also, using a local dev environment like ddev or lando with the IDE will really raise your skill level. Specifically, you need to have xdebug available in your dev environment and know how to use it. With Drupal 7, most of them I was able to get by without a debugger because I could follow the code as the steps occurred more or less in order. With D8+'s OOP, that's impossible; you have to have a debugger.

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.