Drupal 7 Module Development Tutorial #7 - Theming Tables with Drupal's Form API

preview_player
Показать описание


In the 7th drupal video tutorial of this 10 part series, I show you how we can use Drupal's form api to create a table with check boxes and administer our flag applications. Using the form api, we create a tableselect element, check box form element and submit button. In doing so, I also show you the l function and briefing discuss Drupal theme functions.
Рекомендации по теме
Комментарии
Автор

Took me forever to learn what you laid out here. Thanks again for filling in the gaps.

jeffguroo
Автор

Thanks Peter, everything you explained was very helpful. Is there any way for call a function using l. Also what are the different ways in which we can render a page to display data from database.

maheshkharote
Автор

Thanks Peter, your tutorials are extremely helpful!  I'm hoping that you can add to this one just a little bit.   I'm looking to not only have pagination (which this tutorial covers wonderfully) but to also add a sort by column header.  

I have found the ->extend('TableSort') option and read that it should be the answer to my question... it doesn't seem to work though. 

Any chance you have exposure with this one?

rolandreedy
Автор

Thank you! This series is really helpful!

I need help....I am getting 'Undefined offset: 0 in theme_tableselect() ' and table is not showing any results..only the Approve, deny drop-down and check boxes ..I can see!

sheetalmore
Автор

Great tutorial. One thing That confused me was why you need to call theme('pager') to print the pager you generated from the database query.

andrewbickford
Автор

Thank you, Peter! Great tutorials!

I wonder if you can add a form to a page using the same logic. I tried
function mymodule_menu() {
  $items['userinfo] = array(
    'title' => 'My Form',
    'description' => 'Form desc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('mymodule_userinfo_form'),
    'file' => 'mymodule_forms.inc',
  );
  return $items;
}

and visiting the page previously created at the node "userinfo" i don't see the form. But if I change $items['userinfo'] to $items['userinfo/hi'] and visit the node 'userinfo/hi' (which is not associated with any content) I see the form

danielerosa
Автор

Hi. Awesome tutorial. Is the final code for this one (Module Dev #7) available? I typed out the whole thing and can't get it to run properly. The  select element shows on the page and a checkbox shows, but no results from the query.  I know there are results in the database because I saw them output as part of what I did for video #6. Also, there are a bunch of warnings (see below). I'm not sure how to debug it. I'll paste the warnings and code below.

Warnings when I go to
Warning: Invalid argument supplied for foreach() in theme_tableselect() (line 3425 of
Warning: array_unshift() expects parameter 1 to be array, string given in theme_tableselect() (line 3435 of
Warning: Invalid argument supplied for foreach() in tablesort_get_order() (line 206 of
Warning: reset() expects parameter 1 to be array, string given in tablesort_get_order() (line 220 of
Warning: Invalid argument supplied for foreach() in tablesort_get_order() (line 206 of
Warning: reset() expects parameter 1 to be array, string given in tablesort_get_order() (line 220 of
Warning: Invalid argument supplied for foreach() in tablesort_get_sort() (line 248 of
Warning: Invalid argument supplied for foreach() in theme_table() (line 2045 of

---Here's my flag_application.admin.inc:
<?php

/**
*
* Flag Application administration
*/

function flag_application_form($form, $form_state){
    //dpm (debug_backtrace());
    $form = array();
// Build the sortable table header.
  $header = array(
    'title' => t('Flagged Content'),
    'name' => t('Username'),
    'entity_id' => t('Node ID'),
    'uid' => t('User ID'),
    'flagging_id' => t('Flag ID'),
    'status' => t('Status'),
  );

// select table flag_application with alias 'fa' and join with flagging table as 'f'alias
$sql = db_select('flag_application',
$sql->join('flagging', 'f', 'f.flagging_id = fa.flagging_id');
$sql->join('node', 'n', 'f.entity_id = n.nid');
$sql->join('users', 'u', 'f.uid = u.uid');
$sql->fields('fa', array('status'))
    ->fields('f', array('entity_id', 'timestamp', 'flagging_id'))
    ->fields('n', array('title'))
    ->fields('u', array('name', 'uid'))
    ->orderBy('timestamp', 'DESC')
    ->limit(25);
$results = $sql->execute();

$rows = array();

foreach($results as $result){
    switch ($result->status){
        case 0:
            $status = 'Pending';
            break;
        case 1:
            $status = 'Approved';
            break;
        case 2:
            $status = 'Denied';
            break;
    }
    // for each row in the results, get all the data for that row
    // The order matters (make it same as you made for the headers)
    $rows[] = array(
        // l() makes links
        'title' => l($result->title, 'node/'.$result->entity_id),
        'name' => l($result->name, 'user/'.$result->uid),
        'entity_id' => $result->entity_id,
        'uid' => $result->uid,
        'flagging_id' => $result->flagging_id,
        'status' => $status,
        '#attributes' => array('class' => array('application-row')),     
        );
    } //end foreach

   = array(
        '#type' => 'select',
        '#title' => 'Actions',
        '#options' => array(
          1 => t('Approve'),
          2 => t('Deny'),
        ),
    );
    
   = array(
        '#type' => 'tableselect',
        '#header' => 'header',
        '#options' => $rows,
        '#empty' => t('No applications found.'),
        '#attributes' => array('class' => array('applications')),
        );


    $form['pager'] = array('#markup' => theme('pager'));

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
    );


return $form;
}

gp
Автор

Jeez. As a beginner this video probably contained exactly what I needed to know, but sorry, you were speed reading and mumbling - 1/10 for presentation skills. Perhaps a rehearsal would have been a good idea too! All I'm trying to do is get output from a table I have created displayed in a side-bar on the home screen. Just a straightforward table - no check boxes or other paraphernalia and only two columns. It shouldn't involve all this. I have used CMS systems in the past to do this and I get databases. Drupal is just complicated. Having said that, I really am trying to give it a chance!

gmaff