Drupal 8 Content Types and Views
Drupal 8 content types and views are a core capability in the CMS. With D8 getting pretty close to ready for betas it is a good time to play around with these. We’ll take a look at all the basics within Drupal 8 to see what’s new, different, and also what is actually working (at the time of writing). Creating a basic content type, some views of that, and then applying the views to pages and blocks is what I’ll do.
Getting Drupal 8 Installed
There are lots of ways to get a full Drupal 8 stack installed. On my MBP I already have a LAMP stack installed for Drupal 7 and other Symfony 2 development work so the simple thing for me to do is a git pull to grab the code, placing the under under my apache root. Before loading up the install page, I created a new MySQL schema and user. Pretty simple – the normal install process works just fine.
I’ve gone thru this on some previous posts in case that wasn’t enough detail.
The file layout in Drupal 8 is quite a bit different from Drupal 7. In the Core is all of the main logic re-organized – if you are familiar with Symfony2 then you can see some familiar things. However there’s also quite a bit different in here from a typical Symfony2 application. Well – let’s move on – this post isn’t about the merits of how drupal.org is using Symfony2.
Content Types – What’s a Content Type?
I can’t tell you how many Drupal sites I’ve looked at where the developer(s) obviously did not know what a content type was. Extra database tables in the schema and modules with embedded SQL are a classic sign of a problem with a Drupal site. A content type, one of the core features of Drupal, allows you to define a data type through the admin system and then also define form and display formats. Drupal will create the HTML forms to create, edit, and delete instances of the data object.
If you are familiar with using content types in Drupal 7 then you will be right at home in Drupal 8. Seems like there is very little difference in Drupal 8 (D8 is on the left:
Creating Drupal 8 Content Types
Creating a content type is as simple as hitting the button and filling out the information. Let’s make beer! Um oh yeah a content type to model beer. Not as cool, but ok 🙂 So click the “add content type” and you’ll get some basic entry things. Complete that and you can move to the next step of actually adding fields to your beer data type. First you might want to change the default field from “body” to “review”. Tehn you can add a few fields:
- Style – the kind of beer
- Rating – use a list (integer) for a select box.
- Review – already added, but updated the name.
- Review Date – use a date field for this one.
That’s pretty good for now – you can always add more later.
Going thru the steps above, one of the things yet to be ported for Drupal 8 is all the fancy field display types, like popup calendars and fivestar ratings, but for the most part all the guts of content types seems to work just fine. With that done we can add new beers just like adding pages. How cool is that? Here’s a shot of the form – you’ll notice the error, but the form does in fact work – I’ll have to look at that error and see if there’s an open bug on drupal.org.
A fairly basic form, but that’s pretty cool. Once the full set of UI widgets are ported up to D8 there will be additional options to make creating complex content types easier.
So now I’ll go and create a few beers, but that means I need to drink a few beers. Sounds like fun! I love Drupal!
Creating Drupal 8 Views
Once you create a few beers – how does one display them nicely? Well the answer is Views – view basically are queries that you can create from within the Drupal admin system and assign their results to a block.
To find this bit of magic, go to Structure and Views from the admin menu. Then you can pick off the content type (Beers) and tell Drupal to build both a block and a page view for your content. You can also add a link to a menu – for example the main navigation menu to get quickly to the full list. Again – if you know D7 then D8 is pretty familiar with a few UI changes.
You can do the same with blocks – and add a summary view of the content into a block that you can then assign into your layouts.
I’m excited to see that so much of Drupal 8 is really working in a robust way. I think also the fact that performance seems to be ok is encouraging. I’m really hopeful for a summer run on RC’s and a 2014 release. We’ll have to see.