View on GitHub

drupal-govcon-2017-bodiless-drupal

Source code for DrupalGovCon: 2017 Bodiless Drupal presented by Quotient, Inc.

Drupal GovCon 2017
Sponsored by Quotient, Inc.
Visit our Drupal GovCon 2017 page

Environment Setup

All examples were developed against Drupal 8.3 using Acquia Dev Desktop Version 2, built on March 23, 2017.

Acquia Dev Desktop Downloads

PLEASE NOTE: Many of the examples below are minimal representations of Drupal applications. You would also want to add in validation, stronger security measures, and error checking. These examples are not exhaustive solutions - they simply demonstrate the concept of Bodiless Drupal.

Where to place the ‘static_files’ files
The code repository contains a folder of static files called, oddly enough, “static_files”. These files should be moved to a location outside of your Drupal root.

The Examples!

Example 1: Bodiless Drupal with a simple text file

This example demonstrates a Drupal page which renders content stored in an external text file. All functional features of Drupal are available and may be used, with the exception of the database.

Before installing the module
----------------------------
You will need to make a minor modification to the code to ensure
Drupal knows where the external file lives. Edit the path value
on Line 6 of modules/custom/ex1_flatfile/ex1_flatfile.routing.yml
so that it contains the full path and filename to where you put
sample.txt.

Example 2: Bodiless Drupal with a JSON file

This example demonstrates a Drupal page which renders content stored in an external JSON file. All functional features of Drupal are available and may be used, with the exception of the database.

Before installing the module
----------------------------
You will need to make a minor modification to the code to ensure
Drupal knows where the external file lives. Edit the path value
on Line 6 of modules/custom/ex2_jsonfile/ex2_jsonfile.routing.yml
so that it contains the full path and filename to where you put
sample.json.

Example 3: Bodiless Drupal with an external MySQL database

This example demonstrates a Drupal page which renders content stored in an external MySQL database. All functional features of Drupal are available and may be used, with the exception of the database.

Before installing the module
----------------------------
You will need to modify your settings.php configuration file to
register your external MySQL database. Execute the mysql dump of
the sample data in the static_files folder to create the table
and insert the sample data.

$databases['govcon']['default'] = array(
  'database' => 'your_db_name',
  'username' => 'your_db_user',
  'password' => 'your_db_pass',
  'host' => 'your_db_host',
  'port' => '3306',
  'driver' => 'mysql',
  'prefix' => '',
  'collation' => 'utf8mb4_general_ci',
);

Example 4: Create a RESTful API endpoint to retrieve external data

This example demonstrates how you can use Drupal in a bodiless AND headless way. Drupal provides the RESTful API endpoint to retrieve data from an external MySQL database. The sample node.js application is the front end which consumes the Drupal RESTful API endpoint via a GET call.

Prerequisites

Before installing the module
----------------------------
1. Enable the RESTful Web Services module.

2. Enable the HTTP Basic Authentication module.

2. Download and enable the REST UI module:
https://www.drupal.org/project/restui

3. You will need to modify your settings.php configuration file
to register your external MySQL database. Execute the mysql dump
of the sample data in the static_files folder to create the table
and insert the sample data. You may skip this if you have alreay
completed it as part of Example 3.

$databases['govcon']['default'] = array(
  'database' => 'your_db_name',
  'username' => 'your_db_user',
  'password' => 'your_db_pass',
  'host' => 'your_db_host',
  'port' => '3306',
  'driver' => 'mysql',
  'prefix' => '',
  'collation' => 'utf8mb4_general_ci',
);

4. Modify your hosts file (C:\Windows\System32\drivers\etc\hosts)
to map 127.0.0.1 to your Drupal instance, if running locally. For
example, my hosts file has a reference like below:

127.0.0.1	govcon2017.localhost

Setup

Drupal > Admin > Extend

Drupal > Admin > Configuration > REST

Drupal > Admin > People > Permissions

Other general setup steps

{"id":"1","fname":"Paul","lname":"Day","email":"pday@quotient-inc.com"}

Example 5: Auto-generate Drupal form based on a JSON schema

This example demonstrates using a JSON schema to auto-generate a Drupal form for data entry.

Before installing the module
----------------------------
This module does security horribly wrong - don't do this in production!
The text file containing JSON data (ex5-json-forms-article.json) is
located in the module root.

The purpose of this example is to illustrate the ease of modifying
external data while adhering to a schema.