-1

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?

5
  • mikemadison.net/blog/2019/12/31/… - but overall I agree with @Patrich Kenny, the answer to this question basically is a good book :-)
    – Hudri
    Commented May 21, 2023 at 13:06
  • Those missing bits that you're talking about its all OOP which you're expected to know for D8+
    – No Sssweat
    Commented May 24, 2023 at 23:46
  • @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. Commented May 25, 2023 at 4:23
  • @FreeRadical If you're creating a custom controller, you're almost expected to extend the ControllerBase class as per documentation and you can look further more into it 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.
    – No Sssweat
    Commented May 25, 2023 at 5:33
  • @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? Commented May 25, 2023 at 8:07

1 Answer 1

2

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.

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.