As a developer you get these requests to create projects in a framework so they can see if you fit their development team. I was asked, not so long ago, to create a headless CMS using Drupal 9, which meant creating a custom content entity type and use it via the Drupal JSON API module.
Since I haven’t touched Drupal in so many years, I had to read the Drupal docs to try understand how these modules work now and how to create them. It was a big nuisance, because their docs didn’t have a flow I could follow and their support forums have countless questions from other users with no real answers. So it was a dead end and made me question the usage of Drupal… But that’s a different story.
Finally I found some real help using their Drush CLI.
You can get Drupal here https://www.drupal.org/download
Or get it using the DrupalComposer project:
composer create-project \
Note: The downloaded version from drupal.org might not come with drush as a dependency so just add it in
composer require drush/drush
Lando init & start
Since I’m using Lando to containerise my projects, I’ll need to start it up. Obviously you can use your own tool such as MAMP, Laravel valet etc. So you can skip this part.
Initialise a Lando project:
These are the options I chose using the Lando CLI
? From where should we get your app's codebase? current working directory? What recipe do you want to use? drupal9? Where is your webroot relative to the init destination? web? What do you want to call this app? drupal9
Start the Lando Project:
Run through the Drupal CMS installation in the browser
Note: If you are using Lando, you can get your database info with this command
Create a custom Drupal entity type with drush
You can do a lot with the drush cli, so run lando drush help to see all the commands or go to https://www.drush.org/latest/commands/all/
Let’s create our entity type:
lando drush generate module-content-entityWelcome to module-content-entity generator!–––––––––––––––––––––––––––––––––––––––––––––Module name:➤ Something NewModule machine name [something_new]:➤...The following directories and files have been created or updated:–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––• modules/something_new/something_new.info.yml• modules/something_new/something_new.links.action.yml• modules/something_new/something_new.links.menu.yml• modules/something_new/something_new.links.task.yml• modules/something_new/something_new.permissions.yml• modules/something_new/something_new.routing.yml• modules/something_new/src/Entity/SomethingNew.php• modules/something_new/src/Form/SomethingNewForm.php• modules/something_new/src/Form/SomethingNewSettingsForm.php• modules/something_new/src/SomethingNewInterface.php• modules/something_new/src/SomethingNewListBuilder.php• modules/something_new/src/SomethingNewViewBuilder.php
Enable the content entity
Enable the module in the browser with the Drupal admin backend i.e. Manage > Extend or run:
lando drush en something_new
Where something_new is your custom entity module machine name.
Now you can start creating content for your entity and use it with the JSON:API or across the Drupal CMS.