Posts in the “drupal” category

A Drupal 7 SQL cheat sheet

I'm currently developing a new Drupal 7 application (technically built as a new Drupal module), and as I'm building the application, I've made notes on database queries I've made.

The approach I'm taking is to use the following Drupal SQL query functions. As you can see from the function names, there are now different Drupal functions for each query type:

[toc hidden:1]

How to upgrade a Drupal 6 website to Drupal 8 (how to try)

I’m currently trying to upgrade a Drupal 6 website to Drupal 8, but the process has been slow-going so far. I’ll explain the problems and workarounds in this article as I learn them.

So far I have been able to get a Drupal 6 website with 7,000+ nodes and 18,000+ URL aliases converted to the point that I can now see all of the old blog posts on the Drupal 8 site, and I’ve also started theming the Drupal 8 website.

[toc hidden:1]

A way to migrate Drupal 6 Photo content type to Drupal 8

The main script in this project — MigrateAllD6Photos.php — now seems to be working. The code is ugly and doesn't check for SQL errors, but for my purposes it seems to be working okay. To get that script to work and migrate your Drupal 6 photo content type to a Drupal 8 photo content type, you also need to run a couple of queries shown here.

[toc hidden:1]

Notes on how to update a Drupal 8 website

As a “note to self,” I just updated this Drupal 8 website in less than three minutes. Actually, what I did was (a) test the Drupal update on a Test server, and then (b) did the update here in less than three minutes.

The following block shows my Cliffnotes on performing the update. If you’re familiar with Drupal, these notes may make sense, and help you when you need to perform an update of your own.

UPDATE: Once you get to a certain level of Drupal — I don’t remember the exact version but somewhere around 8.43 or 8.5 — you shouldn’t use Drush to update your website any more, you should use Composer. I write about this in my article, A Drush 9 list of commands for Drupal 8.

Update Drupal 8.1.3 to 8.1.8 (Test Server)

Update Drupal 8.1.3 to 8.1.8 (on the test server)
=================================================

# create a new database on my test server
mysql -u root -p
create database aad8prod;
use aad8prod;

# create a mysql user and grant permissions
GRANT ALL PRIVILEGES 
ON aad8prod.* 
TO 'prod_user'@'localhost'
IDENTIFIED BY 'prod_password' 
WITH GRANT OPTION;


# restore the production database to my test server
source aa.com.sql.20160814;


# update the drupal8 config with the new username and password
# sites/default/settings.php
$databases['default']['default'] = array (
  'database' => 'aad8prod',
  'username' => 'prod_user',
  'password' => 'prod_password',
  'prefix' => '',
  'host' => 'localhost',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);


# Test server notes

- had to disable the 'trusted_host_patterns' in settings.php
- drupal error message was:
  The provided host name is not valid for this server.
- made sure everything is working on the test server
- i turn off my wifi to make sure i don't accidentally do
  something on the prod server

Performing the Drupal Update (Test server)


Performing the Drupal Update (Test server)
------------------------------------------

# download latest drupal version (8.1.8)

- unpack it
- follow the instructions in /core/UPDATE.txt:
  - MINOR AND PATCH VERSION UPDATES
  - if needed, make a backup copy of your settings.php file
  - backup .htaccess and robots.txt
      - cp settings.php settings.php.pre20160814  (sites/default)
      - cp .htaccess .htaccess.pre20160814
      - cp robots.txt robots.txt.pre20160814
  - unpack drupal update files somewhere so you can quickly and easily
    copy them after you remove the old files
        - in this case i put mine in:
            - ../drupal-8.1.8
  - take the website offline
      - admin/config/development/maintenance
  - cd to the drupal8 root directory
  - `drush cr`
      - my cache database tables are several GB; flush them
  - rm -rf core vendor
  - rm *php
  - rm composer*
  - rm web.config
  - unpack new files
      - cp -R ../drupal-8.1.8/* ../drupal-8.1.8/.htaccess .
  - run update.php via browser
      - http://aad8:8888/update.php
      - this shows an update page with no css
          - 'tmp' directory on Test server was messed up
  - check that everything is working
  - go back online
      - admin/config/development/maintenance

Potential Test Server problems


"Couldn't write file" error message (Test server)
-------------------------------------------------

- looked in reports
- may have to do with:
    - couldn't write .htaccess file
    - couldn't use temporary directory b/c it was looking for
      the temp directory i use in production
    - change the temporary directory at this uri:
        - admin/config/media/file-system
            - change to: /Applications/MAMP/htdocs/aad8-drupal-tmp
    - the css/html is screwed up on this page:
        - admin/reports/dblog
        - `drush cr` fixes this

Production Server


Production Server
=================

- after i confirmed that the update didn't clobber my website,
    i followed the same steps on the Production server
- investigate the differences between these files:
    - .htaccess
    - robots.txt

Those notes are cryptic, but if you need an example of the steps needed to update a Drupal 8 website from one minor version to another (in just a few minutes on the Production server), I hope they are helpful.

Drupal 8: How to write a simple custom “block module”

In this tutorial I’ll demonstrate how to write a simple Drupal 8 “block module.” By this I mean that I’ll show you how to write a simple Drupal 8 module that will display output in a block. When you’re done you will have created a new block that you can place in one or more theme regions.

Surviving the Drupal learning curve (learning cliff)

Learning Drupal: I read someone recently refer to the Drupal learning curve as the "Drupal learning cliff", which is actually very appropriate. While a content management system like Wordpress seems to come out of the box ready to go, even with Drupal 7 you need to add your own WYSIWYG HTML editor, Media plugins, page title modules, and more, just to get a basic Drupal website off the ground. Things like this leave a new Drupal user scratching their head and asking "Why?"

How to change a Drupal 6 user password using mysql or phpmyadmin

Drupal 6 FAQ: How can I change the password of a Drupal user in the database without using the Drupal UI? That is, how can I change a user's password using a database query (such as the need to change the Drupal User 1 password)?

If you ever want/need to change a Drupal 6 user password in your database using the MySQL command line or a tool like PhpMyAdmin, a SQL query like this will do the trick:

My Static Drupal app: CPU usage drops by more than 50%

After switching from Drupal 8 to my Static Drupal app — which generates static HTML pages from a Drupal 8 database — the CPU use on this website has dropped from an average of 22% CPU use down to 9.2%. The max CPU use has also dropped from a whopping 59% down to 22.5%.

After ten years of Drupal ...

After ten years of this site running on Drupal, it is now delivering static web pages to you. More news after I get some sleep. :)

Notes on how to update a Drupal 8 website with Drush and Composer

The process of updating a Drupal 8 website changed dramatically back in March-April, 2018, so I deleted the older content on this page and replaced it with the content below.

As of April, 2018 you now need to use Composer along with Drush to update your website. Here are my very brief notes on how to do this:

# note to self: use your `drush8` alias rather than `drush`

- Backup your website files
- Backup your Drupal/website configuration files
    ads.txt
    .htaccess
    robots.txt
    sites/default/settings.php
    composer.json
- Backup your database

- Clear the cache
  drush cr

- Put your site in maintenance mode
  drush sset system.maintenance_mode 1

- Do the actual update
  composer update drupal/core --with-dependencies
  drush updatedb

- Take the site out of maintenance mode
  drush sset system.maintenance_mode 0

- Clear the cache again
  drush cr

As one note about this, the process of updating from Drupal 8.5.1 to Drupal 8.5.2 failed with this error:

OUTPUT: Package "drupal/core" listed for update is not installed. Ignoring.

I found the solution to the problem at this SO page. Suffice it to say, if you plan to keep working with Drupal 8, you’ll want to learn about how Composer works.

Installing a Drupal security update with Composer

Today (August 1, 2018) I had a problem trying to get Composer to update my website from Drupal 8.5.5 to Drupal 8.5.6, where 8.5.6 is a security update. This command did not work:

composer update drupal/core --with-dependencies

But I found that this command did work:

composer update drupal/core --with-all-dependencies

Update: I had that same problem on March 20, 2019, with the Drupal 8.6.13 security update, and once again the --with-all-dependencies flag solved the Drupal core update problem.

Here’s some information on the differences between --with-dependencies versus --with-all-dependencies, from the Composer documentation:

--with-dependencies: Add also dependencies of whitelisted packages to the whitelist,
                     except those that are root requirements.
--with-all-dependencies: Add also all dependencies of whitelisted packages to the whitelist,
                     including those that are root requirements.
                     ------------------------------------------

This Debug Academy page helped me find that solution. If you need to update Drupal core I hope that information helps.

Drupal/Composer/Drush FAQs

I thought I’d put some Drupal/Composer/Drush FAQs here so I don’t have to keep searching for them.

Q: From this page: What does it mean when `composer outdated` shows components that need updating, but the `composer update` command indicates that nothing needs to be updated?

A: Nothing. In composer.json you can limit which versions you accept for a dependency. Drupal core specifies Symfony 2.8.*, so even though there are newer versions of Symfony, Composer keeps you on 2.8 because that's what core wants.

Q: How do I make a database backup with Drush?

A: Run drush cr, then this:
drush sql-dump > 2018-10-21.sql

Q: What is Composer, and how does it work with Drush 9?

A: Composer is a dependency manager for PHP. Drupal core uses Composer to manage core dependencies like Symfony components and Guzzle. Drush 9 no longer supports updating Drupal and leaves the work to Composer.

Q: What is the syntax for adding entries to the composer.json file?

A: You can add entries to the composer.json file manually, but I’ve found it easiest to add entries to that file using commands like these at the command line:

composer require 'drupal/token:^1.5'
composer require 'drupal/metatag:^1.7'

Lately I’ve been able to find these commands on Drupal module release pages, like this Token release page.

How to update Drupal modules with Composer, or manually

In theory, you can update Drupal 8 modules with Composer using either of these two commands:

composer update drupal/metatag --with-dependencies
composer require 'drupal/metatag:^1.8'

In practice, those commands didn’t work today (Feb. 21, 2019), so I just wrote a little post about how to update Drupal 8 modules manually from the command line.

Notes from January, 2020

Here are a couple of links from January, 2020, related to updating from Drupal 8.6 to 8.8.1:

And a note on how to update Drush with Composer:

composer update drush/drush

Related links

Best thing I’ve done to eliminate comments spam in Drupal 8

After trying a lot of different anti-spam modules with Drupal 8, the best thing I’ve ever done to reduce comment spam is to go to the Drupal admin /admin/structure/types/manage/blog/fields URI, click Edit on the Comments field, and then select the “Anonymous posters may not enter their contact information.” Since I chose that option two days ago I’ve only had one spammy comment show up in my approval list. That field gave spammers a simple way to enter a URL, and without it, the spam seems to have dried up.

Disabling Drupal 8 page caching significantly increases CPU use

As I noted five days ago, Mollom went out of business so I had to switch this Drupal 8 website to use a different module to control comment spam. I ended up adding two modules that work together, but the side effect of using their best features is that they end up disabling Drupal 8 page caching. This image clearly shows the result that disabling page caching has on CPU use.

Drupal 8 anti-spam modules, caching, and performance

I’m not sure why, but on April 3, 2018, the people behind the Mollom anti-spam module for Drupal basically went out of business. This meant that I either had to disable comments on this site (which I did for a while), or look at other anti-spam modules, which I did over the weekend.

A bad part of two other popular Drupal anti-spam modules (Honeypot and reCAPTCHA) is that if you use them, the result is that they disable caching on your website. (In the case of Honeypot it only disables caching if you use its time-related feature.)

The image here shows a performance effect of using these modules. I turned on these modules at ~17:00 hours today (which is really 1pm, my time), and there was an immediate spike in CPU use. I assume that web pages are also being served slower, and both of these performance side effects are not good for the website business.

There’s one other Drupal anti-spam module I can try that doesn’t disable web page caching, so I’ll take a look at it soon. If that doesn’t work well I’ll either have to turn off the comments again, or write my own module that doesn’t affect caching.

Drupal URL/URI - How to get the URL/URI of the current Drupal page

Drupal FAQ: How do I get the URI (or URL) of the current web page from my Drupal theme?

Sometimes when you're working on a Drupal theme, or customizing an existing Drupal theme, you'll want to know the URL (or more likely, the URI) of the page that the user is currently looking at so you can customize that page (or more likely, a series of pages beginning with a certain URI pattern, as with this Custom Drupal PHP block example).

Mollom is out of business

On April 3, 2018 this website suddenly got a ton of spam comments. Fortunately I caught the probably very quickly, and turned off the ability for people to post comments here. When I checked into the problem I found that Mollom — created by the same person who created Drupal — basically went out of business on April 2nd. (I’m sure there was some warning about this decision, but I sure didn’t get it.)

I’m often surprised when people who offer a free service shut down that service without asking a simple question: “Would you be willing to pay for this service, and if so, how much?”

(I’ll re-enable the ability to post comments here when I find a good replacement for Mollom.)