Seven tips and traps that make D7 developers go :-) and @#$&*!

When a steam train like D8 rolls your way, resistance is futile. You either jump aside --and some "big" names have done so-- or you jump aboard.

For D7 developers wanting to join the D8 ride, this article critiques some common D8 misconceptions, hopefully making onboarding less precarious.

1. "I need to learn a lot about Symfony."

FALSE ! Unless you work on core you need to learn just as much about HTTP Kernel and Symfony CMF, as you learned about the internals of D7's core menu system, i.e. nothing.

And isn't that little, easy-to-find and easy-to-read .routing.yml file an improvement over the now much leaner hook_menu() ? So stop whinging.

Oh, and if it makes you feel better... a lot of code perceived to be Symfony, is in fact home grown core D8.

D8: +1







2. "I'm using my debugger to step through all this container code, trying to figure out how dependency injection is implemented and I'm getting frustrated and don't understand."

Stop doing that! Just accept that when you put your class in the right spot, it will be sucked into the system automagically and leave it at that. Ride the tiger, don't fight the tiger. You didn't need to know how a Views filter handler in D7 got "injected" either, but that didn't stop you writing one.

D8: +1

3. "My hooks are dead and gone and I need to put my implementations into classes."

Yeah, so what? "Opgeruimd staat netjes!"*) is a Dutch saying that is harder to translate into English than copying your functions from .module and .inc files and pasting them into a .php file that resides in a well-organised auto-findable class file structure. Then put class … extends … {… } around the contents and adjust the method signatures where necessary.

Get over it and get coding. Start here: D7 to D8 upgrade: fields, widgets and formatters.

D8: +1

4. "Ok, so I've started on my first D8 field formatter (or widget or field_type or whatever) class and I get error messages I don't understand."

You mean like this one:

Doctrine\Common\Annotations\AnnotationException: [Syntax Error] Expected PlainValue, got ')' at position 649 in class Drupal\leaflet\Plugin\field\formatter\LeafletDefaultFormatter.

Yep, not very clear I admit. "At position 649…" -- are they expecting us to count the number of characters? And from where? What about a line number? This error is probably due to you inserting a superfluous comma before your closing parenthesis. This is precisely what the Drupal coding standards insist on for arrays, but it's bad in Doctrine annotations.

However worse than a vague error message is no error at all and your class not working. For classes other than the stock standard, it can be tedious to find the correct spot in the directory hierarchy for the class to be noticed by the auto-loader. Some diagnostic tool would be welcome.

D8: -1

5. "Because D8 requires PHP 5.3.10 or later can I now safely employ some of the language features that 5.2.x didn't have ?"

Yes! Most notably const, javascript-style closures (anonymous functions) and the nifty binary shorthand for the tertiary conditional operator ?:

$value = somelongarrayname[$key][0]['name'] ? somelongarrayname[$key][0]['name'] : 'empty';

becomes:

$value = somelongarrayname[$key][0]['name'] ?: 'empty';

Better?

D8: +1

6. "{{Twig}} is breaking by back."

Oh come off it! Twig is easy and syntactically not a great departure from PHPTemplate. But your templates end up cleaner and easier to work with for themers. Check out this one-page tutorial. See? It's not hard.

Then copy an existing core .html.twig template and adjust to fit your needs.

D8: +1

7. "I need to learn Oh-Oh..."

Yes, Object Oriented basics would be good. Copying & pasting and following patterns found in other modules will get you started producing working D8 code. To understand what overriding is and not to forget to call the parent in the first line of the function body will be a bonus. So read up and don't be baffled by symbols like :: and -> or the use of camelCase.

Also, look at it this way: in the process of learning D8, you'll learn how the rest of the world does programming. Not a bad skill to have...

D8: +1

To summarise, in the words of the goddess of victory: Just do it!

Add your own D8 tips and traps to the comments below!

*) After publication a few translations have been suggested for this Dutch phrase. The one that seems to suit best is "Good riddance to bad rubbish!"

1st picture: Western Maryland Railroad

2nd picture: SodaHead