58
Rapid Module Development Drupaldelphia, April 27, 2018

Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Rapid Module DevelopmentDrupaldelphia, April 27, 2018

Page 2: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Eastern Standard

● Philadelphia-based marketing and

technology agency

● Collaborative dev team

● We’re hiring!

Intros

Tom Mount

● Technology Lead, Eastern Standard

● Closet geek

● Hobbies include bass guitar and rec

football year-round

● Email: [email protected]

● Drupal: https://www.drupal.org/u/tmountjr

Page 3: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Cliche alert: “Work smarter, not harder.”

Page 4: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

The Not-So-Rapid Setup

Page 5: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Tools: What you probably already have

● A CLI/terminal/command prompt● A Drupal 8 project, one that’s either set up somewhere already, or at least the zipped version of Drupal Core 8.5.72.

Page 6: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Tools: What you should download

● VSCode (https://code.visualstudio.com/), available for Windows, OS X, and Linux (.rpm and .deb), along with some plugins:● PHP Debug (https://github.com/felixfbecker/vscode-php-debug)● EditorConfig (https://github.com/editorconfig/editorconfig-vscode)● PHP DocBlocker (https://github.com/neild3r/vscode-php-docblocker)● PHP CS (https://github.com/ikappas/vscode-phpcs)● PHP CS Fixer (https://github.com/junstyle/vscode-php-cs-fixer)

● Drupal Console (https://drupalconsole.com/)● Docker (https://www.docker.com/)● Lando (https://docs.devwithlando.io/)

Note that the only plugin we’ll definitely be using here is PHP Debug. The others are nice to have and help you write beautiful and standards-compliant code quickly, but it probably won’t speed up your actual code writing.

Page 7: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Chances are good you already have these installed and running. But in case you don’t:

● Git● Composer● Drush (https://www.drush.org/)

● Drush 8 may already be installed globally (http://docs.drush.org/en/8.x/install/)● Drush 9 may already be installed in the project and can be accessed with Drush Launcher

(https://github.com/drush-ops/drush-launcher)

Tools: What you might need to download, but probably not.

Page 8: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Tools: The One-Time Setup

To follow along, you’ll need to do the following:

● Install VSCode and all the extensions.● Install Docker.● Install Lando.● Install Drupal Console Launcher.

Page 9: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Now can we get to developing?(It’s been like two hours already!)

Page 10: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Your Module: What will it do?

Having a clear idea of what you want to accomplish with your custom module will save you time later.

The Drupal module ecosystem is pretty big; maybe someone has already done what you want to do? Spending 30 minutes poking around https://drupal.org is definitely faster than spending six hours writing a custom module that someone else has already written.

Efficiency isn’t necessarily about doing things quickly, it’s about being smart in your approach.

Page 11: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Your Module: What will it do?

Let’s create a very custom module:

● Should store its configuration the way other modules store Drupal configuration.● Should include a form to edit those config values.● Should include a menu link to that form.● Should include some level of permission control that site owners can set if they so desire.

In most cases, your custom module will have a lot more functionality, but this is fine for the purposes of a demo.

Page 12: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Step 0: Set up a new/existing local Drupal site.

Page 13: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Set up a new/existing local Drupal site

Page 14: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Set up a new/existing local Drupal site

Page 15: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Step 1: Create your module.

Page 16: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Create your module

Page 17: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

(Optional) Step 1.5: Move the module.

Page 18: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Move the module

Page 19: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Step 2: Enable the module.

Page 20: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Enable the module

Page 21: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Step 3: Generate a config file and form.

Page 23: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Generate a config file and form

Page 24: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Step 4: Verify config is being saved.

Page 25: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Verify config is being saved

Page 26: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

The end! …?

Page 27: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Where to go from here

We could stop here. We have a config form, and several hundred lines of code that we didn’t have to write that handle routing, configuration, and menus.

But the form asks for an API URL.

And it happily accepts “hello world.”

Let’s fix that.

Page 28: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Step 5: Add validation.

Page 29: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Validation, part 1: making sure XDebug works

XDebug is a fantastic PHP debugger. You can step through your code line by line, jump in and out of methods, get detailed stack traces, set conditional breakpoints...everything you need to inspect what’s happening in your module under the surface.

It’s fairly easy to set up in VSCode.

Page 30: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Validation, part 1: making sure XDebug works

Page 31: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Validation, part 1: making sure XDebug works

Page 32: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Validation, part 1: making sure XDebug works

Page 33: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Validation, part 1: making sure XDebug works

Page 34: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Validation, part 2: figuring out how to validate

Back in the day, everyone had their own regular expression for URLs, email addresses, etc.

They all had problems.

Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page.

The method looks easy enough to figure out how it works, but let’s test it anyway with another great tool: the Drupal PHP REPL.

Page 35: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Validation, part 2: figuring out how to validate

Page 36: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Validation, part 3: actually validating

Page 37: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Validation, part 3: actually validating

Page 38: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Step 6: Add permissions.

Page 39: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Add permissions

Page 40: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Add permissions

Page 41: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Add permissions

Page 42: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Add permissions

Page 43: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Test permissions

Page 44: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Test permissions

Page 45: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Test permissions

Page 46: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Test permissions

Page 47: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Recap

Page 48: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

How is this faster?

Instead of…...setting up on Pantheon or Acquia, committing code, testing, committing more code, doing more testing...

...we can…

...use Lando as a local development environment.

Page 49: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

How is this faster?

Instead of…...reading the Form API and menu docs and writing a few hundred lines of code by hand, then reading a bunch of blogs about how to set up new configuration files and writing a few dozen more lines of code by hand...

...we can…

...use the Drupal Console to generate a brand new module, set up a new config file, create a configuration editing form, and set up permissions.

Page 50: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

How is this faster?

Instead of…...clicking around in the admin interface for a while to create roles and a new user...

...we can…

...use the Drupal Console to stub out a new user role and a new user, right from the command line.

Page 51: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

How is this faster?

Instead of…...choosing a password, writing it down or putting it in a password manager, and cluttering up your brain...

...we can…

...use Drush and a private browsing window to log in as that new user without worrying what the password was.

Page 52: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

How is this faster?

Instead of…...dropping a bunch of var_dump(); die(); statements everywhere, or, if you want to get really elegant, installing the debug module and using kint() everywhere, both of which require reloading the page every time you change anything…

(What you’re looking for wasn’t in the variable you kint()ed? Guess you gotta reload and try again!)

...we can…

...use a free and powerful IDE, along with a proper debugging tool, to quickly inspect variables while our code was running, letting us get some insight into Drupal was doing “under the hood.”

Page 53: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

How is this faster?

Instead of…...doing something silly like making a custom route that exists solely to run one statement and print the output to the screen (which I’ve done) or blindly hammering away at a method until you find the right variable to pass...

...we can…

...use Drush’s PHP REPL to quickly test out some inputs against a Drupal utility method without having to guess or go the long way around.

Page 54: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Further Reading

Page 55: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Further Reading

● XDebug: https://deliciousbrains.com/xdebug-advanced-php-debugging/● Docksal, similar to Lando, might be easier if you already know Docker pretty well: https://docksal.io/● All the Drupal Console commands: https://docs.drupalconsole.com/en/commands/available-commands.html● All the Drush 8 commands: https://drushcommands.com/

Page 56: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Conclusion

Page 57: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy

Questions

Page 58: Rapid Module Development - Drupaldelphia · Luckily, a quick Google search (“drupal 8 valid url”) landed me on the UrlHelper::isValid() documentation page. The method looks easy