2

I'm new in the use of Drupal and I have some troubles with the module I'm writing. This module generates a form that writes datas in some tables. It works fine, the datas are correctly writen in the database.

My module.module file contains the following functions :

function myform_xml_menu()

function myform_xml_form($form, &$form_state)

function myform_xml_form_submit($form, &$form_state)

The big question is : how to display the submitted datas ?

I've already tryed writing a second module called "Display" :

<?php

function display_menu(){
$items['display'] = array(
    'title' => 'Display',
    'page callback' => 'display_contents',
    'access callback' => TRUE
);

return $items;
}

function display_contents(){

$query = db_select('form_xml', 'f');
$result = $query
        ->fields('f')
        ->execute()
        ->fetchAll();

$output = '<table><th>Id</th><th>Name</th><th>Description</th></tr>';

foreach($result as $key){
    $output .= '<tr>';
    $output .= '<td>'.$key->id.'</td>';
    $output .= '<td>'.$key->name.'</td>';
    $output .= '<td>'.$key->description.'</td>';
    $output .= '</tr>';
} 
$output .= '</table>';

return $output;
}

It adds a menu item and display my results correctly too. But I feel making it wrong or unproperly... I've read many docs speaking about displaying module's results in blocks or pages.

How can I embed my results in a Drupal page in order to be able to manage its display using a .tpl file ?

2

You need to use hook_theme in your module to manage content in tpl file.

Example,

Creating template file using the hook_theme function

    /**
    * Implements hook_theme
   */

    function example_theme(){
      return array(     
        'exampletheme' => array(
            'template' => 'exampletheme',
        'arguments' => array('example_array' => NULL),
          ),
      );
    }

Now, we can use "exampletheme" theme to display content. Below is the page callback function for the menu item.

 function example_callback(){

  $array = array(1,2,3); 

  return theme('exampletheme',  $array);
}

Now, you need to create "exampletheme.tpl.php" file and you can render the "example_array" in the tpl file.

Your Answer

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