drupal 7.x

Drupal - Render a View in a Node Template

In this example, we have a content type called Unit (machine name: unit). And we want to programatically render a view on its node template. So we'll do something like this:

1. Create a copy of our theme's default node.tpl.php file and save it as node--[type].tpl.php file in your sites/all/themes/my_theme directory. In this example, a file name of node--unit.tpl.php is correct.

2. Render the view into a theme variable.

Drupal - Add a Directory to Your Site's Include Path

Say for example, one of your Drupal modules needed to access some PHP function located in a PHP file in a directory somewhere on your server that wasn't in your Drupal directory.

/my_custom_directory/my_custom_php_library/my_custom_php_library.php

You can make Drupal aware of that directory by adding it to your site's include path. In your settings.php file for your Drupal site, add something like this:

Drupal - Change The HTML Output of Field

So I have been working on migrating a giant website from Drupal 6 to Drupal 7. As I migrate the content into Drupal 7 and review it, I often refer back to the Drupal 6 node to make sure the content was migrated properly. I was getting annoyed with copying the Drupal 6 node id rendered on the Drupal 7 node's page, opening a new browser tab, bringing up the old domain and then appending /node/# to it. Instead, I wanted to change the way the field is rendered in Drupal 7 by providing a simple link back to the Drupal 6 page.

Drupal - Save MySQL Data to Cache then Load it from Cache

Say for example you wanted to grab a list of all of your Drupal Content Types with a Node Count for each Content Type and display it as an html item list. That is easy enough with your own custom module and a bit of MySQL. However, on a site with thousands upon thousands (even millions) of nodes, this operation can be a little expensive on the server.

Drupal - Get List of Fields for a Content Type

To get a list of fields for a content type in Drupal 7, try the field_info_instances function.

Here is an example usage:

$my_fields = field_info_instances("node", "my_custom_content_type");

From here, I'll typically use the dpm function from the Devel module to see what is inside:

Drupal 7 - How to Modify a Views Query with a Custom Module

To make custom alterations, changes, adjustments, etc to a Views query in Drupal 7, try this module (or add the code in the .module and .view.inc files to your own custom module):

my_module.info

name = My Module
description = Behold my awesome module.
core = 7.x
package = Other

my_module.module

Drupal - Get User Counts For Each User Role

Here is how to get a count of users for each user role in a Drupal 6 or 7 site:

SELECT
  COUNT(u.uid) AS user_count,
  r.name AS role 
FROM {users} u 
  INNER JOIN {users_roles} ur ON u.uid = ur.uid
  INNER JOIN {role} r ON ur.rid = r.rid
GROUP BY r.name
ORDER BY user_count DESC;

(Note, this isn't the 'correct' way to do it in Drupal 7, we should really use the new Database API layer, but I'm lazy right now.)

This will give you a table something like this:

Drupal - Get Node Counts For Each Content Type

Here is how to retrieve a count of nodes for each content type in a Drupal 6 or 7 site:

SELECT
  COUNT({node}.nid) AS node_count,
  {node_type}.type
FROM {node}
  INNER JOIN {node_type} ON {node}.type = {node_type}.type
GROUP BY {node_type}.type;

(Note, this isn't the 'correct' way to do this in Drupal 7, we should really use the new Database API Layer, but I'm lazy right now.)

This will give you a table something like this:

Drupal - Provide a Custom HTML String Variable to a Node Type Template File

To provide a custom php variable that contains an html string to your custom node type's template file, use some code sauce similar to this...

(Scroll down to the bottom for a Drupal 7 example)

Drupal 6

First in your theme's template.php file, add this (replace my_theme with the machine name of your theme):

Drupal - Send an E-mail with Custom Module Code

Here is how to send an e-mail with some custom code in your module and by utilizing hook_mail.

Add this example code snippet to your custom module where you would like to have an e-mail sent. For example, maybe you want to send an e-mail about a node when one of your module's forms are submitted.

Pages

Subscribe to RSS - drupal 7.x