Drupal

Displaying 1 - 10 of 10

Solving Error: Cannot use * as String because 'String' is a special class name

If you've ever seen an error message in your PHP 7 website about 'String' being a special class name, e.g. Error: Cannot use Drupal\Component\Utility\String as String because 'String' is a special class name in /Users/jesus/drush/commands/core/watchdog.drush.inc, line 4, the reason is that String is now a reserved class name as of PHP 7. So your code that worked fine on PHP 5.7 and earlier will now be broken. This affects Drupal but could affect any old PHP site with a class called String.

This is a problem with Drupal 8 which moved to an OOP-everywhere approach, so...

Drupal 8: Get the current Node's NID

Theming Drupal can be like working with layers of an onion. There are layers and layers and sometimes cutting through makes you cry. Besides not knowing what theme function or template file one must use (Drupal 8 helps with the knowing part with html comments) there is the big problem of global state and context, and the theme layer you're trying to use might be missing some context. The major example is that you want some block to be aware of the node on the page. (This is a problem which React.js helps deal with by passing state from parent to child component via props.)

But...

Solve mysterious Drupal initial page load delay with cron

Sometimes you'll visit your Drupal site in the morning, or after logging in as admin for the first time in awhile, and the page will take forever to load, more than 30 seconds, even longer. (First, if you're using Cloudflare CDN, it could be a problem with their servers not sending the end of the page html when it's finished and thus causing the browser to hang for a long time.)

The main cause of this kind of page load lag is Drupal's cron task! There are two ways that Drupal will run cron. The first is when you trigger it externally (by pinging the cron.php url,...

The .htaccess file in Drupal 7 / Drupal 8

An "htaccess" file is a configuration file read and used by the Apache web server to configure settings local to your website or even just a subdirectory in the website. These are local settings as opposed to what you would configure in .conf files in Apache's conf directory.

There should be a dot in the filename: .htaccess - and so the file will often be hidden or invisible in directory listings unless you specify that hidden or system files should appear. But the file should exist in any Drupal installation in the top-most directory of Drupal (next to the README and...

Installing Drupal 8 (with differences between Drupal 7) on your web host

Drupal 8 is out and many of the necessary modules have been ported to version 8. Drupal 8 is quite different under the hood from Drupal 7. But it's also fun to play with straight out of the box so let's spin up a new Drupal 8 demo installation. All of this assumes you have shell access, even if you don't normally use the command line, but should be applicable whether ssh'ed to a remote Linux server or testing locally on your Mac OS X laptop. To install Drupal 8 then:

  1. Download Drupal 8 (.tar.gz) and untar it in your web root. (As of publication, the most recent version is

  2. ...

Drupal SEO in 2016: URLs

SEO is a moving target. For blackhat SEOs, it's a constant back and forth cat-and-mouse game between them and Google. And the potential penalties by Google against sites employing blackhat tactics should be enough to encourage you as a site owner to focus your efforts on whitehat SEO. Drupal has a lot of tools which can help site builders format their content to be easily understood and categorized by search engines. And one of those factors which Google uses is the page's URL.

So it's important to pay attention to a few things related to URLs in your Drupal config.

  1. ...

How to Use Drupal 7 Views to Count Content

Here's how to quickly use Views UI to count content in your Drupal database. You can then display the counts as Blocks or even pages or just use them internally as site admin. The only prerequisite is to have Views and VIews UI enabled.

  1. Create a new view of content (aka nodes). Leave out sorting! (Sorting/ordering won't be visible in the count anyways but can prevent counting from working.)
  2. Under Advanced settings, enable Use aggregation to Yes.
  3. Under FIELDS, only use one main field, such as a title or nid. There should
  4. ...

Querying the database in Drupal 7 using db_select

Drupal, unlike plain old blog software, gives you the ability to create custom content object types with backing storage in the database as custom tables, doing all this without requiring any programming knowledge. When you create a new custom Content Type and add fields to it, in effect, you are creating a new database table for those fields.

Drupal also gives you ways to access and query the data without writing code. The most powerful form of this is Views, which every Drupal site should install as soon as starting. In the Views UI, you can select which fields (database columns...

Adding Facebook sharing/liking buttons to Drupal 7 content

Drupal 7 Facebook Sharing Widget Module Decision Workflow:

  1. Do you just want Facebook or do you want every social media service under the sun (Twitter, Pinterest, Reddit, Google+, LinkedIn, etc.). There are a number of smaller services which are like Facebook for a niche. Some countries have their own, especially China. Other major social networks like YouTube or Instagram aren't places to share general blog content.
  2. Do you want to add a Like button or a Share button? People tend to Like things more than they Share. Likes across Facebook are aggregated/added
  3. ...

Set up free hosting for Drupal 7 on OpenShift Online

If you're reading this, it means OpenShift is working. It means Drupal is set up using a free tier "gear" on RedHat's OpenShift PaaS. It's actually not completely easy for those of you used to a real VPS for hosting, and it's not any easier for those who are used to FTP-only shared hosting either. But you won't have to bother installing an OS, or installing Apache, PHP, and MySQL. On the other hand, you won't have much control over which version of those you run unless you code your own OpenShift "cartridges" which is much more work than simply installing packages on a server. ...