3.4 3.3
Jump to table of contents

Get Data from the Template

When you override a template, you may need to get data that has already been assigned to the template. For example, consider the single issue template below.

<div class="issue">
  <p>Published: {$issue->getPublished()}</p>
  ...
</div>

Your plugin overrides this template because it wants to show a unique ID with all issues. Your plugin’s template looks like this.

<div class="issue">
  <p>Published: {$issue->getPublished()}</p>
  <p>Internal ID: {$issueInternalId}</p>
  ...
</div>

However, you first need to retrieve the issueInternalId from the plugin settings to display the ID. In the example below, the plugin does the following.

  1. Hook into the call that loads the pages/issue.tpl template.
  2. Get the Issue object from the template variables.
  3. Use the issue ID to get the correct plugin setting.
  4. Assign the issueInternalId for use in the plugin’s custom template.
namespace APP\plugins\generic\internalIssueId;

use APP\core\Application;
use APP\issue\Issue;
use APP\template\TemplateManager;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;

class InternalIssueIdPlugin extends GenericPlugin
{
    public function register($category, $path, $mainContextId = null) {
        $success = parent::register($category, $path, $mainContextId);
        if ($success && $this->getEnabled()) {

            // 1. Hook in before the template is displayed...
            Hook::add('TemplateManager::display',[$this, 'addIssueInternalId']);
        }
        return $success;
    }

    public function addIssueInternalId($hookName, $args)
    {
        $templateMgr = $args[0]; /** @var TemplateManager */
        $template = $args[1]; /** @var string */
        $contextId = Application::get()->getRequest()->getContext()->getId();

        // 1. ...only when it is the issue template.
        if ($template !== 'frontend/pages/issue.tpl') {
            return false;
        }

        // 2. Get the `issue` variable from the assigned template variables.
        /** @var Issue $issue */
        $issue = $templateMgr->getTemplateVars('issue');

        // 3. Get the matching plugin setting.
        $internalIssueId = $this->getSetting($contextId, 'issueInternalId' . $issue->getId());

        // 4. Assign the internal issue id for use in the template.
        $templateMgr->assign('internalIssueId', $internalIssueId);

        return false;
    }
}

View more examples.