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.

Add ajax multiple autocomplete to textarea

In this article we will show a solution to add an ajax call to populate multiple information in a Drupal 8 form textarea element.

In this example, the script will autocomplete users list in the form for a custom module called MyModule. The user will enter first 2 letters of a name or email.

1) create a library

In MyModule.libraries.yml add the necessary javascript reference that will be used to populate the users in the form:

MyModule_lib:
  version: VERSION
  js:
    js/autocomplete.js: {}
  dependencies:
    - core/jquery
    - core/jquery.ui.autocomplete

The autocomplete function as dependencies which are based on jQuery library.

 

2) JS script

The jQuery autocomplete.js file that we use is copied below. It is implemented as Drupal behaviors You need to add this file in MyModule/js/ folder.
 

JS script:

(function ($, Drupal, drupalSettings) {

Send HTML mails with Drupal 8

At the time of this article in October 2015, the HTML swiftmailer module version for Drupal 8 was not released. Thus the below solution is not relevant anymore but may help for configuration or setup. We also have a new post about installation of swiftmailer

Here is a possible solution for sending HTML mails with Drupal 8 specifically when creating custom modules.

The solution below uses Swiftmailer library.

The main module compatible with Drupal 8 can be downloaded here.

This version needs some corrections if it is used with a recent Drupal 8 release. In this case, these are the lines changed:

Configuring Drupal with remote database

Configuring Drupal with a remote database is possible and in some cases recommended.

Our Drupal 8 EK management tools application is a particularly good case where this setup is very helpful. EK manages Drupal system configuration database and content database separately. In other words, the installed database from Drupal 8 and the database where all EK custom modules save their data are different.

This configuration brings few benefits: