-
Snippet: MySQL table sizes ordered by largest to smallest (in MB and Row Count)
Show All Tables By Size in MB
1SELECT table_name AS "Tables", ROUND(((data_length + index_length) / 1024 / 1024), 2) "Size in MB" FROM information_schema.TABLES WHERE table_schema = DATABASE() ORDER BY (data_length + index_length) DESC;Show All Tables By Size in MB (if > 1 MB)
1SELECT table_name AS "Tables", ROUND(((data_length + index_length) / 1024 / 1024), 2) "Size in MB" FROM information_schema.TABLES WHERE table_schema = DATABASE() AND (data_length + index_length) > 1048576 ORDER BY (data_length + index_length) DESC;Show All Tables By Number of Rows
1SELECT table_name, table_rows FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = DATABASE() ORDER BY table_rows DESC; -
Using Drupal 8 Persistent Login Module on Platform.sh
The Persistent Login module relies an extra cookie to maintain a Drupal user’s session. The configuration screen for this module lets you specify a prefix for the cookie. The default is “PL”. If you are using the site on HTTPS, the module prepends an “S” to the cookie it creates. Here’s an example cookie name: SPLfa0650d6d985433d455a3b15cc70fd9b .Platform.sh lets you configure cookie cache settings via routes.yaml .If you’re using Persistent Login you must tell the system not to ignore this cookie.Here’s a standard (and simple) routes.yaml file on a Drupal 8 site:
12345678"https://{all}/":type: upstreamupstream: "app:http"cache:enabled: true# Base the cache on the session cookie.# Ignore all other cookies.cookies: ['/^SS?ESS/', '/^Drupal.visitor/']
12345678"https://{all}/":type: upstreamupstream: "app:http"cache:enabled: true# Base the cache on the session cookie and persistent login cookie.# Ignore all other cookies.cookies: ['/^SS?ESS/', '/^S?PL/', '/^Drupal.visitor/']There is a bit more discussion around how these cookies are handled here: https://www.drupal.org/node/2898065/discuss
-
Switch Audio Input/Output Device using Alfred
My top two must-have Mac apps are Keyboard Maestro and Alfred. If you know me, you’ve likely heard me say this, ad nauseum. I use these two apps countless times each day. Often I use them to run command line scripts via hotkey. This is one such implementation.
Recently I picked up a Jabra Evolve 65 headset. I keep it connected to my Mac via USB. Also, I have some speakers hooked up via the external speaker jack. You can only send output to one of these devices at a time (speakers OR headset). I desired a way to quick-switch between the two. I chose to use Alfred for this.
The steps below show how to create some quick switchers like this:
Requirements and Initial Setup
-
Overriding a Webform Confirmation Message in Drupal 8
There are a few reasons you may need to override a webform confirmation:
- Out of the box webform doesn’t allow you to add some tags (e.g., <button>) to a webform confirmation message. When you save it strips them out.
- What if you needed to alter the webform message based on the values submitted in the webform?
- What if you needed to alter the webform message based on where the user saw the webform?
- etc…
Thankfully, you can use a standard preprocess hook to override the confirmation output.
In this particular example I needed to override the output of the message to include a button tag. I needed this to only happen if specific GET params had specific values.
First, I modified the confirmation message settings as shown here:
-
Using Lazy Builders and Auto Placeholders in Drupal 8
Introduction
I’ve been working on a site that features a lot of user-specific customization and output. The site offers workshops (courses) using the Opigno LMS for Drupal 8. The workshops are rendered in a few different ways throughout the site. For the most part, the rendered workshops appear the same for all users. Here’s an example of a “card” view mode for a workshop:
If a user has successfully completed a workshop, a special badge will appear on the card view mode. Also, the card will be highlighted:
There are two parts of the workshop card template that have to change based on the user viewing the card:
-
NightwatchJS Command to Repeat a Keystroke
Here’s a simple custom command that lets you repeat a specific key (single or combo) N number of times.
In the Drupal world you want to place this in your “Commands” directory (as defined in DRUPAL_NIGHTWATCH_SEARCH_DIRECTORY in web/core/.env).
You’d want to change “sitename” to be your site’s name (so it feels namespaced).
/tests/Nightwatch/Commands/sitenameRepeatKeystroke.js
123456789101112131415161718192021222324252627282930313233/*** Repeats a keystroke (can be multiple keys) a specific number of times.** @param {array} keys* Array of keys to send.* Example 1: [browser.Keys.TAB]* Example 2: [browser.Keys.SHIFT, browser.Keys.TAB, browser.Keys.NULL]);* (it's important to terminate modifier keys with NULL)* @param {integer} times* Number of times to repeat the key send.* @param {integer} pauseBetween* Number of milliseconds to pause between each key send (100 ms default).* @param {function} callback* A callback which will be called.* @return {object}* The 'browser' object.*/exports.command = function myCommand(keys, times, pauseBetween = 100, callback) {const self = this;for (let i = 0; i < times; i++) {this.keys(keys);if (pauseBetween) {this.pause(pauseBetween);}}if (typeof callback === 'function') {callback.call(self);}return this;};Using this in /tests/Nightwatch/Tests/misc.js with a 1000ms pause between each keystroke:
12345678910111213/* eslint-disable no-unused-expressions */module.exports = {'@tags': ['mysite', 'learn-page', 'accessibility'],'When I hit shift-tab 9 times on the Learn page my logo becomes the active element': browser => {browser.drupalRelativeURL('/learn');browser.sitenameRepeatKeystroke([browser.Keys.SHIFT, browser.Keys.TAB, browser.Keys.NULL],9,1000,);},}; -
Monitoring Which Element Has Focus in a Web Browser
While building accessibility-minded websites, it’s useful to be able to monitor which element has focus in your browser.
Using a screen reader works well, but there may be an easier solution for those who aren’t used to screen readers.
Chrome makes this pretty simple. See https://developers.google.com/web/tools/chrome-devtools/accessibility/focus.
In Firefox, Safari, Chrome, and other browsers I’m able to achieve similar functionality (maybe even better) by running a simple snippet in the web developer tools console.
New lines appear as you move through the elements with your keyboard (or mouse).
You may want to turn this into a one-liner for easier pasting.
1234567var lastElement = document.activeElement;setInterval(function() {if (lastElement != document.activeElement) {lastElement = document.activeElement;console.log(document.activeElement);}}, 100);Output in Firefox:
-
Faster Debugging with Lando and xdebug
I’ve been struggling with the speed (or lack thereof) of Lando for Drupal sites. I have suspected xdebug was part of the issue. Sure enough, disabling xdebug speeds up the lando instance considerably. Re-enabling it slows it right back down again. I use xdebug often, so having to lando rebuild to turn xdebug on and off is not an option. I wondered if there’s a way to leave the extension enabled but only use it when needed.
While researching config directives like xdebug.remote_enable and xdebug.remote_autostart, I came across this issue thread on the github Lando project: Allow faster xdebug on/off toggling
The title sounds promising, right?
-
Quickly Protecting a Few Nodes from Deletion in Drupal 8
I’m working on a Drupal 8 site with some critical pages for which we do not want to allow deletion. The homepage, for example, is a basic page; we do not want to allow anyone (even UID #1) to delete this page. If there comes a time where UID #1 decides they need to, they’ll have to update this simple code to support that. We need not make it any more complicated than that for this particular project.
Here’s a solution that:
- shows nothing but an error on the “Delete” form for specific nodes (works for all users)
- prevents deletion by any means (works for all users except UID #1)
- why except UID #1? because hook_node_access doesn’t run for this user
-
Testing Tokens with Entity Ref Traversal via Drush
I am working on a project that has some complicated depth. I needed a way to quickly test token replacement. It’s pretty simple when you understand a few things.
Example Architecture
Here’s the Drupal architecture for this example:
- Discussion Group (content type)
- Title
- Intro
- Body
- “Associated Experts” paragraph field (allows one “Associated Experts” paragraph item)
- Associated Experts (paragraph type, used by many content types like Discussion Group above)
- Heading
- Intro
- Experts (allows unlimited number of “Expert” nodes)
- Expert (content type)
- Title
- Photo
- Discussion Group (content type)