After writing my first Drupal 8 contrib module I have a new appreciation. Let me try to explain. It is certainly possible to write a letter on an empty sheet of paper but achieving tidy results is quite a lot easier if it's a lined sheet. In Drupal 7, the code flow in general is calling a function, getting an array, manipulating the array and passing it to another function. Pretty much anything goes. In Drupal 8, you are handed an object and the object has methods. Instead of trying to figure out which possible function are you continuing with, it is one of the methods. When passing on data, the receiving method (hopefully) has a helpful type hint which tells you what to pass in. Often even the argument name will be helpful too.
It's exactly like having a lined paper: you still need to know how to produce legible handwriting but at least you have a guide. Another apt metaphor is lining up shapes in a drawing program. Yes, it's possible by hand and there's more freedom doing it but for most it's easier to just use the snap to grid feature.
This blog post was inspired by ConfigDevelAutoImportSubscriber
having a $this->configManager
object. That I need a config manager I have copied from a core file doing similar things. So, I wanted to create an entity -- I already know doing that requires calling the create
method of the relevant storage controller. But how do you get one? Well, ConfigManagerInterface
only has 10 methods and only 1 looks even remotely relevant: getEntityManager
. So now I have a EntityManagerInterface
object. Look, a getStorage
method.
<?php
$entity_type_id = $this->configManager->getEntityTypeIdByName($config_name);
$entity_storage = $this->configManager->getEntityManager()->getStorage($entity_type_id);
$entity = $entity_storage->create($data);
?>
We could call this "autocomplete driven development" cos the autocomplete in your IDE pretty much drives you forward. Oh yeah: trying to develop D8 without an IDE is not something I'd recommend.
Commenting on this Story is closed.
Same.
Spent parts of my vacation doing stuff with D8 just for fun.
You've nailed it on the head - such a better experience.
@larowlan
A great post from developer experience point of view. I am glad that it's enjoyable for you. Thank you for sharing this.
I have worked on several D8 contrib modules in recent past and I know form where are you coming from. For me the biggest win is one class/file for adding major functionality like a single class for a form, a single class for a block, a single class for a views plugin, a single class for field types,formatters and widgets, and a single class for entities.
Congratulations, you've produced three lines of obtuse, un-beautiful code that require an IDE to write and a computer science degree to understand. :-\
But, I do not have a CS degree. And, I will bite: what's in here that requires a CS degree? Yes, probably it requires an IDE but I am arguing this is easier to do than before because you have a lot of guidance.
Anyone that understands object oriented code should be fine understanding those 3 lines.
If they want they can go read some of the code of the classes being used if they want to understand more deeply, but that goes for pretty much any code anywhere.
Yes, "growing up" on Drupal since 5 beta I'm also glad about this.
Though I had to change my IDE, NetBeans, because it's navigator didn't show inherited methods so I had to switch to PHPStorm.