Search code examples
formstemplatesdrupal-7admintaxonomy

Load Form from module into custom page template


I have successfully added my own form (from the same module) into my custom template, but now I wish to load the taxonomy add term form (used by ubercart I think for product categories in the catalog vocab) into my template.

I have gotten this far with my module - filename simpleadmin.module

/**
 * @file
 * A module to simplify the admin by replacing add/edit node pages
 */

function simpleadmin_menu() {
  $items['admin/products/categories/add'] = array(
    'title' => 'Add Category',
    'page callback' => 'simpleadmin_category_add',
    'access arguments' => array('access administration pages'),
    'menu_name' => 'menu-store',
  );
  return $items;
}

function simpleadmin_category_add() {
module_load_include('inc', 'taxonomy', 'taxonomy.admin');
$output = drupal_get_form('taxonomy_form_term');
return theme('simpleadmin_category_add', array('categoryform' => $output));
}

function simpleadmin_theme() {
  return array(
    'simpleadmin_category_add' => array(
      'template' => 'simpleadmin-template',
      'variables' => array('categoryform' => NULL),
      'render element' => 'form',
    ), 
  );
}
?>

And as for the theme file itself - filename simpleadmin-template.tpl.php, only very simple at the moment until I get the form to load into it:

<div>
    This is the form template ABOVE the form
</div>


<?php
dpm($categoryform);
print drupal_render($categoryform);
?>

<div>
    This is the form template BELOW the form
</div>

Its telling me that it is

Trying to get property of non-object in taxonomy_form_term()

and throwing up an error. Should I be using node_add() and passing the nodetype?


Solution

  • To render a taxonomy term form, the function should be able to know the vocabulary to which it belongs to. Otherwise how would it know which form to show? I think this is the proper way to do it.

    module_load_include('inc', 'taxonomy', 'taxonomy.admin');
    if ($vocabulary = taxonomy_vocabulary_machine_name_load('vocabulary_name')) {
      $form = drupal_get_form('taxonomy_form_term', $vocabulary);
      return theme('simpleadmin_category_add', array('categoryform' => $form));
    }
    

    To redirect your form use hook_form_alter

    function yourmodule_form_alter(&$form, &$form_state, $form_id) {
      //get your vocabulary id or use print_r or dpm for proper validation
        if($form_id == 'taxonomy_form_term' && $form['#vocabulary']['vid'] = '7' ){ 
      $form['#submit'][] = 'onix_sections_form_submit'; 
     }
    }
    
    function yourmodule_form_submit($form, &$form_state) {
         $form_state['redirect'] = 'user';
    }