2

I try to modify the template of a custom option type in backend, sales order view section. In this page, product options are not displayed using a dedicated template, but only by returning Html of option Title and Value.

Based on the ikantam tutorial, I would like to use the GetCustomizedView($optionInfo)function to create a block which will load my .phtml

the main & original .phtml displaying the options is :

/vendor/magento/module-sales/view/adminhtml/templates/items/column/name.phtml


<?php if ($block->getOrderOptions()): ?>
        <dl class="item-options">
            <?php foreach ($block->getOrderOptions() as $_option): ?>
                <dt><?php /* @escapeNotVerified */ echo $_option['label'] ?>:</dt>
                <dd>
                    <?php if (isset($_option['custom_view']) && $_option['custom_view']): ?>
                        <?php /* @escapeNotVerified */ echo $block->getCustomizedOptionValue($_option); ?>
                    <?php else: ?>
                        <?php $_option = $block->getFormattedOption($_option['value']); ?>
                        <?php /* @escapeNotVerified */ echo $_option['value']; ?><?php if (isset($_option['remainder']) && $_option['remainder']): ?><span id="<?php /* @escapeNotVerified */ echo $_dots = 'dots' . uniqid()?>"> ...</span><span id="<?php /* @escapeNotVerified */ echo $_id = 'id' . uniqid()?>"><?php /* @escapeNotVerified */ echo $_option['remainder'] ?></span>
                            <script>
                                require(['prototype'], function() {
                                    $('<?php /* @escapeNotVerified */ echo $_id ?>').hide();
                                    $('<?php /* @escapeNotVerified */ echo $_id ?>').up().observe('mouseover', function(){$('<?php /* @escapeNotVerified */ echo $_id ?>').show();});
                                    $('<?php /* @escapeNotVerified */ echo $_id ?>').up().observe('mouseover', function(){$('<?php /* @escapeNotVerified */ echo $_dots?>').hide();});
                                    $('<?php /* @escapeNotVerified */ echo $_id ?>').up().observe('mouseout',  function(){$('<?php /* @escapeNotVerified */ echo $_id ?>').hide();});
                                    $('<?php /* @escapeNotVerified */ echo $_id ?>').up().observe('mouseout',  function(){$('<?php /* @escapeNotVerified */ echo $_dots ?>').show();});
                                });
                            </script>
                        <?php endif; ?>
                    <?php endif; ?>
                </dd>
            <?php endforeach; ?>
        </dl>
    <?php endif; ?>
    <?php echo $block->escapeHtml($_item->getDescription()) ?>
<?php endif; ?> 

with my modified GetCustomizedView() function :

public function getCustomizedView($optionInfo)
{
$customizedBlock = new \Company\Module\Block\Options\Type\Customview\File();
$customizedBlock->setInfo($optionsInfo);
return $customizedBlock->toHtml();
}

With the new File Class : Company\Module\Block\Options\Type\Customview\File.php

<?php
namespace Company\Module\Block\Options\Type\Customview;

class File extends \Magento\Framework\View\Element\Template
{

    protected $_template = 'Company_Module::options/type/customview/xfile.phtml';

    public function __construct(\Magento\Framework\View\Element\Template\Context $context,
        array $data = []) {
        parent::__construct($context, $data);
    }

    public function getFileInfo()
    {
        $info = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $this->getOption()->getId());
        if (empty($info)) {
            $info = new \Magento\Framework\DataObject();
        } elseif (is_array($info)) {
            $info = new \Magento\Framework\DataObject($info);
        }
        return $info;
    }

}

BUT, when the constructor is called, which, in this case only calls the \Magento\Framework\View\Element\Template constructor, it returns an error as the $context is not defined / passed in parameters..

Am I doing it the right way? How to reach the $context var?

Thank you for your help,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.