drupal

recent posts related to the drupal content management system

Drupal 8: How to write a simple custom “block module”

Table of Contents1 - Goals2 - Backup your database3 - Check my code out of Github, or create a module directory4 - Create a project info YAML file5 - Create the necessary subdirectories6 - Write the code to display your block7 - Clear the caches8 - Enable the module9 - Place the block module10 - See the custom block on your website11 - The biggest problem I encountered12 - The source code13 - Summary

In this tutorial I’ll demonstrate how to write a simple Drupal 8 “block module.” By this I mean that I’ll show you how to write a simple Drupal 8 module that will display output in a block. When you’re done you will have created a new block that you can place in one or more theme regions.

How to create a new book in Drupal 8 alvin August 2, 2017 - 2:44pm

To create a new book in Drupal 8:

How to write if/then/elseif/else in Drupal 8 Twig templates alvin August 1, 2017 - 5:35pm

As a quick note Drupal programming note, here’s an example of how to write if/then/elseif/else in Drupal 8 Twig templates:

{% if node.getType == 'photo' %}
...
{% elseif node.getType in ['book', 'page'] %}
...
{% else %}
...
{% endif %}

While I’m in the neighborhood, here are a few more if conditions I’ve written recently:

How to write “If string starts with” in Drupal 8 Twig templates alvin July 30, 2017 - 11:53am

If you ever need to write a “string starts with” comparison in Drupal 8 Twig templates, I just used this approach in a node.html.twig template file and I can confirm that it works:

{% if uri starts with '/foo' %}

More accurately, what I did was to first get the URI for the current Drupal node, and then I perform that test:

How to get the URI of the current node in a Drupal 8 Twig template theme file alvin July 29, 2017 - 2:43pm

As a quick note, I was just able to get the URI of the current page (node) in a Drupal 8 Twig template theme file using this code:

{% set uri = path('entity.node.canonical', {'node': node.id}) %}

As an example, I’m rendering some different content based on the URI of the current node, so I first use that code to set the uri field, then I have a little Twig if/then/else condition like this:

{% if uri starts with '/foo' %}
    <div>Option Foo here ...</div>
{% elseif uri starts with '/bar' %}
    <div>Option Bar here ...</div>
{% else %}
    <div>Option Baz here ...</div>
{% endif %}

In summary, if you wanted to see how to get the URI of the current page/node when using a Drupal 8 Twig theme template file, I hope this example is helpful.

Drupal 8 Twig ternary operator syntax alvin July 29, 2017 - 2:30pm

As a quick note, the Drupal 8 Twig template ternary operator syntax looks like this:

{{ foo ? 'FOO IS TRUE' : 'FOO NOT TRUE' }}

You can test it like this in a Drupal 8 Twig template theme file if you’d like:

{% set foo = true %}
{{ foo ? 'FOO IS TRUE' : 'FOO NOT TRUE' }}

That test will emit the string “FOO IS TRUE” to your web browser.

You can find more Twig information at this symfony.com page.

Drupal 8 Twig templates comments syntax alvin July 29, 2017 - 2:18pm

As a quick note, the Drupal 8 Twig template comment syntax looks like this:

{# this is a comment #}

The Twig multiline comment syntax uses the same characters:

{#
   This is a comment.
   More comments ...
   Even more comments ...
#}
How to update Drupal 8 using drush alvin June 21, 2017 - 1:42pm

This drupal.org page shows how to update Drupal 8 using drush and other techniques.

How to configure Share Buttons by AddToAny with Drupal 8 alvin May 13, 2017 - 6:17pm

As a quick “note to self,” to get the Share Buttons module by AddToAny working in Drupal 8, I followed these steps:

- install the AddToAny module
- configure permissions so i could work with it
    - admin/people/permissions
- configure AddToAny as desired
    - admin/config/services/addtoany
- create a block, add it to a region (such as Content)
    - i actually created a block, then modified
      my theme to show AddToAny between the Contents and
      the Comments

The key to getting AddToAny was knowing to go to admin/structure/block and then selecting “Place Block” to create an AddToAny block. Not knowing that I had to do that slowed me down for quite a while.