Recent Blog Posts

Turn Drupal 8 into an Identity Provider with SimpleSAMLphp

By Ronald van Belzen | December 11, 2017

There is enough information available to help you turn a Drupal 7 installation into an Identity Provider (IdP) for Single Signon (SSO) and Single Logout (SLO). In fact that information will help you with accomplishing the same for Drupal 8. However, the amount of configuring that is involved to accomplish this might be too daunting for someone starting out on this venture.

Personally the following links helped me on the way:

The latter delivers a Drupal 7 module and a SimpleSAMLphp module written for Drupal 7 and instructions on how to configure these. They are the same modules used by the author of the blog post in the first link.

Brad Jones has programmed a module inspired by the work done by Steve Moitozo for Drupal 7 (Drupalauth module): saml_idp. This blog describes how to use saml_idp to turn your Drupal 8 installation into an IdP.


The saml_idp module that will be installed with Composer depends on openid/php-openid, which in turn requires the PHP extension GMP to be installed. Most standard PHP installations do not include this extension. You may need to install it first. In my situation I used the Linux shell command:

sudo apt-get install php7.1-gmp

After restarting the webserver the module can be installed using Composer:

composer require drupal/saml_idp

The installation description for saml_idp advises you to run the post installation script. You can do this with Drush from the web root with the command:

drush ev 'Drupal\saml_idp\Install::postInstall()'

What this post installation script does is create the subdirectory /vendor/simplesamlphp/simplesamlphp/modules/drupalauth and in that subdirectory create an empty file with the name 'default_enable'.

Upload an Image File using REST API in Drupal 8

By Ronald van Belzen | December 8, 2017

Currently there is no support to directly upload images using REST in Drupal 8 ( The work-around that I describe here uses Base64 encoded images to accomplish the upload of an image using REST.

For a decoupled Drupal 8 installation I needed to upload an avatar image for a user. Since I started with a minimal installation of Drupal 8, I first had to enable the image and field_ui modules and added an image field to the user entity that I named 'avatar' (with the machine name 'field_avatar'). I also created three new image styles specifically to be used for this image style (machine names: 'avatar_large', 'avatar_medium' and 'avatar_small').

Next, I installed the restui module and enabled the modules basic_auth, rest, restui and serialization before starting on my own module. The info file 'web/modules/custom/mymodule/':

name: MyModule REST Services
type: module
description: "MyModule REST Service Resources"
package: Web services
  - rest
core: '8.x'

The helper class that I created to handle most of the functionality is displayed in full below, but will be explained as we make use of its functionality ('web/modules/custom/mymodule/src/Base64Image.php'):

Change the sorting of a view in Drupal 8

By Ronald van Belzen | February 10, 2017

The situation that required this solution was a view on nodes in which duplicate titles occurred frequently.

Instead of just sorting the list of nodes in the view by the title or the creation date, the sorting on title needed a secundary sorting field. For this I needed to implement the hook "hook_views_query_alter" in the file "" as shown below.


use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;

 * Implements hook_views_query_alter().
 * Order by title and created when order by title is requested.
function mymodule_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  if ($view->id() == 'my_view_id') {
    if ($query->orderby[0]['field'] == 'node_field_data.title') {
      $query->orderby[1]['field'] = 'node_field_data.created';
      $query->orderby[1]['direction'] = 'DESC';

I had some problems getting it to work. Fortunately, a test script made me realize that the hook function needed to placed in the above mentioned ".inc" file. And the test reminded me of the fact that the function parameters are not arrays, as would be the case in Drupal 7, but objects. For that reason you should not use '&$view' nor '&$query' as you would do in Drupal 7, which I did at first. After a cache rebuild the hook does its work by adding the second sorting criteria on the created field when the title field was clicked for the view with id "my_view_id".

Localized number format in Drupal 8

By Ronald van Belzen | July 14, 2016

When building this blog I decided to use the core module statistics to display the view count on blog posts. Because some of my older blog posts have had a lot of views, the counter on those blog post views had grown quite large. For making those large numbers more readable it would be nice that those numbers would be displayed with thousands-seperators, just as can be accomplished with the PHP function number_format() or, even better, with the NumberFormatter class. The statistics module does not do that.

As it turns out Drupal 8 core does not support that either, and a sole module that offers this functionality has no Drupal 8 version. I decided to make a solution specifically for the statistics module. That module implements the hook hook_node_links_alter() in the statistics.module file to add the view count to the list of node links (as text). So I set out to create a copy of this hook that would override this particular hook.

The module to which I will add this hook will be named mystatistics:

name: 'My Statistics'
type: module
description: 'Module to override the display of the Statistics module.'
package: Other
version: '1.0'
core: '8.x'
  - statistics

In the mystatistics.module file I added the following code:

Change the PageUtility class into a service

By Ronald van Belzen | May 1, 2016

In the previous article of this series we concluded writing a class that will take care of the pagination, sorting and filtering of data in a tabular display. Next we are going to transform this class into a service. For this we are going to rename the class and place it in the proper directory in accordance with the namespace for this new class that will be:

namespace AppBundle\DependencyInjection;

For coming up with a new class name let's consider what the service will actually do. It will provide the data to be displayed on a page. For this reason I will call the new class PageDataProvider. With this name we can add the service to our "services.yml" that is imported by the configuration file of our application:

        class: AppBundle\DependencyInjection\PageDataProvider
        arguments: ["@request_stack", "@service_container"]

As you can see we pass the RequestStack and the ContainerInterface as arguments for the class constructor. These parameters do not coincide with the constructor parameter of the PageUtility class. So, let's change the constructor of our new class accordingly:

namespace AppBundle\DependencyInjection;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;

class PageDataProvider {
    protected $requestStack;
    private $request;
    private $em;
    private $entityName;

    // ...
    public function __construct(RequestStack $requestStack, Container $container) {
        $this->requestStack = $requestStack;
        $this->em = $container->get('doctrine')->getManager();

In the constructor we set the new variable $requestStack and set the enity manager $em. We now need a function in which we can set the entity name for which the data need be be delivered: