Submitted by webmaster on May 9, 2016

I have been working with Drupal 8 and getting to know the internals of it over the last couple of weeks, and while it is true that in some situation the new Framework and API will require more code for some things, it has reduced a lot of repetition for other things.  Being a sharp developer and knowing how to build out your boilerplates will go a long way to helping to eliminate a lot of rework between your projects.  In this article, I hope to go over some ideas that you can use in order to get started with this process in your team.

So firstly you can create pages using multiple methods.  Extending from existing classes and altering them, Creating new classes that return either a render array, extend FormBase, extend EntityListBuilder or other similar classes.  This means really that you have a lot more flexibility when it comes to generating your pages as compared to Drupal 7's menu callback system.  This adds complexity and a bit of confusion as to which is the best option for a given circumstance.  So I will go through the easiest / Simplest example to the more complex in this article.  I am not trying to make an extensive article here, but give options/ideas.

Create a simple page callback, that uses a theme to render output:


The next level up in complexity, in my opinion, is to return a form to be rendered and managed on a page. Similar to the previous example we will create a new class and notify Drupal in a routing yml file about it so that we can render a settings form.


As you can see, we created our form elements similar to how we would have if we were building a Drupal 7 module, really the only difference is instead of having stand-alone functions for the form, form validation, and form submit functions we are now building all of these contained within our single FormBase class. I think this will lead to better more structured code that should be easier to troubleshoot once you have a good grasp on Drupal 8's API. This example was basically a copy of the Taxonomy Overview Terms class, but there is a better way to do this. That is extending an existing class that renders a page and override it to suit your needs.

Overriding an existing Page building class would look something like this:


In this final example you can see that instead of rebuilding the form in my own Class, I am just extending the OverviewTerms class from the taxonomy. In this case, I am specifying the Vocabulary I am wanting to render terms for. This could be useful in cases where you need to create a streamlined administration interface and are looking to build out / present specific functionality while hiding other things.
While I realize this is not a full extensive list of ways to create pages, with the three above examples you should have enough to get started building your Drupal based sites and applications using the numerous methods that Drupal 8 brings to the table.