1

I am trying to theme a custom using a template file but unfortunately I am not getting it by using the below method. listing.module

function listing_menu() {
  $items['ad-listing'] = array(
    'page callback' => 'adlist_template',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,

  );
      return $items;
    }
function listing_form($form, &$form_state) {

$form['title'] = array(
    '#type' => 'textfield',
    '#attributes' => array('placeholder' => t('Title')),
);
$form['category'] = array(
    '#type' => 'select',
    '#options' => array(
        'default' => '- Category -',
        'Car' => 'Car',
        'Van' => 'Van',
    ),
);
 return $form
}

function listing_theme($existing, $type, $theme, $path){
return array(
  'listing_form' => array(
    'render element' => 'form',
    'template' => 'adlisting',
    'path' => drupal_get_path('module', 'listing') . '/form',
  )
    );
}
function adlist_template(){
    return theme('listing_form');
}

listing/form/adlisting.tpl.php

<div class="row">
  <div class="small-12 medium-12 large-8">

      <div class="row">
          <div class="small-12 columns">
              <h5 class="">Contact Information</h5>
          </div>
      </div>

      <div class="row">
          <div class="small-12 large-6 medium-6 columns">
              <?php print render($form['title']); ?>
          </div>
      </div>

      <div class="row">
          <div class="small-12 medium-6 large-6 columns">
              <?php print render($form['category']); ?>
          </div>

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

I tried to follow this thread How to a use a template file to theme a form? but did not help me, neither other solutions

  • 2
    Why do you want to use a custom template to render a form, I just asking because if all you want is add custom classes or wrapper around form fields, you can pretty much do that with FAPI using 'fieldset'/'container' for wrappers and '#attributes' for clasess; – arpitr Sep 18 '15 at 18:40
  • 1
    Hey there, try changing the name of listing_form() to something like listing_myform() and flush your caches. I'm assuming your module is listing so by naming your function listing_form you are actually implementing hook_form which I'm pretty sure you do not want to do. – othermachines Sep 18 '15 at 19:53
  • @othermachines - Of course he wants to implement hook_form. Why not? – leymannx Sep 18 '15 at 21:13
  • 3
    @leymannx AFAIK hook_form is a node API hook (if I'm wrong feel free to enlighten me). This function above is a theme function. I honestly don't know if it would even be problematic - since obviously blah_form is a popular naming convention - just trying to eliminate possible gotchas. – othermachines Sep 18 '15 at 21:58
  • Now it dawns on me that of course the node API hook would only be called for node modules, so never mind. @DropDragon So are you getting a 404 or is it something else? – othermachines Sep 18 '15 at 22:11
2

There were 2 major changes on the code above

  1. I changed the form name to listing_adform()
  2. I changed the hook_menu to call my form not theme
  $items['ad-listing'] = array(
    //'page callback' => 'adlist_template',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('listing_adform'),
  );

Flushed the cache, and I saw my fields rendering using my template in form/adlisting.tpl.php

|improve this answer|||||
  • Glad you got it sorted, DropDragon. So it helps others, I would recommend including all of the relevant code in your answer. – othermachines Sep 19 '15 at 13:11
  • @othermachines - Thank you for your valid objection on hook_form. I get your point. Since this is no content type defined here, hook_form shouldn't be the naming to be used here. I still wonder, that even on DO hook_form is used in simple form tutorials. – leymannx Sep 20 '15 at 21:13
  • @leymannx Yeah, I wonder about that, too. I guess it's just one of those things that you can get away with in some cases, but not in others, and nobody is exactly sure what makes the difference. :) – othermachines Sep 20 '15 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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