1

This is definitely being called

function studiga_theme_theme($existing, $type, $theme, $path) {    
    $items['add_property_form'] = array(
        'render element' => 'form',
        'template' => 'node--14',
        'path' => drupal_get_path('theme', 'studiga_theme') . '/templates/node',
    );

    return $items;
}

But which should set up the form fields isn't and I have no idea why

function studiga_theme_preprocess_add_property_form(&$variables) { 

  // Shorten the form variable name for easier access.
  $form = $variables['form'];

  $form['first_name'] = array(
        '#type' => 'textfield',
        '#attributes' => array('placeholder' => t('First name')),
    );
    $form['last_name'] = array(
        '#type' => 'textfield',
        '#attributes' => array('placeholder' => t('Last name')),
    );
    $form['address'] = array(
        '#type' => 'textfield',
        '#attributes' => array('placeholder' => t('Address')),
    );
    $form['city'] = array(
        '#type' => 'textfield',
        '#attributes' => array('placeholder' => t('City')),
    );
    $form['state'] = array(
        '#type' => 'select',
        '#options' => array(
            'default' => 'State',
            '...' => '...',
        ),
    );
    $form['zip'] = array(
        '#type' => 'textfield',
        '#attributes' => array('placeholder' => t('Zip')),
    );
    $form['email'] = array(
        '#type' => 'textfield',
        '#attributes' => array('placeholder' => t('Email')),
    );
    $form['phone'] = array(
        '#type' => 'textfield',
        '#attributes' => array('placeholder' => t('Phone')),
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
    );   
}

Thanks

1
  • Can you elaborate on the problem you're solving? Adding forms to templates in the theme layer is usually not the best solution: The theme layer is to control how your site looks, not how it works. Commented Jan 29, 2016 at 12:28

1 Answer 1

3

As marcvangend mentioned already, theming functions are not the right place for altering forms.

I suppose you are using Drupal 7 and like to alter a form that has already been added to the node before. In this case you should have a look into hook_form_alter() or hook_form_FORM_ID_alter():

https://api.drupal.org/api/function/hook_form_alter/7

https://api.drupal.org/api/function/hook_form_FORM_ID_alter/7

These hooks can be used within your custom module to alter forms even before theming takes place. For getting the ID of the form in question, just debug the $formid parameter of the hook_form_alter() while visiting the node.

For adding a custom form to your node you should write a custom module implementing the following hooks:

/**
 * Implements hook_block_info().
 */
function MYMODULE_block_info() {
  $block = array();
  $block['mymodule_form'] = array(
    'info' => t('My Form'),
  );
  return $block;
}

/**
 * Implements hook_block_view().
 */
function MYMODULE_block_view($delta = '') {
  $block = array();
  if ($delta == 'mymodule_form') {
    $block['content'] = drupal_get_form('MYMODULE_my_awesome_form');
    $block['subject'] = t('My Form Title');
  }
  return $block;
}

/**
 * My Form
 */
function MYMODULE_my_awesome_form($form, $form_state) {
  $form = array();

  // define your form here...

  return $form;
}

/**
 * My Form Submit Handler
 */
function MYMODULE_my_awesome_form_submit($form, $form_state) {
  // do your magic on the submitted form values in $form_state['values']
}

Then you will find the form within /admin/structure/block and can add it to the desired page region of your node. Filter the node within the block settings by choosing 'Only the listed pages' under Pages/Show block on specific pages and entering your node/14.

14
  • I just want to set up a form in node--14.tpl.php and capture what is posted, validate then write it to the database. Simple with PHP and I expect with Drupal if I knew how but I can't even display the form Commented Jan 29, 2016 at 12:59
  • For showing a new custom form on a node you should write a small module utilizing blocks. (1) Use hook_block_info() to define a block. (2) Use hook_block_view() to define its output. (3) Return a custom form as output of your block. (4) Add a submit handler for this form to use the submitted data. (5) Add the block to your node using structure/blocks and filter to show on your node only. It's quite an amount of code, so I will add it later to my above reply. Commented Jan 29, 2016 at 13:07
  • Please with just say a couple form fields as an example then I can just change it the fields I need as I am totally in the dark at the moment. I am a bit of a Newby Commented Jan 29, 2016 at 13:31
  • Don't worry. Noticed that already. ;) Please check my above edits. For a reference of how to define your form fields / submit buttons, I'd suggest api.drupal.org/api/drupal/…. Commented Jan 29, 2016 at 13:33
  • Right so I need a library called modules under the theme then another library called MYMODULE or whatever I want to call it then is it a MYMODULE.inc file under there? Commented Jan 29, 2016 at 13:49

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