drupal

Delete comments and disable commenting on all nodes of a particular content type

Please TEST these queries before running them on a live site! I do not want to be held responsible if something goes wrong ;)

Here is how a colleague explained his situation (the solution follows):

The trap is that if you create a content type and don’t remember to set comments off, all the nodes you create have to be updated manually to turn it off, even after you change the default in the content type settings. In our case, I imported a ton of records, and only later realized comments were on.

Cache-related WSOD! HELP!

Well, you've probably all had a White Screen Of Death a time or two while dealing with Drupal or PHP. They can be very frustrating, but are usually easy enough to resolve. The following illustrates how to recover from a Drupal WSOD as a result of making changes to your cache settings in the Site Configuration -> Performance admin page. Again, this "fix" should only be applied if you have issues that you know are related to recently altering the cache/aggregation/compression settings.

User Access Example #1 - A Mostly-Private Site

We recently completed a site that offers only a small amount of anonymously-accessible content. I want to share the setup with you, as I feel it's pretty effective. The basic structure is as follows:

  1. Anonymous users can only see some pages (the ONLY content type they can ever see is Page; though this could change)
  2. Authenticated users can see most content, across most content types
  3. Client Admin users can see/edit everything
  4. If an anonymous user gets an "Access Denied" page, redirect them to the user login page, but maintain the original destination

Changing the Title of a Node Add/Edit Page

The following example shows one way of changing the title on add/edit pages for a specified content type. We'll accomplish this using hook_form_alter() in a custom module.

Hiding Fieldsets in a Form using hook_form_alter()

The following examples shows how to hide fields/fieldsets in forms using hook_form_alter(). I suggest using the devel module to determine how to target your field(s) or fieldset(s). You can add a dpm($form) to the function during development and testing.

Adding a Custom Token to the Ubercart Email Template(s)

In this post I'll show how to set up an extra token for use in an Ubercart template. This will require creating a custom module (because we don't really want to modify others' modules). We'll use a few different hooks to create the token, and then simply modify the template to include this token. This setup requires the token module (which is required by Ubercart), so make sure this is enabled!

Altering the basic properties of a Drupal textarea

The following example, when placed in your theme's template.php file, will shrink the size of all of the "body" textareas on your site to 5 rows, and set the textarea as resizable. The function we're using to alter the textareas is theme_textarea().

Custom Image Link in a Drupal View

The following code can be used in a views_customfield php field to create a custom image link. This example also shows how to use the db_query() function with a proper %-modifier.

Custom views image link

Using module_list() to Show Active Drupal Modules

This code can be used in a custom module, a template.php file, or most easily in a block. This looks good in the header bar on a demo site, without a title, as you can see in the screenshot! The reason I am using this is because I do a lot of Drupal demos and it's great to have the active modules showing so there is no question of what's required to do whatever it is I am showing. The list of modules is ordered by weight, then filename, which makes it easy to determine if your custom modules are running at the right times.

Showing active modules in a block

Custom Voting API Calculation

The Voting API is really nice when used in conjunction with a module like Fivestar. It takes all of the complexity out of setting up a voting/rating system. On occasion, however, you need it to do some things that aren't built in. Using the VotingAPI's API, I'll show you how to trigger a function (send an email, write a message to the screen, etc.) when a piece of content receives a fifth vote of four stars or greater. That is, a node may have four votes of five stars, and two votes of two stars. As soon as the next greater-than-four vote goes in, we want to do something about it. In our case, the client wants to receive an email when a node is popular, and has had five four-star-or-greater votes.

VotingAPI table
VotingAPI cache table