[OJS 3.1.2.1] Add Author’s Country to »article_details.tpl«

@asmecher

I’d like to show the author’s country on the »article_details.tpl« page, so I added this after the »affiliation« section in the template:

{if $author->getCountry()}
	<span class="country">
		({$author->getCountry()|escape})
	</span>
{/if}

But I don’t get the localized version of country, when changing the site language, and even more, I sometimes only get the country code instead of the country full name:
https://tatup.de/index.php/tatup/article/view/3257
and
https://tatup.de/index.php/tatup/article/view/3254

What am I doing wrong?

Thanks, Tobias

No ideas around? It would be of great help!

Hi @twa,

You’ll need to use the CountryDAO to map the country code to the localized country name. Off the top o of my head, that’s this (in PHP):

$countryDao = DAORegistry::getDAO('CountryDAO');
$country = $countryDao->getCountry($author->getCountry());

Regards,
Alec Smecher
Public Knowledge Project Team

Thanks @asmecher, and how do I integrate that into the (Smarty) template?

Thanks,
Tobias

Hi @twa,

That should be added/adapted to the handler class that eventually calls the template you are trying to modify; it’s best not to add that kind of code to the template.

Regards,
Alec Smecher
Public Knowledge Project Team

@asmecher Can you give me a clue of how to do that for the article_details.tpl? Can I integrate something into our child theme?

Changing templates is easy, but now I don’t exactly know where to start this challenge …

Thanks,
Tobias

Hi @twa,

There are two pieces to this:

  1. Finding out what PHP code is responsible for calling the template, and
  2. Determining how best to intervene in that PHP code to add your new functionality.

For the first challenge, we need to search the code for references to the article_details.tpl template. You can use a search tool for this – I use grep:

 $ fgrep -l article_details.tpl `find . -name \*.inc.php` `find . -name \*.tpl`
./templates/frontend/objects/article_details.tpl
./templates/frontend/pages/article.tpl

So ignoring the fact that the template mentions itself, it’s referred to from articles.tpl. That’s still a template, and we’re looking for PHP, so we’ll search one step back, and use the same trick to find out what calls articles.tpl:

$ fgrep -l article.tpl `find . -name \*.inc.php` `find . -name \*.tpl`
./pages/article/ArticleHandler.inc.php
./templates/frontend/pages/article.tpl
./templates/frontend/components/breadcrumbs_article.tpl

Once again the article.tpl template refers to itself (and we have a false hit on breadcrumbs_article.tpl, since it ends with article.tpl as well).

The relevant code is pages/article/ArticleHandler.inc.php. This is what sets up all the variables your Smarty templates will require. Looking for article.tpl in that code, you’ll see the following in the view function:

return $templateMgr->display('frontend/pages/article.tpl');

OK, good, so you could go ahead and simply modify that code to add more variables to one of the $templateMgr->assign calls to send more data to the template file.

However, you mention that you’re working with a child theme. Is that where you’re modifying article_details.tpl? If so, it’s better to keep your changes in that theme as well. This will be a little more complicated, but will build on what I’ve already described.

For an example of this, look at the “Critical Times” plugin. It registers a hook to watch out for template loads:

HookRegistry::register ('TemplateManager::display', array($this, 'loadTemplateData'));

…and then in the loadTemplateData function, it watches for article.tpl to be referred to

if ($template === 'frontend/pages/article.tpl') {
    $this->loadArticleTemplateData($hookName, $args);
}

…and that function in turn is free to add whatever data it likes to the template before it’s displayed.

To solve your specific problem, because I think you’re probably looping through all the authors, it would be best to load the full set of countries and assign that to the template manager:

$countryDao = DAORegistry::getDAO('CountryDAO');
$templateMgr->assign('countries', $countryDao->getAllCountries());

…then in the template, look it up from the array:

{assign var=authorCountryCode value=$author->getCountry()}
Country: {$countries.$authorCountryCode|escape}

Consider this all pseudocode – I haven’t tested it.

Regards,
Alec Smecher
Public Knowledge Project Team

3 Likes

@asmecher This is marvellous, Alec! Thank you very much for your patience and your time!!

Is there a »technical« documentation, which leads deeper into this kind of development online?

Thanks,
Tobias

1 Like

Hi @twa,

Check out the docs hub – https://docs.pkp.sfu.ca. There’s a section there on developer documentation. You might also check out the Smarty template engine’s documentation.

Regards,
Alec Smecher
Public Knowledge Project Team

2 Likes