简体   繁体   中英

Drupal, overriding add/edit form forms for custom content type

I have created a new content type called protocol. The problem is that when you define a content type that means you also say how in the form the content is to be added and edited, like which form elements there will be.

A protocol is a content type that stores a title, an abstract and instructions. I want to add the title/instructions/abstract through one textarea where you tag the parts of the text like this:

[title]This is a title[/title] [abstract]This is an abstract. [/abstract][instructions]And these are my instructions.[/instructions]

That text is then processed and the content between each tag can be picked out and stored in a variable which should then be stored for the content type just like it had been added through a seperate field/textarea in a add/edit content form.

Is this possible to do? What kind of things should I read up on? Where in the drupal code are the function/functions that describes what happens when you push "Save" for a new content type for the standard add content form?(I just want to read it, not change anything)

Not sure this exactly matches what you're trying to do, but in a basic sense it should get you towards your goal. I wrote a module called endorse for Drupal 6 that provides a custom form feeding the submitted values into a new node:

http://drupal.org/project/endorse

Here's the form definition:

http://drupalcode.org/project/endorse.git/blob/refs/heads/master:/endorse.module#l136

Some basic validation follows and then the actual node save occurs at the top of the submit function, here up to line 231:

http://drupalcode.org/project/endorse.git/blob/refs/heads/master:/endorse.module#l206

The rest in that function is irrelevant except for the thank you and redirect at the very end of the submit function. If you're doing this in D7, it'll change a bit (see api.drupal.org for function definitions and whatnot), but it should look more or les the same.

Steps to solve your problem.

  1. Create a module. Implement hook_menu with your custom add page.
  2. Create a custom form using FORM API that it's gonna be displayed in your new page.
  3. In your hook_form_submit get your values from the variable form state.
  4. Parse the text and create and save a new node (snippet here).

     $newNode = (object) NULL; $newNode->type = 'protocol'; $newNode->title = $parsed_title; $newNode->uid = 1; $newNode->created = strtotime("now"); $newNode->changed = strtotime("now"); $newNode->status = 1; $newNode->comment = 0; $newNode->promote = 0; $newNode->moderate = 0; $newNode->sticky = 0; // add CCK field data $newNode->field_{YOUR_CUSTOM_FIELD_1}[0]['value'] = $parsed_data1; $newNode->field_{YOUR_CUSTOM_FIELD_2}[0]['value'] = $parsed_data2; // save node node_save($newNode); 

Those are the basic steps. If you have any more questions please ask.

TIP: Install the Devel module and use the function dpm() when you need to know the contents of some variable. You are probably gonna need it when you are implementing hook_form_validate or hook_form_submit for knowing the contents in the variable $form_state.

So just do:

dpm($form_state); //this will give you the variables inside the array with a krumo view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM