How to Magento-2 get a template file path

This way is how to Magento 2 find a template file path for a block.

Example, a template defined in a layout file is FixBugs_Debug::panel/dashboard.pthml

=> Then a template file will be renderred is [Magento Root]/app/code/FixBugs/Debug/view/frontend/templates/panel/dashboard.phtml

Magento\Framework\View\Element\Template
public function getTemplateFile($template = null)
     {
         $params = ['module' => $this->getModuleName()];
         $area = $this->getArea();
         if ($area) {
             $params['area'] = $area;
         }
         return $this->resolver->getTemplateFileName($template ?: $this->getTemplate(), $params);
     }
Magento\Framework\View\FileSystem
public function getTemplateFileName($fileId, array $params = [])
    {
        list($module, $filePath) = \Magento\Framework\View\Asset\Repository::extractModule(
            $this->normalizePath($fileId)
        );
        if ($module) {
            $params['module'] = $module;
        }
        $this->_assetRepo->updateDesignParams($params);
        return $this->_templateFileResolution
            ->getFile($params['area'], $params['themeModel'], $filePath, $params['module']);
    }
Magento\Framework\View\Design\FileResolution\Fallback\TemplateFile
public function getFile($area, ThemeInterface $themeModel, $file, $module = null)
    {
        $template = parent::getFile($area, $themeModel, $file, $module);

        if ($template && $this->assetConfig->isMinifyHtml()) {
            switch ($this->appState->getMode()) {
                case State::MODE_PRODUCTION:
                    return $this->getMinifiedTemplateInProduction($template);
                case State::MODE_DEFAULT:
                    return $this->templateMinifier->getMinified($template);
                case State::MODE_DEVELOPER:
                default:
                    return $template;
            }
        }
        return $template;
    }

Magento\Framework\View\Design\FileResolution\Fallback\File
public function getFile($area, ThemeInterface $themeModel, $file, $module = null)
    {
        return $this->resolver->resolve($this->getFallbackType(), $file, $area, $themeModel, null, $module);
    }
Magento\Framework\View\Design\FileResolution\Fallback\Resolver\Simple
public function resolve($type, $file, $area = null, ThemeInterface $theme = null, $locale = null, $module = null)
    {
        $params = ['area' => $area, 'theme' => $theme, 'locale' => $locale];
        foreach ($params as $key => $param) {
            if ($param === null) {
                unset($params[$key]);
            }
        }
        if (!empty($module)) {
            $params['module_name'] = $module;
        }
        $path = $this->resolveFile($this->rulePool->getRule($type), $file, $params);

        return $path;
    }

protected function resolveFile(RuleInterface $fallbackRule, $file, array $params = [])
    {
        $params['file'] = $file;
        foreach ($fallbackRule->getPatternDirs($params) as $dir) {
            $path = "{$dir}/{$file}";
            $dirRead = $this->readFactory->create($dir);
            if ($dirRead->isExist($file) && $this->checkFilePathAccess($file, $path)) {
                return $path;
            }
        }
        return false;
    }
Magento\Framework\View\Design\Fallback\Rule\Module
public function getPatternDirs(array $params)
    {
        if (!array_key_exists('module_name', $params)) {
            throw new \InvalidArgumentException(
                'Required parameter "module_name" is not specified.'
            );
        }
        $params['module_dir'] = $this->componentRegistrar->getPath(
            ComponentRegistrar::MODULE,
            $params['module_name']
        );
        if (empty($params['module_dir'])) {
            return [];
        }
        return $this->rule->getPatternDirs($params);
    }
Magento\Framework\View\Design\Fallback\Rule\Theme
public function getPatternDirs(array $params)
    {
        if (!array_key_exists('theme', $params) || !$params['theme'] instanceof ThemeInterface) {
            throw new \InvalidArgumentException(
                'Parameter "theme" should be specified and should implement the theme interface.'
            );
        }
        $result = [];
        /** @var $theme ThemeInterface */
        $theme = $params['theme'];
        unset($params['theme']);
        while ($theme) {
            if ($theme->getFullPath()) {
                $params['theme_dir'] = $this->componentRegistrar->getPath(
                    ComponentRegistrar::THEME,
                    $theme->getFullPath()
                );

                $params = $this->getThemePubStaticDir($theme, $params);
                $result = array_merge($result, $this->rule->getPatternDirs($params));
            }
            $theme = $theme->getParentTheme();
        }
        return $result;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *