4

How can I create a customised add/node form in D7?

I am not talking about a form for gathering information that is not content for the site, like an event registration form. I'm talking about the node/add form where you create content for your site.

What I want to replicate is just some simple html tables, arranged together, and some css to make it look good. Lots of boxes in which to insert numbers, mainly, a few drop-downs and text fields too. Imagine you navigate to node/add/scorecard and something like this appears:

Thanks.

SOLUTION

This is the solution I have arrived at:

(1) Create a module (.INFO and .MODULE files).

(2) Implement hook_menu (in your new module) to create a url and page where the form is to reside.

(3) Add a function to render the HTML of the form using drupal_get_form.

(4) Add a form builder function and construct your form using an array called $form (see form API for details).

(5) Add a submit button at the end of your form.

(6) To make the form look the way you want it to there are several methods: (i) create a theme function to add to the elements of $form; (ii) use #prefix and #suffix properties to add additional HTML and CSS ids/classes to the rendered form; (iii) manipulate the many ids/classes, rendered by drupal_get_form, with CSS; (iv) use the #markup properties to add additional HTML and CSS ids/classes to the rendered form (not recommended). I used a combination of (ii) and (iii). I could not work out how to do (i) but it is probably the 'most correct' method.

(7) Add a validation function.

(8) Add a submit function using node_save (for nodes) and drupal_write_record (for fields) to insert your form values into the relevant database tables: node, field_data_field_my_field etc.

4
  • Perhaps you can post a URL to an example of what you're trying to achieve?
    – Bisonbleu
    Mar 24, 2013 at 20:52
  • I've added an image and refined my question to clear up any confusion about what I mean by the term 'form'. Thanks.
    – dbj44
    Mar 27, 2013 at 15:46
  • Did you have a look at this? drupal.stackexchange.com/questions/33928/… Mar 27, 2013 at 21:14
  • Thanks. Basically, he's saying create a custom template page for a specific node/add page. Interesting, but, again, I can see this approach giving me less control. --- I have now successfully created the content types (bundles) I need, so all the tables are in the database. I have successfully created a module, using the form API, which creates a custom form on its own page. Just need to theme the form to make it look the way I want, and then see if I can create/update table data - this last bit is where I may run into problems. This is all I have to go on: drupal.org/node/310079
    – dbj44
    Mar 27, 2013 at 23:04

3 Answers 3

2

you may try using Webform module.

Webform is a module that aids in making various one-off forms such as contact forms, surveys, order forms, reservations, CRM requests, and more.

To install the module, download the module and add it to your sites/all/modules directory. Enable it under /admin/build/modules.

You will now have a new node type called "Webform". From there you simply give the webform a title, description, confirmation message, set up email settings and the like.

Finally, you can add several components (think fields). There are several component types available: fieldset, pagebreaks, grid, textarea, textfield, select (checkboxes, pulldown lists), email addresses and more.

Here's the documentation. Let me know if you need more information.

-------------------- UPDATE ----------------------

To customize the HTML of the form you can copy the file webform-form.tpl.php and copy it into you template. Inside there is the array $form['submit'] that contains the compontents of the form. write dsm($form['submit']) and it will print the compontents. Then you may do something like

<div class="my_class" id="my_id">
<?php print render($form['submit']['YOUR_FIELD']); ?>
</div>

Then you may want to assign this template to that node, so it will be webform-form-NID.tpl.php

3
  • 1
    This may work for me, but I'm concerned, like many contrib modules, it won't give me the amount of control I want. I was thinking of making a custom module and using the form API (drupal.org/node/204270) - which gives a module a way to define a form via a structured array and render it into HTML. I've never used this API before. What do you think?
    – dbj44
    Mar 25, 2013 at 9:51
  • I've updated my answer. Mar 25, 2013 at 19:57
  • Thanks. I may come back to this if Form API doesn't bear fruit.
    – dbj44
    Mar 26, 2013 at 12:58
0

You can certainly use modules like Webform, but if you are truly looking for a customized form - two options based on what you are trying to do.

  1. If this is customizing an existing form (like node add form?) - use hook_form_alter().
  2. If this is a brand new form, use hook_menu()
4
  • What about using Form API to define and render a form via a custom module, which looks something like: (1) Implement hook_help to define help information (2) Create a form builder function to store form elements in $form and to define a submit button (3) Theme the form by adding '#theme' attributes to the form (4) Create a form submit function using db_insert to put the form data into the relevant tables (5) Implement hook_block_view with drupal_get_form ---I've been reading this: drupal.org/node/751826 and trying to make adjustments for D7.
    – dbj44
    Mar 26, 2013 at 11:05
  • When you say use hook_menu() do you mean to define the URL to the form page? Thanks.
    – dbj44
    Mar 26, 2013 at 11:50
  • @user1919784 - Yes you will need to use Form API. First question - are you trying to edit an existing form (then use hook_form_alter) and trying to make it look different? or trying to create a new custom form at a new custom URL(then use hook_menu)? Are you looking for code examples?
    – drupalmav
    Mar 26, 2013 at 17:48
  • I am going to try to create a custom form. I am not trying to alter a form provided by core. I have found some code examples to get me started. I've created a new module. Inside, I have (1) implemented hook_menu to create a custom url; (2) a function to return the form (via drupal_get_form); (3) a form builder function with submit button; and (4) a db_insert function. The latter I am most unsure about. I have node and 3 other tables that will need to be populated by the form. 2 of the tables will need multiple rows to be added by the submitted form. Thanks.
    – dbj44
    Mar 26, 2013 at 20:51
0

Use Entityform Types. That way, you will have more control. It has custom fields, and it integrates with the Views module.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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