4

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="Company\PdfDesign\Model\CompanyDesign">
        <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="Company\PdfDesign\Model\CompanyDesign">
        <arguments>
            <argument name="templateFiles" xsi:type="array">
                <item name="extras" xsi:type="helper" helper="Company\PdfDesign\Helper\Table\Extras::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?


Company\PdfDesign\Model\CompanyDesign

<?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 Company\PdfDesign\Model;

use \Fooman\PdfCore\Helper\Logo;
use \Magento\Framework\App\Config\ScopeConfigInterface;

/**
 * Design source for Default Design
 */
class CompanyDesign implements \Fooman\PdfDesign\Model\Api\DesignInterface
{
    ...
    
    public function __construct(
        array $templateFiles = []
    ) {
        $this->templateFiles = $templateFiles;
    }
    
    ...

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

    ...

}

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

<?php
namespace Fooman\PdfDesign\Model;

use Magento\Framework\Exception\NotFoundException;

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

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

    public function pick(Api\DesignInterface $design, $templateFor)
    {
        $designTemplates = $design->getTemplateFiles() + $this->defaultDesign->getTemplateFiles();
        if (!isset($designTemplates[$templateFor])) {
            throw new NotFoundException(__('No template set for %1', $templateFor));
        }
        return $designTemplates[$templateFor];
    }
}
9
  • Can you explain in details what you are try to achive? Nov 17, 2021 at 10:39
  • @DhirenVasoya, im not sure what more details you need. Can you explain?
    – Black
    Nov 17, 2021 at 13:47
  • my question is that which template and how you want to change? Nov 17, 2021 at 13:56
  • can you please provide content of the Company\PdfDesign\Model\CompanyDesign class? In order we could try to understand what you want to achieve. Nov 17, 2021 at 14:06
  • 1
    Ok, then why can't you dynamically set the necessary template in Company\PdfDesign\Model\ CompanyDesign:: getTemplateFiles() method? Nov 17, 2021 at 14:58

2 Answers 2

0

Why don't you come to class and change logic from here, if it's your module, and can use another way like plugin, preference for changing template.

Hope it help!

1
  • 2
    Hello, this is not an answer to my question, it is a comment and it is not helpful either.
    – Black
    Nov 17, 2021 at 9:21
0

I think you have misunderstood how the DI works a bit. Based on the method you wanted to call to get the array of templates in your example above, you could update your class to use the helper for getting the array of templates like this:

use Fooman\PdfDesign\Model\Api\DesignInterface;
use Company\PdfDesign\Helper\Table\Extras;

class CompanyDesign implements DesignInterface
{
    ...
    
    public function __construct(
        Extras $helper,
        array $templateFiles = []
    ) {
        $templateFiles['extras'] = $helper->getTemplate();
        $this->templateFiles = $templateFiles;
    }
    
    ...

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

    ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.