contao

Reading and showing data from a custom module


I'm new to a Contao project, and the only dev at the project....

A custom module is created. For this custom module there is a data entry form (defined in a php file in the dca folder), in which a user can enter all the data which is then stored in a custom table. The code for the module follows the layout stated in the blogpost 'Create a custom module - the basics' (http://blog.qzminski.com/article/create-a-custom-module-the-basics.html).

I've seen a PHP page (in the templates folder) which gets all the data from the custom database table (gets autoinjected via a variable?) and formats that in a html table.

That is what is built so far.

Now what I want to do is to create a display page for an individual item. Normally that will be:

I realize this is a large question, but I really don't know where to start. Googling shows me a lot of pages with this warning "This guite was written for Contao 2.x and a lot of it's information is outdated! Read with care and only use as a general-purpose guide." and other pages are in German, which is, despite having learned it for 3 years in highschool, not my usp.

Any help is appreciated.


Solution

  • I hope I get you well. To achieve what you want, you will need to create two frontend modules, listModule and detailsModule and two pages, listPage and detailsPage therefore create these two pages in the backend(site structure). You will add listModule to the listPage and the detailsModule to detailsPage.

    Never hardcode the page id unless you have no option.

    Lets start on how to link to the detailsPage by creating the listModule and adding it to the listPage

    1. Create /system/modules/my_module/dca/tl_module.php
    2. Add the following code and save
    $GLOBALS['TL_DCA']['tl_module']['palettes']['my_module'] = '{title_legend},name,headline,type,linkToDetail;{protected_legend:hide},protected;{expert_legend:hide},guests,cssID,space';
    
    
    $GLOBALS['TL_DCA']['tl_module']['fields']['linkToDetail']=array(
                'label'                   => &$GLOBALS['TL_LANG']['tl_module']['linkToDetail'],
                'exclude'                 => true,
                'inputType'               => 'pageTree',
                'foreignKey'              => 'tl_page.title',
                'eval'                    => array('fieldType'=>'radio'),
                'sql'                     => "int(10) unsigned NOT NULL default '0'",
                'relation'                => array('type'=>'hasOne', 'load'=>'eager')
            );
    

    This will create a column in your custom table that you will use it later to access the page ID in the template

    1. Run the install tool.
    2. Work on any errors that may arise. If none, go to next
    3. Create the frontend module according to this url Front end module

    6. In the compile function do as below

    protected function compile(){
    
            $objItems = $this->Database->execute("SELECT * FROM my_custom_table");
    
            
            if (!$objItems->numRows)
            {
                return;
            }
    
            $arrItems = array();
    
            // Generate item rows
            while ($objItems->next())
            {
    
                $objPage = \PageModel::findPublishedById($objItems->linkToDetail);
    
                $arrItems[] = array
                (
                    'linkToDetail' => $objPage->getFrontendUrl('itemId='.$objItems->id),
                );
            }
    
            $this->Template->items = $arrItems;
        }
    

    please note the itemId parameter added to the url

    1. Create a template /system/modules/my_module/templates/my_template.html5

      and you will be able to access easily the items

    <?php if($this->items): foreach ($this->items as $item): ?>
    
    <div class="item">
    
    <?php if ($item['linkToDetail']): ?>
    
    <a href="<?php echo $item['linkToDetail']; ?>">Please take me to the details page</a>
    
    <?php endif; ?>
    
    </div>
    
    <?php endforeach; endif; ?>
    
    1. Now go to themes -> modules ->new module and create a new module. I am assuming you have followed the instructions in that link on how to add your module to your module list. Assuming you added to the miscellaneous group, select your module. You will see a page picker with label 'linkToDetail'. select the detailsPage you created in the beginning.

    2. Go to articles -> listPage -> new select 'module' as the element type and the choose your listModule above. We are good here. preview your page and you should be good.

    Now lets build the detailsModule and add it to the detailsPage

    1. Follow all the steps above only that in the compile function of the details module, you will select the details data as follows``
    $objItemDetails = $this->Database->execute("SELECT * FROM my_custom_table where id=".\Input::get('itemId'));
    

    step 7,8 and 9 are same.

    This will help in the case where details page changes in times of ID. It makes it dynamic enough.

    Hope this helps