Build a blog with Drupal 8: Part 2

By Ronald van Belzen | March 24, 2016

I concluded the previous part of this blog with the message that we are not finished yet.

One of the first problems that I am going to tackle is to not display the friendly message of Drupal that "No front page content has been created yet". I would like to install the module Empty Front Page, but it is not available for Drupal 8 yet at the moment of this writing. For that reason I am going to build my own solution by building my own module. I have decided to call that module "My Empty Front Page".

My Empty Front Page

The directory in which I will place the sources is a subdirectory of the module directory: "module\custom\my_empty_front_page". In that directory I first create a file "my_empty_front_page.info.yml" that will provide the Extend page in the Administration section of my site with information about this module.

name: 'My Empty Front Page'
type: module
description: 'My empty front page for Drupal 8 (removes all front end page content when Default front page points to /empty)'
package: Other
version: 1.0
core: '8.x'

Next I need to inform Drupal what controller function to call by creating a routing file with the name "my_empty_front_page.routing.yml" in the same subdirectory.

my_empty_front_page.content:
  path: '/empty'
  defaults:
    _controller: '\Drupal\my_empty_front_page\Controller\MyEmptyFrontPageController::emptyContent'
  requirements:
    _permission: 'access content'

The routing file tells Drupal that for the path '/empty' the controller "MyEmptyFrontPageController" function "emptyContent" needs to be called. The permission tells that the same access permission as for viewing content is to be used. I still need to program that controller in the file "MyEmptyFrontPageController.php" that will be placed in the subdirectory "module\custom\my_empty_front_page\src\Controller".

<?php
namespace Drupal\my_empty_front_page\Controller;

use Drupal\Core\Controller\ControllerBase;

class MyEmptyFrontPageController extends ControllerBase {
	/**
	 * Return empty content for an empty front page
	 */
	public function emptyContent() {
		return array('#markup' => '');
	}
}

Next I will need to visit the administration Extend page where I will find my module grouped under Other. Check the checkbox for the module and click the Install button. I still need to configure my site to make use of the module's functionality in Configuration > Site information. Under Frontpage I change the value of the field "Default front page" from "/node" into "/empty". And this solves the problem of the display of the warning text "No front page content has been created yet" on the front page.

This is, of course, a very simple module. It does not teach you how to program for Drupal 8. The focus was to solve a problem. When you are interested to learn about programming in Drupal 8 I would advise to take a look at the Drupal documentation. I would especially recommend to take a look at the coding examples.

Add new comment