Drupal 7 Quick Tip: Hiding Content Types on the Add Content Page


In Drupal 7, hiding contents on the Add content page (node/add) is pretty easy.

Go to Admin > Structure > Menus, and choose the Navigation menu.

You should see a page such as this one:

From here you can simply disable the content type(s) that you don't want to appear on that page, by unchecking the checkbox under Enabled.

Your node/add page will now be limited to the content types you have Enabled.

Drupal Quick Tip: Update Every Site in a Multisite Installation with Drush


This is one of those situations where I feel like I've been walking around with toilet paper hanging off the leg of my pants but nobody has the heart to tell me.

This evening I decided to, lo and behold, update my personal sites. This is nothing to take lightly. I may strongly recommend that sites be updated with security releases as soon as possible to clients, co-workers and bosses, but let's be honest here. Many a developer have put their own personal sites at the bottom of their never ending to-do lists as I have, because who has the time?

My Drupal 7 multisite consists of a variety of somewhat random websites, of which I will avoid sharing at this moment. Nevertheless, it was in need of an update-- desperately and I have grown tired of the warning at the top of the page every time I log into it.

Here I go, cd'ing happily into my multisite's directory and type drush. Woo hoo! I actually installed drush once upon a time-- I'm a happy camper. Next up: drush pm-upgrade. Oops. I'm told, "The drush command 'pm-upgrade' could not be found."

This reminds me that every. single. time I create a symlink I get the syntax backwards. And every. single. time. I try to update core or modules of a Drupal site I replace pm-update with pm-upgrade. This reminds me of the saying, "Practice makes perfect," when in reality it should be, "Perfect practice makes perfect."

After typing the proper command (which is pm-update if you were skimming), I'm prompted to hit y or n a few times and away the update churns away giving me a lovely status I completely ignore, only for me to log into one of the multisites and my heart sinks. It wasn't updated at all!

Oh right, the whole multisite thing. It only hits the default site by... well, default. This is where, after all my long winded ranting, comes in this handy dandy command which I was forced to Google in hopes such a thing existed.

drush @sites pm-update

Ooooh yeah, we're talking now. You can @sites quite a few things which makes managing a Drupal multisite with drush a much more pleasant evening.

Get to those updates!

Working with Arrays and Objects in Drupal with print_r() and dpm()


When customizing things in Drupal, whether we like it or not, there often comes the need to dive in and alter or override things. Because of Drupal's API, we're given a lot of wonderful hooks and theme functions to take advantage of without touching Drupal core. Instead we can modify most things the way we want within our own custom module or theme, without ever having to worry about our changes getting overwritten when doing an update to our web site.

Great! There's plenty of tutorials, blog posts, forum posts and issues both on and throughout the interwebs on how to work with data in order to bend it to our wills, but it's not always clear on how exactly to work with the data in the first place.

Understanding print_r()

PHP provides us with a handy (if not a bit "dirty") function called print_r. What it does is print readable information about PHP objects and arrays that assist with debugging and figuring out what value we're trying to target. Here is a partial output of me doing a print_r($form) in a hook_form_alter of a blog post. What we need to understand is I am printing the full output of $form, which is an array. Each piece of the array contains a key and a value, where the value can be another array, an object, or a string.

Take a look at the screenshot above. When I am manipulating this form, I need to target the keys of the array(s) in order to manipulate any of the values. Each indent shows us the tree. We know that this is printing out $form. In order to target any of the keys within that, we need to use $form['nid'] or $form['type'] or $form['uid'], and deeper than that is $form['nid']['#type'], $form['type']['#value'], or $form['uid']['#value'].

Now we understand that arrays are handled with brackets: [ ]

But PHP objects are a little different. Below is a screenshot of another part of the $form array I posted above. This portion is accessed with $form['#node']. You'll notice instead of Array, it says stdClass Object. This means we're working with an object, not an array. Let's ignore what the differences are right now, and just figure out how we manipulate that data.

Arrays are targetted with brackets. Objects are targetted with ->.

Homework: A few other useful PHP functions are var_dump() and var_export().

Using dpm()

The Devel module takes this a bit further and provides us with dpm(). This gives us output in a much more readable format, though the concept is the same.

With this, you can now use many of Drupal's great hooks to override various content-- take a look at my previous blog posts, Drupal Alters and Overrides: hook_form_alter and Drupal Alters and Overrides: hook_menu_alter.

Drupal Alters and Overrides: hook_menu_alter


This brief tutorial will cover altering and removing items in Drupal's menu system. See my previous blog post on hook_form_alter.


Introduced in Drupal 6, hook_menu_alter allows you to actually alter elements in Drupal's menu system. The primary links, secondary links, tabs and page callbacks are alterable in this way. For example-- using the Content Profile module, it adds an extra tab to each user's page. If I choose the option to "Show a tab at the user's page", it will then display an extra tab when visiting that profile.

The problem with this is I find the tabs a bit unclear and want to rename them; instead of "View", "Edit" and "Profile", I want them to say "View", "Edit Account Settings", and "Edit Personal Information".

In my demo module, I'll add the following:

 * Implementation of hook_menu_alter().
function demo_menu_alter(&$items) {
  // Change the name of user profile tabs. 
  $items['user/%user_category/edit']['title'] = 'Edit Account Settings';
  $items['user/%user/profile/profile']['title'] = 'Edit Personal Information';

You can, of course, also remove menu items or tabs this way. You can find and target your specific menu item with a print_r($items) or dpm($items).

 * Implementation of hook_menu_alter().
function demo_menu_alter(&$items) {
  // Change the name of user profile tabs. 
  $items['user/%user/profile/profile']['title'] = 'Edit Personal Information';

In Drupal 6, you can also use the Tab Tamer module.

Image Uploads Using WYSIWYG and Media in Drupal 7


Modules you'll need:

Install the WYSIWYG as instructed with CKEditor support. Enable the Media module and the dependency on File Styles. Under Configuration > Content authoring > WYSIWYG, Edit the text format of your choice. Under the Buttons and Plugins fieldset, make sure you select the Media browser checkbox. Hit Save.

Under Configure > Content authoring > Text formats, and click configure under the text format of your choice. Select the Converts Media tags to Markup checkbox.

Now, under Limit allowed HTML tags fieldset, add the img tag.

When creating a post, you'll now have an Add Media button. Select the file you want to upload and click Submit.


Subscribe to RSS - blogs