Drupal

Allowed Values pulled from Table

Fred's picture

A simple method for pulling Allowed Values for a CCK Select List field:

$sql = 'SELECT p.`partno` FROM {nrg_custom_monitor_part_numbers} p ORDER BY p.`partno`';
$result = db_query($sql);
while($row = db_fetch_array($result)){
  $rows[$row['partno']] = $row['partno'];
}
return $rows;

Note that you need both a key and value!

Filling an array

Fred's picture

To fill an array from results of a database query you normally do something like this:

function get_monitor_models() {
  $sql = "SELECT title FROM {node} n WHERE n.type='dell_monitor_model' ORDER BY n.title";
  $result = db_query($sql);
  $monitor_models = array();
  while ($monitor_model = db_fetch_array($result)) {
    $monitor_models[] = $monitor_model;
  }
  return $monitor_models;
}

However, if you want the key to be one returned field, and the value to be the second, do this instead:

Changing Admin & User paths

Fred's picture

Having signed up for a Reseller account over at Terra Networks, I found out that the Ensim Control Panel (or Parallels control Panel as it is now known as) automatically uses http://<sitename>/admin and http://<sitename>/user for it's administration URLs. This is a bit of a pain as they are the same as the Drupal URLs!

Theming CCK Input Forms

Fred's picture

Theming CCK forms initially appears to be a bit of a black art, but is very simple. The simplest method is to add:

function phptemplate_node_form($form) {
  if(file_exists(path_to_theme().'/'.$form['type']['#value'].'_form.tpl.php')) {
    return _phptemplate_callback($form['type']['#value'].'_form', array('user' =&gt; $user, 'form' =&gt; $form));
  }
}

to either your theme's template.php, or to a custom module for greater portability. Then create a content_type_form.tpl.php in your theme folder.

How to waste 3 hours on a 5 minute task

Fred's picture

I was recently updating some features of the Missing List, specifically how the UK Postcodes are handled. Due to the nature of the site, it was requested that only certain roles had access to full postcodes not entered by the current user (i.e. only the main Administrator). Everyone else would only get the first part of the postcode (e.g. EC1V instead of EC1V 4PY). This was recently requested to be expanded to include other roles.

Formatting Exposed Filters (Views)

Fred's picture

For some reason, the default layout code for the exposed filters gives some very strange results. To force the filters to be rendered in a series of divs to be styled as required, add the following to your theme's template.php:

Creating Sub-sites in Drupal

Fred's picture

Synopsis:
Your main website Drupal installation is in /home/[user]/public_html) and you want to create www.mainwebsite.com/subsite

CCK Numeric Field Problems

Fred's picture

When using CCK Numeric fields, entering 1,000,000 is displayed as 1e+06 (which is nothing like that we were looking for when displaying rewards). To display the value correctly use:

sprintf("%01.2f",$node->field_amount[0]['view'])

CCK Field validation

Fred's picture

When using CCK Fields, you can validate the input by using the CCK Field Validation module. Being a bit slow on the uptake, I took me a while to work out exactly how to do this. It turns out to be very simple:

if ($node->field_ police_constabulary[key] == 'Select Force/Location') {
  form_set_error('field_police_constabulary','must choose a police force to post an Intelligence Report');
}

CCK (Content Construction Kit)

Fred's picture

CCK (or Content Construction Kit) is one of the most important modules to help the new, and even seasoned, Drupal website creators. Rather than having to painstakingly create your new content type by hand in a custom module, CCK allows you to create a new content type with a few clicks of a mouse.

Syndicate content