I want to provide the ability for users to make and save a set of pre-defined options when they create a node.

They can then choose from their predefined set of options when creating future nodes.

(At the moment I allow users to create nodes with a custom form, I could possibly switch to the standard add node method but I'd prefer not to, unless it's a lot easier.)

Has anyone on here tried this before? At a high level, how did you go about trying to save user presets?

  • "a set of predefined options which will be automatically filled in on opening the template form" - wait, what? I'm not sure if I understand. You mean simply per-user defaults? Why not to call it that way then? Please try to avoid words that have a certain meaning in Drupal. If .tpl.php is not what you want, try not to call it template.
    – Mołot
    Feb 10, 2014 at 14:20
  • Ah that's a better way of phrasing it. I was struggling to come up with a good replacement for template. Editing it now. Feb 10, 2014 at 14:50

1 Answer 1


I find using a custom form always seems to be more work then just using the one Drupal provides to add nodes. Even if they are not quite what you want hook_form_alter makes it easy to modify them to suit your needs.

I would create an unpublished node for each of my 'presets'. This way it is easy for an administrator to update the preset values, but normal user won't be able to see them.

Then using hook_form_alter I would add a selectbox to the top of the node add form allowing the users to choose between any of the presets. You could add an #ajax property to the selectbox that rebuilds the whole form.

Again in hook_form_alter if $form_state['values']['yourselectbox'] is not empty then you can load the values from that node and populate the form with them.

As an example (none of this code is tested but I think should be almost right):

function mymodule_form_alter($form, &$form_state, $form_id) {

    $templates = db_query("SELECT nid, title FROM node WHERE type='my_type' AND status=1")->fetchAllKeyed();
    // TODO: Should maybe add a field to control if this node is a template or not?
    $default_template = 0;

    if(!empty($form_state['values']['templates'])) {
        $default_template = $form_state['values']['templates'];

        $selected_template = node_load($default_template);

        // if your fields have multiple values you will need to loop here
        $form['other_field_1'][LANGUAGE_NONE][0]['#value'] = $selected_template['other_field_1'][LANGUAGE_NONE][0]['#value']
        $form['other_field_2'][LANGUAGE_NONE][0]['#value'] = $selected_template['other_field_2'][LANGUAGE_NONE][0]['#value']


    $form['#prefix'] = '<div id="whole_form_wrapper">';
    $form['#suffix'] = '</div>';

    $form['templates'] = array(
        '#type' => 'select',
        '#title' => 'Template',
        '#options' => $templates,
        '#default_value' => $default_template,
        '#ajax' => array(
            'callback' => 'change_template_callback',
            'wrapper' => 'whole_form_wrapper',
            'event' => 'change',


function change_template_callback($form, $form_state) {
    return $form;

See the examples module - it has loads of good stuff on forms and ajax.

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.