drupal

recent posts related to the drupal content management system

Drupal “Rewrite the output of this field” replacement patterns alvin May 23, 2016 - 7:56pm

As a quick note, if you’re looking at a Drupal form and it says you can use the "Rewrite the output of this field" replacement patterns shown (somewhere) on this page — and you can’t find those replacement patterns on that page — you can find a complete list of them at this drupal.org url.

As an example, if you’re working with a Drupal Node, you can use replacement patterns like these:

[node:author:name]
[node:content-type]
[node:content-type:name]
A Drupal 8 XML Sitemap generating PHP script alvin May 23, 2016 - 5:25pm

I’m not going to comment on the following code too much or provide support for it, but (a) if you need to create an XML Sitemap for a Drupal 8 website, and (b) you don’t like the Drupal 8 sitemap modules that are available, then (c) this PHP script can serve as a starting point for you.

A PHP script to migrate Drupal 6 Nodewords to Drupal 8 Metatag “meta descriptions” alvin May 22, 2016 - 8:25pm

If you’re trying to convert Drupal 6 Nodewords “meta description” fields to Drupal 8 Metatag meta description fields, the following PHP script may help you.

Why doesn’t Drupal have a meta description field by default?

I like about 98% of Drupal. A lot of it is well thought out and organized. But some design decisions are real head-scratchers. For example, why in the world does Drupal 8 not include the ability to set a meta description field out of the box? I understand that meta keywords aren’t used by (most) search engines any more, but internet search engines do use the meta description field, and Drupal is supposed to be a CMS for the internet, so why doesn’t it include such a basic CMS feature?

Instead, in Drupal 8 we currently have to rely on an “alpha” third-party module for this capability. It’s cool that this module will eventually emit a bunch of other meta tags that may be important, but IMHO, the meta description field is a basic, out of the box requirement of any CMS. And because of all of these things, I have to spend many hours porting my old Drupal meta descriptions to this new module, which oh by the way is in an alpha state, and may change after I finish my port. #argh

Drupal 8 website not showing images on image/photo nodes

If you have a Drupal 8 website and your images are not showing up on your image/photo nodes, one possible problem (which I just learned) is that your Drupal 8 theme needs to refer to {{ content }} in node.html.twig and not {{ content.body }}.

I don't know why you have to do that, but after four hours of troubleshooting the problem with my own Drupal 8 website/theme, I can confirm that if you don't do it, your images/photos won't be shown on their nodes. (All I was trying to do was to separate content.body from content.comment, and when I did that my images no longer showed up at their nodes/URLs.)

Drupal 8 Twig template "if string is not in array" solution

As a quick note, if you need a Drupal 8 Twig template if/else/then structure where you test to see if a string value is in an array, code like this will work:

{% if node.getType not in ['photo', 'text']  %}
    <div class="similar">
        {{ similar_by_terms }}
    </div>
{% endif %}

That code can be read as, “If the node type is NOT ‘photo’ or ‘text,’ emit the HTML/Twig code shown.”

What are the Drupal 8 Node class fields (field names)?

I was just trying to modify one of my Drupal 8 template files — node.html.twig — and I couldn’t find any good documentation for what variables/values/fields are in the Drupal 8 Node class, so I dumped some output to my browser, and saw that these are the Node fields:

Drupal 8: How to put a View or Block between the Content and Comments

I’m not going to discuss this code much, but in short, the source code below is for a Drupal 8 preprocess_node function that I use to set variables for (a) a custom view and (b) a custom block. I set the variables in this function, and then display them in my node.html.twig file like this:

{{ similar_by_terms }}

and this:

What are the Drupal 8 Twig template front page file-naming conventions?

Drupal FAQ: What are the Twig template file naming conventions for Drupal 8? Specifically, how do I need to name a template file so it will be used for the front page of my website?

In short, the Drupal 8 Twig “HTML” template file has this name:

html.html.twig

and if you want to create a custom HTML front page template, use this name:

html--front.html.twig

The same goes for “page” and “node” files, where the default page template file has this name: