Custom Block in Drupal 8

In this article, we will see how we built custom blocks in EK management tools suite with a sample basic block in a module called 'mymodule' used for demo. It can be used to display multiple content, static or dynamic as in the example above.

Create the block script

First we will create a script that will display some content within a block. the script file will be called MyBlock.php and is placed in /mymodule/src/Plugin/Block/.


/**
 * @file
 * Contains \Drupal\mymodule\Plugin\Block\MyBlock.
 */
namespace Drupal\mymodule\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;

/**
 * Provides a 'Custom module widget' .
 *
 * @Block(
 *   id = "my_block",
 *   admin_label = @Translation("My custom block"),
 *   category = @Translation("mymodule Widgets")
 * )
 */

Install and use Swift Mailer in Drupal 8 (part 2 : implementation)

In previous article we explained how we installed the Swift Mailer module and its dependencies.

In this second part, let's see how we configure and implement it to use in our modules to send formated HTML mail with attachment.

First you will need to have Mail system module installed already. There is no particular issue or difficulty here.

 

1) Configure Swift Mailer

 

In Swift Mailer configuration (/admin/config/swiftmailer/transport) , we select the following options:

Transport:

Install and use Swift Mailer in Drupal 8 (part 1 : dependency)

In a previous post from 2015, we described usage of Swift Mailer module to send HTML mail and mail with attachment. At this time, the module was not yet available for Drupal 8.

There is now a version alpha1 available. Let's go through installation process.

Because it has been rather tedious for us, we will try to explain the flow of the process as much as possible to help you save time.

 

1) Composer

 

The prerequisite is the installation of composer.

In our case we installed first on Windows inside a folder named  F:\Program Files\composer2\.

The installation exe for Windows can be found here.

Drupal 8 Guided Tour module

In Drupal 8 there is a Tour module in core that is very useful when it comes to web applications. In EK management tools we target professional users with small to medium scale companies. They usually have limited resources and time to spend on back office trainings. This is where the Tour module is very convenient to introduce functionalities to users who can quickly grasp the functions available to manage their back office.

We use the Tour functionality in our pages to guide users in their daily tasks like for instance in the form to create a new invoice or project page:

 

Invoice guided tourproject tour

 

Create list view in custom module (part 3)

In previous articles (here and here), we have seen a method to add custom views and data in MyModule.

With Drupal 8 there is a very easy and practical way to add this custom view as a configuration that will be installed with the module.

1) extract the configuration data

Navigate to "/admin/config/development/configuration/single/export".

On this page, select configuration type 'view' and configuration name 'My module list' that was created earlier.

 

Single export

 

2) create configuration install file

You will obtain from the above export a list of configuration data that you can copy and paste into a file called for instance "views.view.mymodule-list.yml";

Simply place this file into the install folder :

Create list view in custom module (part 2)

In previous article we have seen how to declare the data accessible in a custom view in MyModule.

Now that the data from our tables mymodule_tb (and mymodule_tb_2) are available, let's create the list view.

First navigate to "/admin/structure/views/add" and create the view by entering basic information as per the example below.

 

New view basic info

 

After "save" you are redirected to "Edit" form where further settings will be set.

1) Add fields from you source table

Add fields

 

 

From this form, select the fields to display. We will select 2 here "name" and "type" (refer to the table structure declared in MyModule_views_data())

Create list view in custom module (part 1)

In our EK management tools suite we have custom designed lists of items like for instance list of management documents.

Those lists are build with custom codes and templates which is somehow more convenient to manage with complex data, links, menus and filters as in the example below.

Example of documents list

 

However for simple list, the views module is very useful and can be integrated in a custom module as well to automatically create the list.

Here is an example with companies list in the system address book module showing the company name as link and a field about the type of record plus a simple filter box.

Drupal 8: back office - modules solution

Sometime in April 2014, we started an ambitious project to "translate" into Drupal 8 modules an in-house developed back-office management solution.

This back-office system, EK, was initiated in 2006 for internal needs. It was developed in PHP with mysql data storage. The initial idea was to have a tool that could help a new company to run its business as efficiently as possible with few constraints that are always critical in a good organization: central and unique data references (share unique information across offices); simple to use with minimum training or learning curve (no resource for that!); flexible and cost effective; access control and security.