Posts tagged with “drupal-planet”

February 09

Some test HTML for Drupal themes

It’s all too common that you hand a Drupal site off to a client, only to realize that the WYSIWYG threw in a common element (such as a blockquote perhaps) that should work look beautiful, but it looks like crap because you never tested it.

As an attempt to solve that, I gathered a crap-ton of markup to be used to test your Drupal theme. It includes both regular HTML elements (pretty much every one you can think of), as well as Drupal-specific stuff (such as tabs and status messages). The purpose of this markup is that you can copy and paste into a node and you have your very own markup tester.

Even though it’s just a single file, I went ahead and put it on GitHub with my other pet projects.

Posted 08:59 AM | 0 Comments | Tags:
January 28

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

Note: this post is a direct copy and paste from Agile Adam’s post. since that page started throwing a 404. Google’s cache still had it so I grabbed it and posted it here so it wouldn’t be lost forever. If the page comes back up or if Adam gets mad, holler and I’ll take this down.

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!

Before diving into this, please make sure you have a Ubercart system that sends emails when orders are placed. You should also have a test account from which you can place orders and receive emails.

1) Create a custom module

Just an empty modulename.module and modulename.info will do for now.

2) Create the Ubercart Template

Template files are in ubercart/uc_orders/templates. You must make a copy of the customer template and name it something unique (must end with .itpl.php). After doing so, you can choose this new template in Ubercart in the Order Settings page. The drop down there should show your new template. This setting is for the on-site invoice template viewing within the Drupal site. To set this template as the email template, you must also edit the conditional action in the Ubercart settings.

3) Add a hook_token_values() to your custom module

This code creates a token containing the last 4 digits of the credit card number used for an Ubercart order. I’ll leave it up to you to research any of the code used.

/**
 * Implementation of hook_token_values(). (token.module)
 */
function mymodule_token_values($type, $object = NULL){
    $values = array();
    switch($type){
    case 'order':
        $order = $object;
        if($order->payment_details['cc_number']){
            $values['order-cc-number'] = uc_credit_display_number($order->payment_details['cc_number']);
        }
        break;
    }
    return $values;
}

4) Add a hook_token_list() to show token in list of tokens

/**
 * Implementation of hook_token_list(). (token.module)
 */
function mymodule_token_list($type = 'all') {
    if ($type == 'order' || $type == 'ubercart' || $type == 'all') {
        $tokens['order']['order-cc-number'] = t('The last 4 digits of the credit card number used for the order.');
    }
    return $tokens;
}

You can now see your new token in the list of available ubercart tokens (admin/store/help/tokens)

5) Add the token to your template

Add your token using the following in your template:

[order-cc-number]

6) Clear the Drupal cache and make sure your custom module is enabled.

7) Create an order using an email account you can check. Then, verify that it is working as expected (check the email that is sent, as well as the online invoice.

Please check out the Token and Ubercart APIs for more information! If you have problems, make sure your module doesn’t have any errors, and make sure it’s being used. Also, verify that your Ubercart system is using the new template!

Posted 01:50 PM | 0 Comments | Tags:
January 21

Header & Footer Includes in Drupal

If your Drupal theme is dying for header and footer includes, you can go with regular old include(), or you can do it the Drupal way. In your template.php:

/**
 * Implementation of hook_theme().
 */
function THEMENAME_theme() {
  $items['header'] = array(
    'template' => 'header',
  );
  $items['footer'] = array(
    'template' => 'footer',
  );
  return $items;
}

/**
 * Preprocess the page template.
 */
function THEMENAME_preprocess_page(&$vars) {
  $vars['siteheader'] = theme('header');
  $vars['sitefooter'] = theme('footer');

  // Reconstruct CSS and JS for inclusion 
  $vars['css'] = drupal_add_css();
  $vars['styles'] = drupal_get_css();
  $vars['scripts'] = drupal_get_js();
}

You must do the last few lines so that you still have access to $styles and $scripts in your header.tpl.php.

Also, if you’re wondering what that $vars['css'] = drupal_add_css(); line is, that’s to create $css, which is an (often unused) array of CSS files (as opposed to $styles which is the HTML output for the CSS files).

Now, just create your header.tpl.php and footer.tpl.php in your theme. Then, in your page.tpl.php, just do a simple…

<?php print $siteheader ?>
..
<?php print $sitefooter ?>

Why would you want to do this instead of tried and true (and easy) include()? Well, for one, it keeps your page.tpl.php nice and clean. But the real advantage is that you now have preprocess functions for the header and footer:

function MYTHEMENAME_preprocess_header(&$vars) {
  ..
}
function MYTHEMENAME_preprocess_footer(&$vars) {
  ..
} 

See what I mean?

Posted 09:17 AM | 4 Comments | Tags:
January 18

Installing and updating Drupal with CVS

It’s relatively easy to use CVS to keep Drupal core up to date. Here’s a quick guide.

To install core:

$ cvs -z6 -d:pserver:anonymous:anonymous@cvs.drupal.org:/cvs/drupal co -r DRUPAL-6-14 -d directory_name drupal

When doing the above, change 6-14 to the Drupal version you want to check out (obviously using X-X syntax instead of X.X), and change directory_name to the path to the directory where you want it to download.

When it comes time to update, navigate to the site root and run:

$ cvs update -dP -r DRUPAL-6-15

Just change 6-15 to the version you’re updating to.

And of course, don’t forget to run your site’s update.php after these updates are done to keep your database in the game.

Bonus tip: since we all tend to do updates more than we do installs, I added this to my .bashrc file:

alias drupdate="cvs update -dP -r"

(Make sure that after you edit the file, you run bash so that it gets loaded.)

With this alias, you can navigate to your site root (assuming it’s already running off of CVS), and update it using a simple;

$ drupdate DRUPAL-6-15
Posted 09:35 PM | 0 Comments | Tags:
January 08

Project Verity

Mark Boulton and Leisa Reichelt just launched Project Verity, which looks to ultimately be a Drupal admin theme aimed at content creators and site editors. It will be “designed in the open and available soon,” although the site says it will be a paid/premium theme.

I asked Mark Boulton about that last part via Twitter:

@markboulton it will be a paid/premium theme?

And his reply:

@mcrittenden Yes it will. This is aimed very clearly at agencies developing sites for people like verity.

That doesn’t make very much sense to me. I’m sort of confused about the notion of designing a theme in the open and then charging for it.

Also, I’m a little confused as to the difference between Verity and Seven, the D7 admin theme they designed. Seems like they would fit the same use case.

That said, I’m pleased to see them taking the initiative I’ll be very interested to see what comes of this and how the community reacts.

Posted 09:46 AM | 0 Comments | Tags:
December 14

Drupal: Add a class to the first non-image paragraph

Say you need to grab the first paragraph of a node’s body that starts out with text. For example, you want to add a drop cap to the first letter of a node’s body, but if the first paragraph only has an image, then you can’t do anything cool like first-child. So here’s what you do.

Throw this into your theme’s template.php:

function THEMENAME_preprocess_node(&$vars, $hook) {
    $vars['content'] = preg_replace('/<p>(?!<img)/','<p class="first">', $vars['body'], 1);
}

Replace THEMENAME with your theme’s name (i.e., the name of your theme folder) and you should be good to go. This just adds class=”first” to the first paragraph of your node’s body that does NOT start with an img tag.

Posted 12:49 PM | 0 Comments | Tags:
December 02

Why host your Drupal module/theme at Drupal.org?

Many reasons!

I also made this into a handbook page.

Posted 09:06 AM | 0 Comments | Tags:
October 28

Response to “Why running the White House Web site on Drupal is a political disaster”

The original article which I will be tearing apart is here at Slate Magazine.

So let’s get started! Chris Wilson of Slate Magazine recently posted an article detailing how the White House’s move to Drupal is a political disaster. If anything is a “disaster”, it’s the article itself, which apparently is written by someone who read an intro to Drupal and has never used a CMS before at all.

First of all, I’d like to say that even if his points were true, it would still not even be close to a political disaster. I sincerely doubt that ANY politician would question the White House administration based on the technology used to run its website. Most would probably go out of their way to avoid hearing about it.

Anyways, the first wrongdoing:

Drupal knows best. It’s not that Drupal thinks you’re evil. It just thinks you’re ignorant. In a basic setup, the software is suspicious of everything you try to do. Should you, say, go completely rogue and try to add some Javascript in the body of a page—a 14-year-old technology that controls interactive components like buttons—the platform will have none of it.

What? First of all, JS can easily be embedded if you know ANYTHING about Drupal’s input formats, specifically the fact that there’s an input filter which you can disable. The default input format (Filtered HTML) doesn’t allow it for security’s sake, but it can be changed in about 2 seconds. Come on.

Also, the example he proposes (which involves putting a mouseover popup in a page’s content) would ALWAYS be done with external Javascript pulling the link’s title and displaying it in a fancy way. Nobody would put that JS right into the content. That’s gross, even though Drupal will allow it. So he’s doubly wrong.

Drupal is impenetrable. Even the software’s defenders admit that it is hostile to newcomers—or at least indifferent to their plight, as a University of Baltimore study found. The apologists will tell you that, once you scale the learning curve, it gets much easier. This is probably true, but a lot of ordinary, code-fearing people who just want a simple Web site are getting left behind.

What does this have to do with anything? Drupal has a learning curve. So what? The site’s already been built, the learning curve has already been tackled. It’s over.

Drupal hates change. Want to modernize Drupal by upgrading to a newer version? Ask these guys how that worked out for them. If Drupal were a piece of legislation, it would be the farm bill: desperately in need of an overhaul but unlikely ever to get one because entrenched interests keep the forces of reform at bay.

If by “hates change” you mean that Drupal doesn’t continually change, then you’re wrong. Drupal is consistently ahead of the game in terms of functionality, flexibility, technology, etc. Look at the changes from D6 to D7 for an example.

However, if by “hates change” you mean that upgrading is a pain, then it depends. Upgrading to a new major version release is a pain in that modules and themes have to be updated, but major versions are only released about every 18-24 months so even if you do decide to update your sites, you’re only doing it every 2 years. Upgrading to minor maintenance releases takes all of about 3 minutes. I’d for one MUCH rather have a system that was a little tough to update when a major release comes out than a system that makes updating easy by staying backwards compatible and never revolutionizing itself.

Also, I still don’t see what this has to do with politics.

Drupal is disorganized. Instead of displaying your pages in folders that you can browse, like you do on your personal computer, Drupal provides a nightmarish content list. To find what you’re looking for, you have to search for it.

A lot of Drupal sites have hundreds of thousands of nodes. Try finding those things if they’re put into folders. Folders also constrict your way of thinking. Say I want to have one piece of content reachable from two places in the menu system, or I want to have two path aliases point to the same thing, or I want to have one blog post in the top level navigation while all the others are just plain old blog posts.

And unlike most content management systems, Drupal doesn’t have a convenient way to prevent two people from accidentally editing the same page at the same time.

Not only is that not true (the second user would get a message saying that you can’t save without overwriting someone else’s changes), but that link has absolutely nothing to do with the topic.

Drupal is righteous. The open-source movement has done wonderful things for the Web. But at its core, it remains a religion. If you went to DrupalCon in Paris last month, then you would have almost certainly come across proselytizers of one the movement’s fundamental tenets: Drupal doesn’t break Web sites. People with Drupal break Web sites. Most problems with Drupal stem from people who “don’t get it” or aren’t using it correctly. This is probably true, but it’s not much consolation when you spend 45 minutes trying to upload a photo.

[citation needed]

This is just some grumpy opinion passed off as fact.

Come on, Slate. Do a little research. Don’t be so dramatic.

Posted 08:27 AM | 2 Comments | Tags:
October 15

Drupal: change a menu item’s class in module

So I just spent 3 hours writing 4 lines of code.

The problem: we need a way to make a certain “Approvals” link red if there are approvals waiting. This could also apply to making an “Inbox” link stand out if you have unread messages or whatever. Anyway, according to Google, this is something that has never been done before in the history of computing, so here it goes.

The hook we’re using is hook_translated_menu_link_alter() a.k.a. the_longest_hook_name_that_drupal_has_ever_dreamed_of(). Basically, that hook grabs menu items before they’re displayed so you can change them however you want. Changing them is tough though, particularly because it’s not documented at all.

So here’s the code.

/**
 * Implementation of hook_translated_menu_link_alter()
 */
function next_menu_translated_menu_link_alter(&$item, $map) {
  $num_unapproved = db_result(db_query("SELECT count(*) FROM {node} WHERE status = '0' AND type = 'reservation'"));
  if ($num_unapproved > 0 && $item['mlid'] == '2101') {
    $item['localized_options']['attributes']['class'] = 'attention';
  }
}

So as you can see here, I’m counting all the reservation nodes which are unapproved, and if there are any, I grab that specific menu item and give it a class of ‘attention’. The tough part here is the ‘localized_options’ part. The API would have you believe that that part should just be ‘options’ but it’s all a big lie.

Maybe it has something to do with the locale module? Anyway

Posted 07:38 PM | 2 Comments | Tags:

WTF?