DEV Community

Cover image for Craft CMS from a Drupal perspective
david duymelinck
david duymelinck

Posted on

Craft CMS from a Drupal perspective

My initial thoughts after a first look

You have to pay for this?
Is Yii 2 still alive?
What is the controller code doing in the templates?

So my first thoughts are not positive. But never judge a book by its cover.

My main gripe: controller code in templates

I call it controller code as I'm looking at the code through MVC goggles. So I'm thinking about data retrieval, authorisation and request data handling.

In Drupal the controller code is done by the content type view modes, views and hooks for a more fine-grained output.

The equivalent in Craft is the twig hook tag, which allows you to manipulate the context.

{# the Craft way #}
{% set news = craft.entries()
  .section('news')
  .orderBy('postDate DESC')
  .limit(10)
  .all() %}
{# the hook way #}
{% hook "limited-news-ordered-by-postdate" %}
Enter fullscreen mode Exit fullscreen mode
use craft\elements\Entry;

Craft::$app->getView()->hook('limited-news-ordered-by-postdate', function(array &$context) {
    $context['news'] = Entry::find()
    ->section('news')
    ->orderBy('postDate DESC')
    ->limit(10)
    ->all();
});

Enter fullscreen mode Exit fullscreen mode

If you think the hook tag is still too dirty for your templates you can use controllers by using the config/routes.php file.
This file contains an array with routes as keys and the module/controller/action as value.
There are some gotchas;

  • you better don't create routes for sections in the Craft backend interface
  • In the controller set the $allowAnonymous property
  • the controller name has Controller as a suffix, and the actions have action as a prefix
  • you have to create a module to store the controllers

The weird things coming from Drupal

  • To create translations you need to add multiple sites with a specific language
  • Craft calls content types sections, and there are three types; singles, channels and structures. A Channel is a single dimension list of a type. A Structure is a multiple dimensional list of a type.
  • you can turn the CMS configuration on and off in the backend interface using the allowAdminChanges method in the config/general.php file
  • there are no standard templates for the frontend
  • the fields you create can only be used once per content type

The good things coming from Drupal

  • the matrix field is the equivalent of the paragraphs module
  • the translation configuration is less hassle than in Drupal
  • the data in twig is not wrapped in display metadata.
  • twig has a switch tag, which is nicer than elseifs
  • plugins, drupal modules, can be installed using the store. And it includes adding it to composer.json
  • it comes with graphql out of the box

Conclusion

Craft cms is a CMS solution that is more focused on the backend interface and easy setup. Rather than having a lot of modularity but at the same time have the batteries included.
Like all tools it has its place in your tool belt.

Top comments (3)

Collapse
 
publicvar profile image
Nicolas LUDOVIC

I used it on multiple projects. It has the best backend I've ever used.
If your case is to create a content type then theming the view with your own front-end stack, go for it. If you need do to custom things with some business logic, good luck ! For that purpose, CraftCMS has the worst documentation.

Collapse
 
xwero profile image
david duymelinck

I wouldn't say the documentation is bad. The documentation is written for people who don't want to spend a lot of time on customising the backend.

Drupal wants to do it all, be quick to set up, be modular, be headless, ... And with all those different goals there are consequences in the form of it doesn't do one thing very good.

And that I think is the power of Craft. An easy to setup backend, give enough modularity to let developers do their thing, and make it easy for more frontend orientated developers to set up a cms with a backend language.

I did struggle with using more code than Craft comes out of the box with, but I did find the answers in their documentation.

Collapse
 
mandrasch profile image
Matthias Andrasch

Craft Discord is also helpful - friendly community there!
(as well as craftcms.stackexchange.com/)