oldGregg icon indicating copy to clipboard operation
oldGregg copied to clipboard

Load article section on $publishedArticles indexJournal.tpl

Open geniusdesignbrasil opened this issue 2 years ago • 5 comments

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

geniusdesignbrasil avatar Aug 17 '22 20:08 geniusdesignbrasil

Something like:

$sectionDAO = DAORegistry->getDao('SectionDAO')
$section = $section->getById($publishedArticle->getData('sectionId'))';
$localizedSectionTitle = $section->getLocalizedTitle();

Vitaliy-1 avatar Aug 18 '22 11:08 Vitaliy-1

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'));

geniusdesignbrasil avatar Aug 18 '22 12:08 geniusdesignbrasil

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 avatar Aug 18 '22 13:08 Vitaliy-1

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));
}

`

geniusdesignbrasil avatar Aug 18 '22 13:08 geniusdesignbrasil

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

Vitaliy-1 avatar Aug 18 '22 13:08 Vitaliy-1