Posts in the “drupal” category

Drupal 8 website not showing images on image/photo nodes

If you have a Drupal 8 website and your images are not showing up on your image/photo nodes, one possible problem (which I just learned) is that your Drupal 8 theme needs to refer to {{ content }} in node.html.twig and not {{ content.body }}.

I don't know why you have to do that, but after four hours of troubleshooting the problem with my own Drupal 8 website/theme, I can confirm that if you don't do it, your images/photos won't be shown on their nodes. (All I was trying to do was to separate content.body from content.comment, and when I did that my images no longer showed up at their nodes/URLs.)

Drupal “Rewrite the output of this field” replacement patterns

As a quick note, if you’re looking at a Drupal form and it says you can use the "Rewrite the output of this field" replacement patterns shown (somewhere) on this page — and you can’t find those replacement patterns on that page — you can find a complete list of them at this drupal.org url.

As an example, if you’re working with a Drupal Node, you can use replacement patterns like these:

[node:author:name]
[node:content-type]
[node:content-type:name]

What are the Drupal 8 Twig template front page file-naming conventions?

Drupal FAQ: What are the Twig template file naming conventions for Drupal 8? Specifically, how do I need to name a template file so it will be used for the front page of my website?

In short, the Drupal 8 Twig “HTML” template file has this name:

html.html.twig

and if you want to create a custom HTML front page template, use this name:

html--front.html.twig

The same goes for “page” and “node” files, where the default page template file has this name:

What are the Drupal 8 Node class fields (field names)?

I was just trying to modify one of my Drupal 8 template files — node.html.twig — and I couldn’t find any good documentation for what variables/values/fields are in the Drupal 8 Node class, so I dumped some output to my browser, and saw that these are the Node fields:

Drupal mobile website/multisite installation and robots.txt

As I’m working on getting a mobile version of this site working, I ran into a problem with having a robots.txt file on a Drupal multisite installation. The root of the problem is that you need to have a robots.txt file like this on your mobile site:

User-agent: *
Disallow: /

That’s to keep the search engines from scanning and storing that content, which will be a duplicate of your main website.

Drupal 6 image errors and solutions (ImageCache, ImageField, CCK)

After just creating another Drupal 6 website that uses ImageCache, ImageAPI, ImageToolkit, CCK, and ImageField, I thought I'd make some notes here about the problems I ran into when trying to use ImageField with CCK to let users upload images to my website. I encountered several very frustrating errors along the way, and here are my notes related to those errors.

Note: This blog post is about Drupal 6. Things have changed quite a bit in Drupal 7, and these tips are not likely to be helpful there.

Drupal APC performance improvements

Summary: A look at the performance improvements gained on a Drupal 6 website by using the PHP APC Opcode Cache.

Wow, I just installed the PHP APC Opcode Cache module on alvinalexander.com, and the results are really incredible. By any metric the website is performing much, much faster.

I ran two benchmarks on the website using the Apache 'ab' benchmarking tool, one without the APC opcode cache, and one with it.

Huge Drupal Boost module performance improvements (APC, Boost cache, Drupal 6/7)

Summary: This post provides a review of the Drupal Boost module, which dramatically improves website speed/performance.

Over the last few weeks I've been spending a little time every weekend trying to improve the performance of the alvinalexander.com website. I didn't think there was much of a performance problem with the website, but the Google Webmasters' site kept reporting that the site was slow compared to most internet websites.

Drupal website speed and performance tuning

Summary: A look at Drupal speed, performance, and scalability improvements recently made to the devdaily.com website.

I’ve read before that the secret to great Drupal performance is Cache, cache, cache,” and while I always knew that to be true, I didn’t know how true it was. After several recent tweaks to the devdaily.com website, I can say that is very true. (I documented this in detail in my Drupal Boost module performance article.)

A Drupal get user id function (uid)

Just a quick note here to share a Drupal get user id (uid) function. I've found that when I'm developing a Drupal module or Drupal application, I often need to access the user id, so I created this simple function and include it in a common utilities module:

# return the current drupal user id (uid)
function get_user_id() {
  global $user;
  return $user->uid;
}

As you can see, the code accesses the global Drupal user object ($user), then gets the uid from the user object.

Handling Drupal SQL exceptions (db_insert, db_update, db_delete)

While developing a Drupal module, I just ran into a situation where it may be common for users to generate SQL exceptions. I have a 'unique' limit on several of my database tables, essentially saying that certain name fields must be unique for the current project. As you can imagine, it's extremely easy to enter a duplicate name, and while doing a SQL INSERT that can easily lead to a SQL exception.

As a result, I dug around and found a way to handle SQL exceptions in Drupal queries. My current problem is with a Drupal 7 db_insert query, and I handled it like this:

Drupal form default input focus

Drupal form FAQ: How do I get default input focus on a Drupal form field (presumably a form textfield)?

There may be a better way to do this, but here's what I just did to get default input focus on a series of Drupal form 'add' and 'edit' pages. I just included the following Drupal form suffix definition on any form where I wanted to control the default input focus:

Drupal 7 - A Rails-like approach for Drupal form and module development

Looking for a Ruby on Rails like approach for Drupal 7 form and module development? If so, I may have just the tool you're looking for.

As you can see in the video below, I show how I can generate Add, Edit, Delete, and List forms for a Drupal 7 project I'm working on in four minutes, or one minute per form. Of course there's a little more work to be done, but the code-generating approach I've taken is exactly what Ruby on Rails and CakePHP did the last time I used them.

A Drupal form states dropdown field (list of states) example

I was just working on a Drupal form, and I wanted to include a list of states (the United States) in a dropdown field (an HTML select/options field). Knowing that I'll need this list of states for other Drupal applications, I decided to put the "states" code in a separate file.

To that end I put this list of states in a file named states.inc, in the same directory with the rest of my Drupal module code:

Drupal developer modules

Once you get into serious Drupal module development, you'll realize that it would be really helpful if you could do several things during the module development process, including clearing the cache, rebuilding the menus, error logging (logging errors to a file, in particular), and so on. Fortunately, with Drupal's really large developer ecosystem, other Drupal programmers have already created some great modules to help you along in the coding process.

Here's a list of the Drupal developer modules I currently use, or have used.

Drupal form and module debugging

Drupal debugging FAQ: How can I debug my Drupal form and module development process?

I should know more about this over the next few weeks, but if you're developing Drupal forms and modules, I've found the PHP error_log function and the Drupal watchdog function to be very helpful. They both let you log errors 'somewhere', and the error_log output shows up in your Apache logs, and the watchdog output shows up in your Drupal reports.

Here's how you call the PHP error log function:

Drupal CCK form field - US states list

If you're ever creating a Drupal form using the CCK module and need to show a list of states (the United States) in a combo box (also known as a "select list" or "drop down" field), you'll want to have that list of states in the right format.

Fortunately (for you) I just ran into this problem, and created two different versions of CCK form state fields. This first one displays the full name of the state, and stores the two-digit state code in the database:

Drupal - How to redirect a user when altering an existing form

Drupal form FAQ: How do I redirect a user when they submit a form, and I'm writing a hook_form_alter function to alter that form?

In Drupal 6 you set the "$form['#redirect']" property in your hook_form_alter function when you alter the form, like this:

$form['#redirect'] = 'thank-you';

That syntax tells Drupal to redirect the user to the "/thank-you" URI on your website.