Jul 9

Drupal conventions plain and simple.

Category: Code

There are some common misconceptions about doing certain tasks in Drupal. I take the following to be more of a random collection of thoughts about Drupal and some of the explanations behind them. Lets take for example the CCK node types and forms.

When you build in the CCK you are building a node type but some things are not apparent such as how do I access the CCK node type form on another page? The answer is not simple because the documentation is sparse in this area. I have found that if you want to call a CCK node add/edit from you can do so by appending the machine readable name of the node with “_node_form”. So for example lets say we have a CCK node type with the label “article” the internal form name would be “article_node_form”. Now to call a form is very simple, all you have to do is

function show_me($node_id)
{
$output = ”;
$article_node = node_load($node_id);
$output .= drupal_get_form(’article_node_form’, $article_node);

return $output;
}

As you can see pre-loading any form with a node is just a simple matter of loading the node and then passing it as a second parameter. It took me quite a while to dig through the API and the docs on drupal.org to figure this out.

Additionally theming the form elements is also very trivial once you sift through the documentation You can create a theme function that takes the form as a parameter. So we have our CCK form and we can theme this just like any other form. Usually this is done by pre-pending “theme_” to the form name in our case “article_node_form”. The completed function name would be “theme_article_node_form” For example:

function theme_article_node_form($form)
{
// render specific elements of the form.
$output .= drupal_render($form['title']);

// most important is to render anything additionally such as submit hooks
$output .= drupal_render($form);
return $output;
}

The key to the “theme_” hook is that you can do anything with the form elements and combine them with html attributes. Take for example this complete example of a theming function I built to put certain elements in a table:

function theme_advertisement_node_form($form)
{
$output = ”;
$output .= drupal_render($form['title']);
$output .= drupal_render($form['field_campaign_reference']);
$output .= drupal_render($form['taxonomy']);

foreach($form['field_editions']['keys']['#options'] as $eid => $edition)
{
// get the machine label for the edition
$edition_info = db_fetch_array(db_query(”select label from {edition} where name=’%s’;”, $edition));
$row = array();
$row[] = drupal_render($form['field_editions']['keys'][$eid]);
$row[] = drupal_render($form[$edition_info['label']]['revenue']);
$row[] = drupal_render($form[$edition_info['label']]['offset_revenue']);
$row[] = drupal_render($form[$edition_info['label']]['no_commission']);
$rows[] = $row;
}

$table_header = array(
array(’data’ => ‘Edition’),
array(’data’ => ‘Revenue’),
array(’data’ => ‘Offset Revenue’),
array(’data’ => ‘No Commission’)
);

//$output .= drupal_render($form['field_editions']['keys']['#title']);
$output .= theme(’table’, $table_header, $rows);
$output .= drupal_render($form['field_flight_date']);
$output .= drupal_render($form['field_required_sends']);
$output .= drupal_render($form['field_cpm']);
$output .= drupal_render($form['field_total_revenue']);
$output .= drupal_render($form['field_creative_approved']);
$output .= drupal_render($form['field_invoiced']);
$output .= drupal_render($form['image_attach']);
$output .= drupal_render($form['submit']);

// unset any form elements we didn’t use
unset($form['field_editions']);

$output .= drupal_render($form);

return $output;
}

That should wrap anything in regards to theming and working with CCK node types.

No comments

Jul 3

To drupal or not to drupal that is the question…

Category: Code, General, Music

So I was debating using Drupal which we are basing the company that I work for enterprise site or just scrap the existing WordPress and do a fresh install with a new theme.  Well if you can’t tell I went with the latter.  So I have been busy learning the nuances of Drupal 5.7 and have come to the following conclusion.

1. It is some what great, major companies use Drupal and accomplish great things.

2. You can do some cool stuff with it, even build some excellent modules.

3. You can also learn to bang your head against the damn wall…

Numero tres is more of a product of you staring at the api.drupal.org site, your code, back a the api.drupal.org site, then your code, asking your boss why it might not work (not that he is a Drupal master of course), then going onto #drupal on Freenode and not finding the info, then going back to your code realizing you have spaces in your array keys, then you fix it…

So that is it in a nut shell.  If you are feeling quite peckish, take a look at Sasha & Digweeds set over @ mercuryserver.com.  Shizzle my nizzle.

No comments

Jul 3

Hello world!

Category: Uncategorized

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

No comments