-
List of articles
Hello
I am looking for an example to add the list of article in a back end module in order to select one.
I have the list of the page, but I don't understand how to do to get the content ...
Can anyone could help me to realize it ?
here is the field I have added in the palette from which I can select a page.
Code:
/**
* Add fields
*/
$GLOBALS['TL_DCA']['tl_module']['fields']['popcontent_content'] = array
(
'label' => &$GLOBALS['TL_LANG']['tl_module']['popcontent_content'],
'exclude' => true,
'inputType' => 'pageTree',
'eval' => array('fieldType'=>'radio', 'helpwizard'=>false, 'tl_class'=>'clr'),
'doNotCopy' => true,
);
Thanks in advance
-
Re: List of articles
Hi,
Contao allows you to select an Article as a Content Element. To do this it does exactly what you are trying to do - get a list of articles into a select box.
The code for this is located in system/modules/core/dca/tl_content.php.
Here is the code which i've just taken from the file mentioned above:
Code:
'article' => array
(
'label' => &$GLOBALS['TL_LANG']['tl_content']['article'],
'exclude' => true,
'inputType' => 'select',
'options_callback' => array('tl_content', 'getArticles'),
'eval' => array('mandatory'=>true, 'chosen'=>true, 'submitOnChange'=>true),
'wizard' => array
(
array('tl_content', 'editArticle')
),
'sql' => "int(10) unsigned NOT NULL default '0'"
),
You see the 'options_callback' parameter passes a class name 'tl_content' and a method name 'getArticles'. This returns the list of articles to the select box.
This class is located in the same file, you can find it at the bottom. Here is the code for the class - I've only included the bit you need, you can see the whole thing by looking in the file I mentioned above:
Code:
class tl_content extends Backend
{
/**
* Import the back end user object
*/
public function __construct()
{
parent::__construct();
$this->import('BackendUser', 'User');
}
/**
* Get all articles and return them as array (article teaser)
* @param \DataContainer
* @return array
*/
public function getArticles(DataContainer $dc)
{
$arrPids = array();
$arrArticle = array();
$arrRoot = array();
$intPid = $dc->activeRecord->pid;
if (Input::get('act') == 'overrideAll')
{
$intPid = Input::get('id');
}
// Limit pages to the website root
$objArticle = $this->Database->prepare("SELECT pid FROM tl_article WHERE id=?")
->limit(1)
->execute($intPid);
if ($objArticle->numRows)
{
$objPage = PageModel::findWithDetails($objArticle->pid);
$arrRoot = $this->Database->getChildRecords($objPage->rootId, 'tl_page');
array_unshift($arrRoot, $objPage->rootId);
}
unset($objArticle);
// Limit pages to the user's pagemounts
if ($this->User->isAdmin)
{
$objArticle = $this->Database->execute("SELECT a.id, a.pid, a.title, a.inColumn, p.title AS parent FROM tl_article a LEFT JOIN tl_page p ON p.id=a.pid" . (!empty($arrRoot) ? " WHERE a.pid IN(". implode(',', array_map('intval', array_unique($arrRoot))) .")" : "") . " ORDER BY parent, a.sorting");
}
else
{
foreach ($this->User->pagemounts as $id)
{
if (!in_array($id, $arrRoot))
{
continue;
}
$arrPids[] = $id;
$arrPids = array_merge($arrPids, $this->Database->getChildRecords($id, 'tl_page'));
}
if (empty($arrPids))
{
return $arrArticle;
}
$objArticle = $this->Database->execute("SELECT a.id, a.pid, a.title, a.inColumn, p.title AS parent FROM tl_article a LEFT JOIN tl_page p ON p.id=a.pid WHERE a.pid IN(". implode(',', array_map('intval', array_unique($arrPids))) .") ORDER BY parent, a.sorting");
}
// Edit the result
if ($objArticle->numRows)
{
System::loadLanguageFile('tl_article');
while ($objArticle->next())
{
$key = $objArticle->parent . ' (ID ' . $objArticle->pid . ')';
$arrArticle[$key][$objArticle->id] = $objArticle->title . ' (' . ($GLOBALS['TL_LANG']['tl_article'][$objArticle->inColumn] ?: $objArticle->inColumn) . ', ID ' . $objArticle->id . ')';
}
}
return $arrArticle;
}
}
The code in that method fetches the articles. It does a few extra things too like checking that the user has access to the articles and the pages that the articles are placed on.
The end result is that it returns an array of articles to your field which are then listed in your select box.
The code formatting isn't great on this post, so i'd recommend you open up system/modules/core/dca/tl_content.php in your editor and have a look.
Note: If you're not using Contao 3, the file will be located in a different place - system/modules/backend/dca/tl_content.php
-
Re: List of articles
-
Re: List of articles
including the palette and only the getArticle function, I've got a list (dropdown) of only one root of the web site.
How could I have the complete tree presented like the pages ?
-
Re: List of articles
You can only use the tree with pages, files and folders but if you need to be able to select multiple articles you can add 'multiple' to your field's 'eval':
Code:
'eval' => array('mandatory'=>true, 'chosen'=>true, 'multiple'=>true),
That will let you select multiple articles, depending on how many there are you might also want to change the field type to 'checkbox'.
-
Re: List of articles
In fact I just need to select 1 article to have its reference.
For esthetic reason I would like to present the complete structure (2 roots, one per language) in the same orden rather than in an alphabetical orden...
It's not possible to generate the tree list of article, like in the article page where you can edit, move, delete ... ?
-
Re: List of articles
Unfortunately no, you can't use that as a field in your DCA. The Articles section in the backend of Contao is a list of records rather than a form field.
The file and page tree can be used as fields because Contao provides these as field types. If you visit https://contao.org/en/manual/3.0/dat...html#reference you can see a list of the available field types Contao offers for a DCA.
Here's the list:
text - Text field
password - Password field
textarea - Textarea
select - Drop-down menu
checkbox - Checkbox
radio - Radio button
radioTable - Table with images and radio buttons
inputUnit - Text field with small unit drop-down menu
trbl - Four text fields with a small unit drop-down menu
chmod - CHMOD table
pageTree - Page tree
fileTree - File tree
tableWizard - Table wizard
listWizard - List wizard
optionWizard - Option wizard
moduleWizard - Module wizard
checkboxWizard - Checkbox Wizard