Zend certified PHP/Magento developer

Magento 2 – Load values in di.xml from a helper class

Currently the template is defined like this:

Company/PdfDesign/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="CompanyPdfDesignModelCompanyDesign">
        <arguments>
            <argument name="templateFiles" xsi:type="array">
                <item name="extras" xsi:type="string">Company_PdfDesign::pdf/table/extras.phtml</item>
            </argument>
        </arguments>
    </type>
</config>

However, I want to make this dynamic and load it from the settings, so I tried to use a helper like seen here:

Company/PdfDesign/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="CompanyPdfDesignModelCompanyDesign">
        <arguments>
            <argument name="templateFiles" xsi:type="array">
                <item name="extras" xsi:type="helper" helper="CompanyPdfDesignHelperTableExtras::getTemplate"></item>
            </argument>
        </arguments>
    </type>
</config>

But if I flush cache from CLI then I get alot of XML errors.´

Invalid Document 
Element 'item', attribute '{http://www.w3.org/2001/XMLSchema-instance}type': The QName value 'helper' of the xsi:type attribute does not resolve to a type definition.
Line: 11

Element 'item': The type definition is abstract.
Line: 11

Internal error: xmlSchemaXPathProcessHistory, The state object to be removed is not the first in the list.
Line: 10

Internal error: xmlSchemaValidateChildElem, calling xmlRegExecPushString2().
Line: 9

Internal error: xmlSchemaValidateElem, calling xmlSchemaStreamValidateChildElement().
Line: 9

Internal error: xmlSchemaDocWalk, calling xmlSchemaValidateElem().
Line: 9

How can I load the value from a helper instead of hardcoding it?


CompanyPdfDesignModelCompanyDesign

<?php
/**
 * @copyright Copyright (c) 2015 Fooman Limited (http://www.fooman.co.nz)
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CompanyPdfDesignModel;

use FoomanPdfCoreHelperLogo;
use MagentoFrameworkAppConfigScopeConfigInterface;

/**
 * Design source for Default Design
 */
class CompanyDesign implements FoomanPdfDesignModelApiDesignInterface
{
    ...
    
    public function __construct(
        array $templateFiles = []
    ) {
        $this->templateFiles = $templateFiles;
    }
    
    ...

    public function getTemplateFiles()
    {
        return $this->templateFiles;
    }

    ...

}

vendor/fooman/pdfdesign-m2/src/Model/TemplateFileDecider.php

<?php
namespace FoomanPdfDesignModel;

use MagentoFrameworkExceptionNotFoundException;

/**
 * pick template file based on chosen pdf design
 */
class TemplateFileDecider
{
    private $defaultDesign;

    public function __construct(
        DefaultDesign $defaultDesign
    ) {
        $this->defaultDesign = $defaultDesign;
    }

    public function pick(ApiDesignInterface $design, $templateFor)
    {
        $designTemplates = $design->getTemplateFiles() + $this->defaultDesign->getTemplateFiles();
        if (!isset($designTemplates[$templateFor])) {
            throw new NotFoundException(__('No template set for %1', $templateFor));
        }
        return $designTemplates[$templateFor];
    }
}