0

This is the code in my template.php file.

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;
}


 function custom_add_property_form($form, &$form_state) {
    $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',
    );

    return $form;
}

This is the code in node--14.tpl.php.

Contact Information
<div class="row">
    <div class="small-12 medium-12 large-8 large-offset-2 columns">
        <?php print render($form['submit']); ?>
    </div>
</div>

But no fields are appearing - what am I doing wrong? You can't have a straight forward in a tpl.php file can you?

1
  • All I want is a form in a .tpl.php file and to be able to access the fields when they are posted. You can't use <form></form> and no fields show up if I use $items['add_property_form'] = array( 'render element' => 'form', 'template' => 'node--14', 'path' => drupal_get_path('theme', 'studiga_theme') . '/templates/node', ); Jan 28, 2016 at 17:39

2 Answers 2

2

Given your definition of studiga_theme_theme(), Drupal will never call custom_add_property_form() because there isn't any relation between the theme template you define, and that function. Eventually, you could implement the preprocess function for that theme template, and set there a variable that is passed to the template.

function studiga_theme_preprocess_add_property_form(&$variables) {
  $variables['form'] = /* define here the form */;
}

Now your theme template will get a $form variable.

Keep in mind that, except in a case, themes don't define forms; that is the task for a module. Eventually, themes can alter a form provided by a module, but (generally speaking) you will never see a theme that defines a form that is not for its own settings.

4
  • I was trying to follow drupal.stackexchange.com/questions/312/… Jan 28, 2016 at 18:22
  • I am confused - how does function studiga_theme_preprocess_add_property_form know which tpl.php to use? Jan 28, 2016 at 18:25
  • It's not the function that knows which template file to use; it's Drupal that calls that preprocess function for a theme / template named add_property_form. See node.tpl.php for which Drupal calls a preprocess function like mymodule_preprocess_node() o mytheme_preprocess_node(), where mymodule and mytheme are respectively an enabled module and a theme.
    – apaderno
    Jan 28, 2016 at 19:10
  • In that case, hook_theme() contains an entry for custom_donate_form too. Then, it has not been explicitly said, but that custom_donate_form() is an example of module code. A theme can just define its settings form, not any other form.
    – apaderno
    Jan 28, 2016 at 19:58
0

I think your theme name(studiga_theme) is wrong. Try making it just "studiga". "_theme" is reserved word in Drupal. I also faced similar issue once. Hope that will resolve your problem.

4
  • The short name of the theme is not a problem, if the theme doesn't define a studiga_theme() function, and there isn't a studiga module. Otherwise, studiga_theme() would be understood from Drupal as hook_theme() implementation of the studiga module.
    – apaderno
    Jan 28, 2016 at 17:47
  • The name of the theme is studiga_theme Jan 28, 2016 at 18:20
  • I know. As I said, that would be a problem only in a case. That is not the reason of the issue the OP is having, though.
    – apaderno
    Jan 28, 2016 at 19:05
  • function studiga_theme_preprocess_add_property_form(&$variables) { $form = $variables['form']; $form['first_name'] = array( '#type' => 'textfield', '#attributes' => array('placeholder' => t('First name')), ); } and <?php print render($form['first_name']); ?> in the tpl.php but no input field shows up Jan 29, 2016 at 10:15

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.