tags:

views:

5982

answers:

5

How do I customize a create content form for a certain content type. In this instance I have a CCK type, of Products but every time I create a product I use 4 fields Name, Price, Picture and dimensions.

Is there a way to slim down the create content form to just have these options? Is this what Contemplate does?

+3  A: 

You'll want to use the hook_form_alter hook.

In Drupal 6, I use this to hide most of the extraneous stuff in the node edit/add form.

function mymodule_form_alter(&$form, $form_state, $form_id) {
  // hide extraneous options in the node form for nodetype nodes
  if($form_id == 'nodetype_node_form') {
    $form['path']['#access'] = FALSE;
    $form['menu']['#access'] = FALSE;
    $form['author']['#access'] = FALSE;
    $form['options']['#access'] = FALSE;
    $form['comment_settings']['#access'] = FALSE;
    $form['revision_information']['#access'] = FALSE;
  }
}

Contemplate is for styling the node view, not the node form. I advise against it - it's far better to use node-nodetype.tpl.php files.

ceejayoz
This is also relevant to Drupal 5, I guess?
Brian G
Relevant but slightly different (hook_form_alter's arguments changed in D6).
ceejayoz
A: 

Another option -- and this might be simpler if you're really trying to slim things down -- is to create your own form from scratch, and create a node object yourself in the form's submit handler.

Eaton
+1  A: 

Found a great resource on this:

http://www.lullabot.com/articles/modifying-forms-5-and-6

One thing that it mentioned that was not covered by Eaton or ceejayoz is to hide the fields in the theme layer.

Brian G
Setting #access to FALSE hides the fields in the edit form without having to get into the theme layer.
ceejayoz
A: 

I create a module and use form_alter and the nodeapi.

Below is the basic idea of the components your module will need.

My product.install file has the following:

function product_install() {
  db_query("CREATE TABLE {product} (
    nid int(10) unsigned NOT NULL default '0',
    primary key (nid),
    price DECIMAL(7, 2) UNSIGNED NOT NULL DEFAULT 0.00
    ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;"
  );
}

function product_uninstall() {
  db_query("DROP TABLE {product}");
}

Then the product.module file contains:

function product_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if($node->type != 'product') {
    return;
  }
  switch($op) {
    case 'load':
      return _product_load($node);
    case 'insert':
      _product_insert($node);
      break;
    case 'delete':
      _product_delete($node);
      break;
    case 'update':
      _product_delete($node);
      _product_insert($node);
      break;
    case 'view':
      //display your product 
      $node->content['price'] = array(
        '#value' => theme('transact_node_status', $node),
        '#weight' => 5,
      );
      break;
  }
}

function product_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'product_node_form'){
    $form['price'] = array(
      '#type' => 'textfield',
      '#title' => t('Price'),
      '#default_value' => $form['#node']->price,
    );
  }
  return $form;
}

function _product_load($node) {
  $result = db_query("SELECT * FROM {product} WHERE nid = %d", $node->nid);
  $arr = db_fetch_array($result);
  unset($arr['nid']);
  return $arr;
}

function _product_insert($node) {
  db_query("INSERT INTO {product} (nid, price) VALUES(%d, %d)", $node->nid, $node->price);
}

function _product_delete($node) {
  db_query("DELETE FROM {product} WHERE nid = %d", $node->nid);
}
Rimian
A: 

Instead You can Use nodeformcols module

ali

related questions