0

I have constructed a content type, 3 fields are timestamps.

I wish to reuse this content type (form input etc) to a client facing application, with the caveat being the form is modified. For example my module should be able to tweak the form (ie: Hiding the timestamps fields and updating them accordingly).

I don't want to rebuild the whole form and muck with the CCK API to get data to persist, so I am wondering if it is possible to simply re-call that form and it's functionality and tweak it ever so slightly???

I found this googleing:

//Showing a node edit page with drupal_get_form() and hook_menu()

And implemented some of it:

function _tasks_create() {
  module_load_include('inc', 'node', 'node.task');

  $nid = '1';
  $node = node_load($nid);

  return drupal_get_form('task_node_form', $node);  
}


function tasks_menu() {
  $items = array();

  $items['tasks/create'] = array (
    'type' => MENU_CALLBACK,
    'title' => t('Create New Task'),  

    'page callback' => '_tasks_create',
    // Where is $node coming from???
    'page arguments' => array('task_node_form', $node),

    'access arguments' => array('create tasks')    
  );

  return $items;
}

But I see two immediate issues:

  1. $node in hook_menu is undefined -- where do I get it from??
  2. I have no idea what the $nid (Node ID) will ever be for my content type

I need to essentially re-display the form that I use in the admin, hiding everything but maybe three fields (that means no URI, menu options, etc).

I will probably need to hook into the submission of the form in this context only and update the timestamps accordingly.

What am I missing or doing wrong? Any ideas?

Alex

  • drupal_get_form in D& doesn't seem to support additional arguments according to the docs...the signature is totally different than what the examples above have shown??? – Alex.Barylski Sep 6 '13 at 15:10
  • Have you tried drupal_get_form('node_form', $node); in your callback function? BTW there's no node.task.inc file so you can remove the module_load_include line. You'll probably need to replace it with an include of node.pages.inc, where that form function lives – Clive Sep 6 '13 at 15:41
  • How to obtain $node if I do not know the ID of the content type? Ideally I would figure that out based on the fact I know the content type machine name is "task" – Alex.Barylski Sep 6 '13 at 15:55
  • The node object passed to a node creation form can't have an ID yet (it doesn't get created until you press submit). I can't quite follow what you're trying to do but I think something like $node = new stdClass(); $node->type = 'task'; node_object_prepare($node); would suffice before passing it to drupal_get_form() – Clive Sep 6 '13 at 16:00
  • Basically I want to show the same form which is shown in the /admin section but on a custom URI which I have specified in a module hook_menu. Once i get that working I want to manipulate the $form object (hiding some fields, tweaking others, etc). I do not understand the internals of drupal well enough to accomplish this though :p – Alex.Barylski Sep 6 '13 at 16:05
0

Clive's answer is right.. You can not pass $node object before you created yet. If you want to show the same form which is shown in the /admin section you can do something like this:

function tasks_menu() {
  $items = array();

  $items['tasks/create'] = array (
    'type' => MENU_CALLBACK,
    'title' => t('Create New Task'),  
    'page callback' => 'drupal_get_form',
    'page arguments' => array('task_node_form'),
    'access arguments' => array('create tasks')    
  );

  return $items;
}

and:

function task_node_form($form, &$form_state) {
  $form = array();
  $node = new stdClass();
  $node->type = 'YOUR TYPE';

  field_attach_form('node', $node, &$form, &$form_state, $langcode = NULL, $options = array())

  // Other code
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.