Based on requirements of a couple projects that I am currently working on, I was in the need to create a content entity type in Drupal 8, and as usual I decided to go ahead and do what every developer does, RTFM and follow the steps as shown on the official documentation.
I was lucky enough to found a link related to my task “Creating a content entity type in Drupal 8” on the official documentation but since code on a project at Alpha state changes a lot the documentation was not up to date, it means the official documentation is the code and as you can guess I faced some challenges that I will list below and the approach I used to overcome it.
While creating this blog post the documentation was update so you can see the original documentation I was reading when started writing this blog post https://www.drupal.org/node/2192175/revisions/7222609/view the only change so far is the migration from PSR-0 to PSR-4 as you can see it here https://www.drupal.org/node/2192175/revisions/7382233/view
First task was to copy all the source code from the documentation and this was the result
Source code at github https://github.com/jmolivas/foo_bar/tree/1-source-code-from-documentation
As you can see the directory structure is based on PSR-0 but Drupal modules must be PSR-4 so I updated with the following result.
Source code at github https://github.com/jmolivas/foo_bar/tree/2-update-to-psr-4
After installing the module and trying to visit the route "foo-bar/list" the following error was triggered.
Fix: Replace on "src/Entity/FooBar.php" class at the plugin controllers declaration "list" with "list_builder" and since "FooBarListController" class does not exist, point it to the correct class "FooBarListBuilder"
After this changes the route "foo-bar/list" was properly working
Source code at github https://github.com/jmolivas/foo_bar/tree/3-list-builder
When trying to add new content using the follwing route "foo-bar/add" the following error was triggered
Fix: "ContentEntityFormController" class not longer exist it was repalced with "ContentEntityForm" then the next step was to replace it on "FooBarFormController" class
After this changes the route "foo-bar/add" was properly working
Source code at github https://github.com/jmolivas/foo_bar/tree/4-ContentEntityFormController-class-not-found
When trying to save the values in the form and click save the following error was triggered
Fix: Set "type" form field default value as Entity Type Id at "form" method on "FooBarFormController" class
Source code at github https://github.com/jmolivas/foo_bar/tree/5-integrity-constraint-violation
When trying to delete any content using "foo-bar/{id}/delete" the following error was triggered
Fix: Return an URL object on getCancelRoute method at "FooBarDeleteForm" Class
After this changes the route "foo-bar/{id}/delete" was properly working and I was able to delete content
Source code at github https://github.com/jmolivas/foo_bar/tree/6-call-to-member-function-on-non-object
Finally after the code was properly working you can get the latest version at this github repository https://github.com/jmolivas/foo_bar
I decide to update the documentation with the generated code, you can see the updated version here https://www.drupal.org/node/2192175/revisions/7382931/view
Feel free to send any corrections, improvements or comments related to the new code.