In Drupal 7 API there is a chapter dedicated to site theming. All elements of a site, without exclusion, should go through the theming process. But first of all theming itself should be set up. From a Drupal developer's point of view, almost everything is done with the help of functions and there are many of them that facilitate theming in Drupal 7, too. The most important one is the theme() function. We'll look at 2 of them. They are:
- theme_table() – function for table formatting.
- theme_fieldset() – function for drop-down list.
Our tasks are as follows:
- To make a table with static data sized 3/3 with the help of theme_table().
- To organize output of all site articles that have the following fields: article name (in an active link) and link for editting the article. To draw up a similar table to sit within reach with a link to perform the article deletion.
- To outlet 2 drop-down lists using theme_fieldset() where in list 1 a description to the table is given which relates to node editting, and list 2 outlets information about table of node deleting.
- To make 2 drop-down lists that will contain our tables.
Task 1
Let's start with the theme_table().
Let's create a file of our module, which in our case will be table_page.module file. First of all we need to create a page where our table will be situated. In order to create a page we use hook_menu, in our case it will look the following way:
php /** * Implements hook_menu(). */ // function declaration function table_page_menu() { // we create variable that indicates that page path will be «our_site/ table», // our page has the following parameters $items['table'] = array( // page name 'title' => t('Page with table'), // function that forms data for a table 'page callback' => 'main_table', // access is true of course 'access arguments' => array('TRUE'), ); // let's not forget to return our variable return $items; } /** * Function main_table(). */ // create function for data outlet for the created page function main_table() { // we'll leave the page empty for now return ''; }
Now it's time to remember that a table in html is initiated with table tags, then the table lines are formed with the help of tr tag and later the cells are created, again with td tag. But it's not us who will have to form something, because function theme_table() will work with tags, and we'll set the function itself.
Every table must have a header. $header will consist of 1 row and 3 columns in the row.
That's how it should be done:
php //we create a variable that contains an array $header = array( // now we create another array where data of first cell is situated array('data' => t('Header cell1')), // second cell array('data' => t('Header cell2')), // and third cell array('data' => t('Header cell3')) );
Table header is created, now let's work with table body. We'll create 2 rows that will have 3 columns each.
php // creating first row $row [] = array( // output 1 cell in 1 row array('data' => t('Row 1 – Cell 1')), // second cell array('data' => t('Row 1 – Cell 2')), // third cell array('data' => t('Row 1 – Cell 3')) ); //output of second row $row [] = array( // cell 1 array('data' => t('Row 2 – Cell 1')), // cell 2 array('data' => t('Row 2 – Cell 2')), // cell 3 array('data' => t('Row 2 – Cell 3')) );
Now that we've completed the data input, we need to indicate the functons correctly. In our case the function has to contain the following type:
php // we use theme function and assign table parameter to it theme('table', //creating array for data array( // value header will be in $header 'header' => $header, // value rows will be taken out of $rows 'rows'=> $rows ) );
Now we have the general view of our code as follows:
php /** * Implements hook_menu(). */ function table_page_menu() { $items['table'] = array( 'title' => t('Page with table'), 'page callback' => 'main_table', 'access arguments' => array('TRUE'), ); return $items; } /** * Function main_table(). */ function main_table() { // creating value that contains array $header = array( // creating array that contains data from first cell array('data' => t('Header cell1')), // second cell array('data' => t('Header cell2')), // and third cell array('data' => t('Header cell3')) ); // creating first row $rows[] = array( // output of first cell in 1 row array('data' => t('Row 1 – Cell 1')), // second cell array('data' => t('Row 1 – Cell 2')), // third cell array('data' => t('Row 1 – Cell 3')) ); //second row output $rows[] = array( array('data' => t('Row 2 – Cell 1')), array('data' => t('Row 2 – Cell 2')), array('data' => t('Row 2 – Cell 3')) ); return theme('table', array('header' => $header, 'rows'=> $rows)); }
The general view will be the following: