oldGregg
oldGregg copied to clipboard
Load article section on $publishedArticles indexJournal.tpl
https://github.com/Vitaliy-1/oldGregg/blob/bb7e64afba758d3ea496e6cfcac7a81b8f6bfcb0/templates/frontend/pages/indexJournal.tpl#L145
Hi @Vitaliy-1 !
First of all, thank you very much for your theme. I'm trying to load article section on $publishedArticles
- but without success. How can I load it?
Thank you
Something like:
$sectionDAO = DAORegistry->getDao('SectionDAO')
$section = $section->getById($publishedArticle->getData('sectionId'))';
$localizedSectionTitle = $section->getLocalizedTitle();
Thank you @Vitaliy-1 but it doesn't worked.
I used on browseLatest
function:
$sectionDAO = DAORegistry::getDAO('SectionDAO');
$section = $section->getById($publishedArticle->getData('sectionId'));
$localizedSectionTitle = $section->getLocalizedTitle();
and returned this error:
Fatal error: Uncaught Error: Call to a member function getById() on null in themePlugin.inc.php
on line $section = $section->getById($publishedArticle->getData('sectionId'));
This is PHP code. In Smarty templates the syntax is different, you'll need to adapt it if you want to use it there
This is PHP code. In Smarty templates the syntax is different, you'll need to adapt it if you want to use it there
@Vitaliy-1
Sorry, but I am not a php dev and not very familiar with smarty templates. I did not use this code on smarty template (.tpl file), but on themePlugin.inc.php file and the error has returned.
I add the code you send to me on browseLatest function. Is it right?
` public function browseLatest($hookName, $args) {
$smarty = $args[0];
$template = $args[1];
if ($template != 'frontend/pages/indexJournal.tpl') return false;
/* get number of latest article to display from user input; if there was none - use default */
$latestArticles = $this->getOption("latestArticlesNumber");
if (is_null($latestArticles)) {
$latestArticles = OLDGREGG_LATEST_ARTICLES_DEFAULT;
} else {
$latestArticles = intval($latestArticles);
}
/* retrieve current journal id from the request */
$request = $this->getRequest();
$journal = $request->getJournal();
$journalId = $journal->getId();
-> $sectionDAO = DAORegistry::getDAO('SectionDAO'); -> $section = $section->getById($publishedArticle->getData('sectionId')); -> $localizedSectionTitle = $section->getLocalizedTitle();
/* retrieve latest articles */
$publishedArticleObjects = Services::get("submission")->getMany([
'status' => STATUS_PUBLISHED,
'contextId' => $journalId,
'count' => $latestArticles,
'orderBy' => 'datePublished',
]);
$smarty->assign('publishedArticles', iterator_to_array($publishedArticleObjects));
}
`
You can assign section title dynamically as a property to submission objects, e.g.:
$sectionDAO = DAORegistry::getDAO('SectionDAO');
$publishedArticles = [];
foreach (iterator_to_array($publishedArticleObjects) as $publishedArticle) {
$section = $section->getById($publishedArticle->getData('sectionId'));
$publishedArticle->localizedSectionTitle = $section->getLocalizedTitle();
$publishedArticles[] = $publishedArticle;
}
$smarty->assign('publishedArticles', $publishedArticles);
And then retrieve it in the tpl
by calling the dynamically created property. But this approach is going to be deprecated from PHP 8.2, see: https://wiki.php.net/rfc/deprecate_dynamic_properties