Skip to content

Instantly share code, notes, and snippets.

@McFateM
Created April 17, 2020 20:21
Show Gist options
  • Save McFateM/291c29ce411493801e2b72a8d2ce9e6a to your computer and use it in GitHub Desktop.
Save McFateM/291c29ce411493801e2b72a8d2ce9e6a to your computer and use it in GitHub Desktop.
The History of MODS-via-Twig (mvt)

MODS-via-Twig

This project now resides in mark@Marks-Mac-Mini ~/GitHub/lando-d8 ‹8.6.x*› where I'm attempting to spin up a local Drupal 8 instance using Lando. In this instance there is a set of files and directories at ~/GitHub/lando-d8/sites/default/files, namely:

  • main.php - my primary PHP script for this work
  • mods-via-twig.md - this document
  • Digital-Grinnell-MODS-Master-Rev16beta.twig - an export of my latest DG MASTER Twig import template for MODS
  • test/ - a directory of miscellaneous files used for testing
  • social-justice/ - a directory containting files exported from DG's "Social Justice" collection

With this configuration it should be possible to run the main.php script using a command sequence like this:

  • lando ssh - opens a bash shell into the running PHP container
  • cd sites/default/files - change the working directory inside to the container
  • php main.php - launch the main.php script inside the container

My Original Drupal 7 Attempt

Everything you see above, and below, I first had working in Drupal 7, but D7 doesn't include native support for Twig, so it turned out to be a bit of a dead-end. There's more to this document than you see now, but a big chunk of my D7 experience has been commented out.

Restart with a Drupal 8 Target

Since Drupal 7 does not easily support the use of TWIG, I'm going to try this again but in a Lando Drupal 8 client.

The following is based on https://www.jeffgeerling.com/blog/2018/getting-started-lando-testing-fresh-drupal-8-umami-site...

╭─mark@Marks-Mac-Mini ~/GitHub
╰─$ git clone --branch 8.6.x https://git.drupal.org/project/drupal.git lando-d8
Cloning into 'lando-d8'...
warning: redirecting to https://git.drupalcode.org/project/drupal.git/
remote: Enumerating objects: 3190, done.
remote: Counting objects: 100% (3190/3190), done.
remote: Compressing objects: 100% (1615/1615), done.
remote: Total 725431 (delta 1663), reused 2903 (delta 1472), pack-reused 722241
Receiving objects: 100% (725431/725431), 161.75 MiB | 15.37 MiB/s, done.
Resolving deltas: 100% (526786/526786), done.
Checking out files: 100% (13476/13476), done.
╭─mark@Marks-Mac-Mini ~/GitHub/lando-d8 ‹8.6.x›
╰─$ lando init
? From where should we get your app's codebase? current working directory
? What recipe do you want to use? drupal8
? Where is your webroot relative to the init destination? .
? What do you want to call this app? mods-via-twig

   _  __                       _
  / |/ /__ _    __  _    _____( )_______
 /    / _ \ |/|/ / | |/|/ / -_)// __/ -_)
/_/|_/\___/__,__/  |__,__/\__/ /_/  \__/

  _________  ____  __ _______  _______  _      ______________ __  ___________  ______
 / ___/ __ \/ __ \/ //_/  _/ |/ / ___/ | | /| / /  _/_  __/ // / / __/  _/ _ \/ __/ /
/ /__/ /_/ / /_/ / ,< _/ //    / (_ /  | |/ |/ // /  / / / _  / / _/_/ // , _/ _//_/
\___/\____/\____/_/|_/___/_/|_/\___/   |__/|__/___/ /_/ /_//_/ /_/ /___/_/|_/___(_)

Your app has been initialized!

Go to the directory where your app was initialized and run lando start to get rolling.
Check the LOCATION printed below if you are unsure where to go.

Oh... and here are some vitals:

 NAME      mods-via-twig
 LOCATION  /Users/mark/GitHub/lando-d8
 RECIPE    drupal8
 DOCS      https://docs.lando.dev/config/drupal8.html

╭─mark@Marks-Mac-Mini ~/GitHub/lando-d8 ‹8.6.x*›
╰─$ lando start
Let's get this party started! Starting app mods-via-twig...
landoproxyhyperion5000gandalfedition_proxy_1 is up-to-date
Creating network "modsviatwig_default" with the default driver
Creating volume "modsviatwig_data_appserver" with default driver
Creating volume "modsviatwig_home_appserver" with default driver
Creating volume "modsviatwig_data_database" with default driver
Creating volume "modsviatwig_home_database" with default driver
Creating modsviatwig_appserver_1 ... done
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   619  100   619    0     0    147      0  0:00:04  0:00:04 --:--:--   147
100 4775k  100 4775k    0     0   384k      0  0:00:12  0:00:12 --:--:--  855k
 Drush Version   :  8.3.2

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  811k  100  811k    0     0   261k      0  0:00:03  0:00:03 --:--:--  261k
Killing modsviatwig_appserver_1 ... done
Starting modsviatwig_appserver_1 ... done
Creating modsviatwig_database_1  ... done
Waiting until database service is ready...
Waiting until appserver service is ready...
Waiting until database service is ready...
Waiting until database service is ready...
Waiting until database service is ready...
Waiting until database service is ready...
Waiting until database service is ready...

   ___                      __        __        __     __        ______
  / _ )___  ___  __ _  ___ / /  ___ _/ /_____ _/ /__ _/ /_____ _/ / / /
 / _  / _ \/ _ \/  ' \(_-</ _ \/ _ `/  '_/ _ `/ / _ `/  '_/ _ `/_/_/_/
/____/\___/\___/_/_/_/___/_//_/\_,_/_/\_\\_,_/_/\_,_/_/\_\\_,_(_|_|_)


Your app has started up correctly.
Here are some vitals:

 NAME            mods-via-twig
 LOCATION        /Users/mark/GitHub/lando-d8
 SERVICES        appserver, database
 APPSERVER URLS  https://localhost:32808
                 http://localhost:32809
                 http://mods-via-twig.lndo.site/
                 https://mods-via-twig.lndo.site/

╭─mark@Marks-Mac-Mini ~/GitHub/lando-d8 ‹8.6.x*›
╰─$ lando composer install
...lots of output...

╭─mark@Marks-Mac-Mini ~/GitHub/lando-d8 ‹8.6.x*›
╰─$ lando info
[ { service: 'appserver',
    urls:
     [ 'https://localhost:32808',
       'http://localhost:32809',
       'http://mods-via-twig.lndo.site/',
       'https://mods-via-twig.lndo.site/' ],
    type: 'php',
    via: 'apache',
    webroot: '.',
    config: { php: '/Users/mark/.lando/config/drupal8/php.ini' },
    version: '7.2',
    meUser: 'www-data',
    hostnames: [ 'appserver.modsviatwig.internal' ] },
  { service: 'database',
    urls: [],
    type: 'mysql',
    internal_connection: { host: 'database', port: '3306' },
    external_connection: { host: 'localhost', port: '32807' },
    creds: { database: 'drupal8', password: 'drupal8', user: 'drupal8' },
    config: { database: '/Users/mark/.lando/config/drupal8/mysql.cnf' },
    version: '5.7',
    meUser: 'www-data',
    hostnames: [ 'database.modsviatwig.internal' ] } ]

Now, visiting https://localhost:32808 brings up the site for configuration. When prompted there be sure to use the database "Advanced Options" and change the target from "localhost" to "database"!

I then set super-user creds to admin across the board.

It Works!

Just confirming here that the aforementioned set of commands, which is reiterated below, does work...

  • lando ssh - opens a bash shell into the running PHP container
  • cd sites/default/files - change the working directory inside to the container
  • php main.php - launch the main.php script inside the container

Engaging Twig

So now, how do I programattically engage Twig? Well, as luck would have it, Jeff Geerling asked and answered that same question some time ago. His work can be found here. But simply following his lead with a script doesn't seem to work right-out-of-the-box because I'm getting errors like this:

www-data@6db17e5163ef:/app/sites/default/files$ php main.php
PHP Fatal error:  Uncaught Error: Class 'Drupal' not found in /app/sites/default/files/main.php:8

Since I am running drush 8.x (my output from lando drush --version is Drush Version : 8.3.2), I am going to try to follow the guidance provided here to create a new drush command in my Lando-based instance of Drupal 8.

Where To Put My Script?

This is where things currently stand...

╭─mark@Marks-Mac-Mini ~/GitHub/lando-d8 ‹8.6.x*›
╰─$ lando drush status
 Drupal version                  :  8.6.19-dev
 Site URI                        :  http://default
 Database driver                 :  mysql
 Database hostname               :  database
 Database port                   :  3306
 Database username               :  drupal8
 Database name                   :  drupal8
 Database                        :  Connected
 Drupal bootstrap                :  Successful
 Drupal user                     :
 Default theme                   :  bartik
 Administration theme            :  seven
 PHP configuration               :
 PHP OS                          :  Linux
 Drush script                    :  /usr/local/bin/drush
 Drush version                   :  8.3.2
 Drush temp directory            :  /tmp
 Drush configuration             :
 Drush alias files               :
 Install profile                 :  standard
 Drupal root                     :  /app
 Drupal Settings File            :  sites/default/settings.php
 Site path                       :  sites/default
 File directory path             :  sites/default/files
 Temporary file directory path   :  /tmp
 Sync config path                :  sites/default/files/config_XGF3iTl5ukT-2H38IGr2FJvhbRslVnI8I-jN14TKmn5OucLACAs4YPLbljJMuZWZ4UDDPeu5TA/sync

So the guidance that I'm following says:

Drush searches for commandfiles in the following locations:

  • Folders listed in the 'include' option (see drush topic docs-configuration).
  • The system-wide Drush commands folder, e.g. /usr/share/drush/commands
  • The ".drush" folder in the user's HOME folder.
  • /drush and /sites/all/drush in the current Drupal installation
  • All enabled modules in the current Drupal installation

Unfortunately, none of these locations appear to be exposed in my host (workstation), and are only available from inside Lando's PHP container, but it does appear that my host's ~/GitHub/lando-d8/sites folder might be visible within the container. So I'm going to try adding a path of /all/drush to that folder and see if the system will pick up the files that I park there.

cd ~/GitHub/lando-d8/sites
mkdir -p all/drush

Next, I retrieved a copy of sandwich.drush.inc to use as an example, and I parked it on my host at ~/GitHub/lando-d8/sites/all/drush/sanwich.drush.inc. I followed that with:

╭─mark@Marks-Mac-Mini ~/GitHub/lando-d8 ‹8.6.x*›
╰─$ lando drush mmas
The drush command 'mmas' could not be found.  Run `drush cache-clear drush` to clear the commandfile cache if you have installed new extensions.                           [error]
╭─mark@Marks-Mac-Mini ~/GitHub/lando-d8 ‹8.6.x*›
╰─$ lando drush cc drush
'drush' cache was cleared.                                   [success]
╭─mark@Marks-Mac-Mini ~/GitHub/lando-d8 ‹8.6.x*›
╰─$ lando drush mmas
What? Make your own sandwich.                                [error]

In spite of the [error] return, the command clearly works!

Making My "Sandwich"

So, the next logical step is to replace the "logic" of make-me-a-sandwich, with something more useful, namely mods-via-twig.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment