jQuery UI ,Drupal, and Behaviors Slides

Section:

Slides from a presentation I did today @ drupal camp austin. Will expand on Drupal.behaviors in a later post.

Note to self: pick an easier topic for next presentation

Switching Drupal tpl.php files at will: Old Switchy McTipplefep's Trick

Section:

One of the first lessons they teach you at the School of Drupal Arts, Arcane Sciences and Sorcery is that a tpl.php can have dynamically generated wildcards or "suggestions". You can see this every day drupal themes: you can simply use the "node.tpl.php" file if you want only one style for a node. Where as if you want different styles for three node types: blog, story & page, you create the following files:
  • node-page.tpl.php
  • node-story.tpl.php
  • node-blog.tpl.php
Obviously, this pattern exists far beyond nodes. For page.tpl.php, user/register can have its own tpl.php file:
  • page-user-register.tpl.php
Where as user/1/edit will be (yes -- these suggestions will remove numbers... i think... i was hung over that day in class):
  • page-user-edit.tpl.php
This may be fine and dandy if your agenda is either to build a simple drupal theme with a few exceptions, or to build manly Texas sized drupal themes with enough tpl.php files, and duplicate html code to choke a donkey. Old Switchy McTipplefep's trick is for anyone who:
  • wants 3 styles that are shared by 20 blocks
  • wants 4 possible layouts for dozens of pages in a site
  • has 12 node types, but wants them to share 3 styles
The magic happens in your theme's $theme_name . '_preprocess_' . $theme function. Below are examples for how a theme named "example" would do it.
function example_preprocess_node(&$vars) {
  $node = $vars['node'];
 // only switch for story type
  switch($node->type) {
    case 'story':
      $vars['template_files'][] = 'node-batman';
      break;
  }
}

function example_preprocess_block(&$vars) {
  // sure why not -- we'll pass our node.tpl.php file to our block
  // That's how tpl pimpin works sometimes
  $vars['template_files'][] = 'node-batman';
}

Architecture is for Everyone: The Path to a More Humane Drupal API

Eaton's presentation on Architecture & Drupal is by far the best of the 12 or so drupalcon presentations I've watched so far (they were all good, but this one just seems to stand out). If you missed it, its worth your time.

http://www.archive.org/details/Architectureisforeveryone

A Damn Simple Technique For Making Anything in Drupal Ajaxed*

*well, probably anything that uses drupal.behaviors...

Today, I was doing a few experiments on how to get several giant CCK node forms to load and submit via ajax from a single custom page.When this technique actually worked on the first try, my exact words were "no f#cking s#it..." I'm sure I'm not the first to figure this trick out, but I have had a hard time finding people who've described it. Perhaps everyone besides me figured it out ages ago -- though if that's true, I don't want to know what horrible things have driven some of you to use your current techniques.

This technique seems especially ideal for integrating any existing drupal form (especially giant CCK forms with sortable, multiple value, file fields), and the results into some highly customized drupal based web gizmo. *IT REQUIRES NO PHP* beyond whatever calls you may have to make to get dependent JavaScript files available to your requesting page. This pattern will mostly degrade to drupal's default behavior if JavaScript isn't present.

The stupid simple strategy

We use javascript to take what we want from drupal's default behavior, and throw away everything else.

Below, is all you need to submit a giant CCK form and have it successfully post:

//node form just happens to be the ID shared by every node form
//replace #node-form with any form ID and the results are the same
$('#node-form').submit( function() {
  $.post($(this).attr('action'), $(this).serialize(), function(response) {
  // ...
  }
  // return false prevents the form from submitting regularly ... noob...
  return false;
});

Even though the user will not be redirected or see any results of this $_POST, jquery will still follow the default redirect to node/$node->nid or return the original form with validation errors -- this is actually a good thing.

$('#node-form').submit( function() {
  $.post($(this).attr('action'), $(this).serialize(), function(response) {
   // why not, lets replace our logo with the resulting node
    $('#logo').replace($(response).find(.'node');
  }
  return false;
});

There's a few downsides I see to this technique: for one generating entire pages is totally unnecessarily; since we are simply taking contents of $(.node) from the result's full document object, why load the entire page? The answer is: the page already exists, and its one less menu_callback, loading, or godforbid form behavior altering trick that will cause bugs down the road. Its easy, it works, and all but the minority of websites would need to worry about the performance implications.

Another downside is that since we are using jQuery, we are depending on classes and ids that may eventually change. I think this is a real risk, but i think the risk can mostly be avoided by making intelligent decisions about what selectors you use. Besides, the maintenance hassle is probably still less compared to many of the alternatives.

Below is a working example: load a node "edit" tab, submit, and refresh the node all via ajax.

The code snippet is cute as a button, though its handling of validation is sort of half-assed. Note that the vast majority of code is devoted to simply throwing around the response data, the jquery itself is elementary.

Web Development:

Intro to Drupal 6 Multistep Form Domination Using Chaos Tools

The mere words "multistep form" once gave me a feeling of dread. There are several techniques (arguably hacks) that enable multistep forms in drupal 6. However, if you've ever used them, you'll know that they are a not techniques for the faint of heart.

While Merlinofchaos's multistep form wizard is not for the faint of heart either, I will say I found programming the forms to be fun. The setup takes a bit of focus, but after that, writing the steps is almost too easy.

Chaos Tool's wizard.inc is distinct:

wombat wizard
  1. See that stepper and buttons in the above image? You never will need to micromanage what step your form is on using the wizard. If you set up the form, it figures that sort of stuff out for you. Since it knows about stuff like "$step", its perfectly happy to figure out whether it should display a next, back, or finish button. I like code that works for me.
  2. Every step in a form is a distinct form_id, that has its own #submit, #validate, and(god knows what else can be done with it via form api). Within the steps, you should never need to think any harder than you would writing a simple form with a message, and an email address. Proof can be found below in the actual form arrays, and the submit functions.
  3. There are thousands of ways you can mess up a multistep form -- in a sense, the wizard lets you make your biggest mistakes in one $form_info array, while keeping the majority of the code (the formapi arrays, and form processing/validation functions) in small, easy to understand units that anyone with even intermediate formapi knowledge could work with.
  4. This wizard DOES power a number of complicated multistep forms that you may use every day - the multistep forms in panels in particular. In a way, its just the most recent chapter of Earl Miles vs. Drupal's formapi -- a saga that has gone on since version 4.7. [come to think of it, i think lullabot needs to make a feature film about that epic story]

Interested? Start Here

Here's a Live Demo of the Wombat Deployment tool I wrote that uses the wizard. Only impressive in how easily it was written, and how easily i could write a 3rd, 4th, 5th, or 20 steps following the same pattern [ the subject of future posts are hinted in a rogue modal.inc file in the download.]

To get started building multistep forms, follows these steps.

  1. Download Chaos Tools
  2. Advanced Help. If you start futzing with this, you'll want the docs merlinofchaos wrote on the wizard. They are available through this module only.
  3. My example module, the wombat tool to rule them all!
  4. check back in a few for all the corrections

The full code, and detailed doc on the $form_info array will only be made available to freaks who click the "read more" link.

top cleric declares nicklewis.org spam free

Update Actually, I think the spam is under control now. Only a few thousand that needed to be cleaned. In other news, I've sent my blog's mullah into exile.
****"

O Navicat -- how I love thee!

[img_assist|nid=1050|title=asdf|desc=|link=node|align=left|width=640|height=351]

Dear Site5: WTF is up with your file permissions behavior?!

I know yall do QA, however, you aren't testing for power users apparently. Check this out. I like to build and deploy drupal websites. Drupal websites require constant code updates, as well as test servers, so I like to use shell to move files around. However, it seems, whenever I move files around, every single files and directory gets assigned permissions 664, or 644 -- i forgot. The fix is running this command in the unix shell:

chmod -R ugo=rX,u+w ~

Content Type Madness (CTM): Symptoms, Treatment, and Prevention

[img_assist|nid=1045|title=|desc=|link=none|align=left|width=100|height=80]Content type madness is a common disease that effects drupal sites of all sizes. It is most frequently seen in drupal 5 and 6 sites, as result of content construction kit.

It is a serious, chronic disease that can result in long term damage to a site's agility, usability , and information architecture.

 

A Reproach to: "In Defense of Eye Candy"

The Time of Webdesign, alistapart.com, published an article "In Defense of Eye Candy". Yes, I agree: people like nice looking things. I'd even go as far as to say that surveys, and studies don't necessarily do justice to how biased all of us are to visually pleasing things. I also think many people aren't aware of the real reasons why they might choose design A, over B.

Pages

Subscribe to Nick Lewis: The Blog RSS