views:

917

answers:

3

I have a data table which is populated with data unrelated to drupal content (from a third party system). The data relates to photos which must be approved / flagged as inappropriate.

So I'm writing a Drupal admin module which should moderate this content. So far, I have built a table using theme('table',...) which shows 1 photo per row, along with some other meta data. I now want to include some form buttons in the table and build some Ajax actions which are triggered by those buttons.

This article looks relevant http://drupal.org/node/112358 but I'm concerned this isn't the Drupal 6 way of doing things.

Can anyone offer some advice on how best to approach this problem - preferably using core modules / form override functions. Drupal version is 6.14.

many thanks,

codecowboy.

A: 

Why are you using the standard theme_table() function? Just make your own theme function that includes the form elements you need. More on theme functions here and here.

theunraveler
Thanks for your reply but it doesn't really help me. I am new to drupal so need a bit more assistance than just a link to the docs. Could you maybe explain what you mean with some basic steps and some pseudo code? thanks!
codecowboy
+1  A: 

Custom theme functions allow you to render content in a completly custom way. You can also create a custom template for your content, this could include the buttons.

Hook_theme() will allow you to create your own content type to add to the theme function so you could have theme('moderatedimages').

A work around would be to put the html for the moderate buttons into the table data so that it is output by theme table. This would save you having to write your own theme fuction.

For the ajax calls you will need to build your own menu callback using hook_menu and a custom function. (Code snippents from this tutorial)

<?php
function mymodule_products_menu() {

  $items = array();

  $items['products/get'] = array(
    'title' => 'mymodule callback',
    'page callback' => 'mymodule_myfunction,
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK
  );

  return $items;
}

This will call the function mymodule_myfunction one thing to remember about his function is that you want to print the result and exit. you may want to use drupal_json() to encode the response.

Jeremy French
A: 

Hi, I had the same problem. I found the solution in drupal-directory/modules/menu/menu.admin.inc. The Form-API and Themes will be used only!

Implementation: -write your query with scheme-api (I think you did it already for your theme('table',...) ) -for each row create a form like this: $form[$row->id]['column_name_1'] = array(...here description of the column -> checkbox, textfield...); $form[$row->id]['column_name_2'] = array(...); -write your own theme-funktion, witch use hook_theme_table (you habe already did it, you need only to change some cells into checkboxes or other form-elements.)

I write a submit-function now, but it doesn't work :-) For more information see the menu-modul.

Hier my code:

/**   
  * Form for editing the event types.
  *  
  * @ingroup forms  
  */
  function mymodule_event_type_overview_form() {  
    $vid = variable_get('mymodule_category_vocabulary', '');
    $sql = "SELECT term_data.tid AS tid,
      term_data.name AS tname,
      term_data.vid AS vid,
      term_site_config.color AS color,
      term_site_config.site_enabled AS site_enabled,
      term_site_config.site_shown AS site_shown
      FROM {term_data} term_data
      INNER JOIN {term_site_config} term_site_config 
      ON term_data.tid = term_site_config.tid
      WHERE term_data.vid = %d";

    $result = db_query(db_rewrite_sql($sql), $vid);

  $form = array();   while ($term = db_fetch_object($result)) {
    $form[$term->tid]['tname'] = array(
      '#type' => 'value',
      '#value' => $term->tname,
    );
    $form[$term->tid]['color'] = array(
      '#type' => 'value',
      '#value' => $term->color,
    );
    $form[$term->tid]['enabled'] = array(
      '#type' => 'checkbox',
      '#default_value' => $term->site_enabled,
    );
    $form[$term->tid]['shown'] = array(
      '#type' => 'checkbox',
      '#default_value' => $term->site_shown,
    );

    // Build a list of operations.
    $operations = array();
    $operations['delete'] = l(t('delete'), 'admin/settings/mymodule/eventtype/'. $term->tid .'/delete');
    $operations['edit'] = l(t('edit'), 'admin/settings/mymodule/eventtype/'. $term->tid .'/edit');

    $form[$term->tid]['operations'] = array();
    foreach ($operations as $op => $value) {
      $form[$term->tid]['operations'][$op] = array('#value' => $value);
    }   }
     if (element_children($form)) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save configuration'),
    );
    $form['reset'] = array(
      '#type' => 'submit',
      '#value' => t('Reset to defaults'),
    );
    if (!empty($_POST) && form_get_errors()) {
      drupal_set_message(t('The settings have not been saved because of the errors.'), 'error');
    }   }else {
    $form['empty'] = array('#value' => t('There are no event types yet.'));   }
     return $form; }

/**  
  * Theme the event type overview form into a table.  
  *
  * @ingroup themeable  
  */
function theme_mymodule_event_type_overview_form($form) {

  $header = array(
    t('Event type'),
    t('Color'),
    array('data' => t('Enabled'), 'class' => 'checkbox'),
    array('data' => t('Shown'), 'class' => 'checkbox'),
    array('data' => t('Operations'), 'colspan' => '2'),   );

  $rows = array();   foreach (element_children($form) as $id) {
    $element = &$form[$id];
    if (isset($element['tname'])) {
      $row = array(
        t($element['tname']['#value']),
        t($element['color']['#value']),
        array('data' => drupal_render($element['enabled']), 'class' => 'checkbox'),
        array('data' => drupal_render($element['shown']), 'class' => 'checkbox'),
        array('data' => drupal_render($element['operations']['delete'])),
        array('data' => drupal_render($element['operations']['edit'])),
      );

    $rows[] = $row;   }   }   $output = '';   if ($rows) {
    $output .= theme('table', $header, $rows);   }   
    $output .= drupal_render($form);   
    return $output; 

}

You will get the html-code of the form if you call drupal_get_form('mymodule_event_type_overview_form');

and don't forget co write following function in mymodule.module:

/** * Implementation of hook_theme(). */ function mymodule_theme() {
return array( 'mymodule_event_type_overview_form' => array( 'arguments' => array(), ), ); }

Have fun :-) Katja

Katja