OOP developer experience: snap to grid
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.
<span style="color: #000000"><span style="color: #0000BB"><?php<br>$entity_type_id </span><span style="color: #007700">= </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">configManager</span><span style="color: #007700">-></span><span style="color: #0000BB">getEntityTypeIdByName</span><span style="color: #007700">(</span><span style="color: #0000BB">$config_name</span><span style="color: #007700">);<br></span><span style="color: #0000BB">$entity_storage </span><span style="color: #007700">= </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">configManager</span><span style="color: #007700">-></span><span style="color: #0000BB">getEntityManager</span><span style="color: #007700">()-></span><span style="color: #0000BB">getStorage</span><span style="color: #007700">(</span><span style="color: #0000BB">$entity_type_id</span><span style="color: #007700">);<br></span><span style="color: #0000BB">$entity </span><span style="color: #007700">= </span><span style="color: #0000BB">$entity_storage</span><span style="color: #007700">-></span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br></span><span style="color: #0000BB">?></span></span>
We could call this "autocomplete driven development" cos the autocomplete in your IDE pretty much drives you forward. Oh yeah: trying to develope D8 without an IDE is not something I'd recommend.