Skip to content

How to call a block function from another block or template

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 MagentoCatalogBlockProductViewAbstractView
{
    protected $_scopeConfig;
    protected $_filterProvider;

    public function __construct(
        MagentoCmsModelTemplateFilterProvider $filterProvider,
        MagentoCatalogBlockProductContext $context,
        MagentoFrameworkStdlibArrayUtils $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.