0

At the moment I am trying to call a function that is in another block that I am writing from. I have tried through the __constructor() to pass the other block but this has not worked and using the function $block->getLayout()->createBlock to call it from the template(.phtml), but it has not worked either because it needs the $data argument.

class Template extends \Magento\Catalog\Block\Product\View\AbstractView
{
    protected $_scopeConfig;
    protected $_filterProvider;

    public function __construct(
        \Magento\Cms\Model\Template\FilterProvider $filterProvider,
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
        array $data
    ) {
        $this->_scopeConfig = $context->getScopeConfig();
        $this->_filterProvider = $filterProvider;
        parent::__construct($context, $arrayUtils, $data);
    }

    public function getProductLanguage()
    {
        return $this->getLanguageTypes($this->getProduct()->getPepitoLanguage());
    }

    public function getLanguageTypes($option_id)
    {
        $languages = [
            '7' => 'es',
            '6' => 'fr',
            '8' => 'de',
            '5' => 'it',
            '4' => 'en',
            '1' => 'ru',
            '2' => 'zh-CN'];

        return $languages[$option_id];
    }
}

This is the block i want to get the getProductLanguage() method from.

1 Answer 1

0

Yes you can call other blocks methods into another: mageno2 has introduced vew model, you can use view model to call block function in other block's template ( phtml)

Please visit this link to how:

When to use view models

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.