An Absolute Beginner's Guide: Using a Drupal Core Method in Your Own Code

I recently had a discussion with a colleague about the “right” way to utilize a drupal/core function (in this case, it was the the Random::word() method). This post is an absolute beginner’s guide to properly dependency injecting this method into your custom code! Let’s check it out.

What does Object Oriented Even Mean?

So in the not so distant past (especially in Drupal 7 and earlier) if you wanted to “use” code that lived “somewhere” you didn’t have a lot of choice but to replicate that code in your own code. This lead to a lot of reuse, which lead to a lot of unnecessary (and often broken) code.

Object Oriented Programming is all about reusing code and allowing you both easily pull in code from elsewhere in the application (meaning that you aren’t actually copying code, you’re using a single instance of it) and override code (so that if it does “most” of what you want already, you can just tweak a few things without having to replicate it whole hog). Pretty cool, huh?

Object-oriented programming has several advantages over procedural programming:

OOP is faster and easier to execute
OOP provides a clear structure for the programs
OOP helps to keep the PHP code DRY “Don’t Repeat Yourself”, and makes the code easier to maintain, modify and debug
OOP makes it possible to create full reusable applications with less code and shorter development time
— https://www.w3schools.com/php/php_oop_what_is.asp

What does in mean in this context?

In this case, my colleague is working to patch the Honeypot Drupal module so that it randomly assigns a title to the field. You can follow along with that issue's progress here: https://www.drupal.org/project/honeypot/issues/3154557. The problem? The Random::word method doesn't exist in the Honeypot code. It's core code! So our choice in this case is to:

  1. use drupal/core's Word method as is

  2. write an entirely new Word method that does our own thing

  3. replicate drupal/core's Word method

  4. override drupal/core's Word method to do what we want

Options 2/3 are usually not the "right" way to do it in an object oriented context. Why? It's replicating code! We want to use the code that drupal/core provides so that if it gets updated / changed we can take advantage of it. Option 4 in this context also isn't necessary (since we just need a random word, but not necessarily anything different than the core method provides). So here we should use option 1: just using core's method.

Using a Method

Disclaimer: I strongly recommend doing this next bit in an IDE.

I've not personally used this method before... so what I initially tried was a use statement and Random::method. Like so:

use Drupal\Component\Utility\Random;

function myFunction() {
  $rando = Random::word();
}

However, my IDE immediately warned me that this isn't the "right" way to use the method. See:

Screen Shot 2020-10-07 at 10.34.09 AM.png

So, instead of statically calling the method (that's just using Random::word()) I actually need to instantiate the Random class and THEN use the method. Like so:

use Drupal\Component\Utility\Random;

function myFunction() {
  $rando = new Random;
  $word = $rando->word();
}

Now, consider the difference in his patch where he didn't utilize the core function https://www.drupal.org/files/issues/2020-09-12/3154557-generate-random-name-option-3b.patch vs. the one where he did https://www.drupal.org/files/issues/2020-10-06/3154557-generate-random-name-option-3c.patch. The new patch is significantly smaller, has significantly less functionality to maintain (since the _honeypot_get_field_title is using the core Random::word method), AND this code will evolve over time as Random::word also evolves because he's using Random::word and not his own thing! Would the first patch work? Yes! it would! Is the second patch better? Also yes! Because it's better following object oriented standards and norms.

As with "most" things in Drupal, it's always best to prioritize core > contrib > custom. So if core provides something (in this case, a method for generating your randomized word) then you should totally use that instead of writing your own. Can you write your own? Probably. Is it going to be as good? Maybe. It it going to be a lot more work in the long run to write / maintain your own random word function than it would be to just use what comes out of the box? Definitely.

Why Use an IDE?

Like I mentioned previously, I've never used this method before. Even the example I posted that "works" is missing a parameter and will fatal error. How do I know? My IDE told me so!

Screen Shot 2020-10-07 at 10.41.59 AM.png

CAN you write object oriented code without an IDE? I mean sure. You can write it in a terminal if you want to... But why would you? Yes there's a steep learning curve with an IDE. Yes it's quite different from text editors and other tools that perhaps you have more experience with. But also yes, it will help you write better code, save you from blowing up a site (because, like me in this case, I didn't pass in a length parameter), and it will help you find all the things that you might not have known even existed. Use an IDE!

Special thanks to Karl Kaufmann for this topic.

Related Content