我希望我能让你好起来。要实现您想要的,您需要创建两个前端模块listModule和detailsModule以及两个页面listPage和detailsPage因此在后端(站点结构)中创建这两个页面。您将listModule添加到listPage并将detailsModule添加到detailsPage。
除非您别无选择,否则切勿对页面 ID 进行硬编码。
让我们开始介绍如何通过创建listModule并将其添加到listPage来链接到detailsPage
- 创造
/system/modules/my_module/dca/tl_module.php
- 添加以下代码并保存
$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
- Run the install tool.
- Work on any errors that may arise. If none, go to next
Create the frontend module according to this url Front end module
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
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; ?>
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.
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
- 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