In order to get onto the issue, let’s have a look at the method loadFileLayoutUpdatesXml() of the class Magento\Framework\View\Model\Layout\Merge.
1 2 3 4 5 6 7 8 9 10 11 |
/** * Collect and merge layout updates from files * * @return \Magento\Framework\View\Layout\Element * @throws \Magento\Framework\Exception\LocalizedException */ protected function _loadFileLayoutUpdatesXml() { $layoutStr = ''; $theme = $this->_getPhysicalTheme($this->theme); ... |
As for me, I use Magento 2.1.7, so in the picture, we can see the variable $theme that contains theme data. In my case, there are data concerning default theme “Magento Luma”.
1 |
$updateFiles = $this->fileSource->getFiles($theme, '*.xml'); |
Partner With Us
Let's discuss how to grow your business. Get a Free Quote.On the stage, we get all XML files with the help of the method getFiles placed in the class \lib\internal\Magento\Framework\View\Layout\File\Collector\Aggregated. Let’s have a closer look at the method to comprehend how it works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/** * Retrieve files * * Aggregate layout files from modules and a theme and its ancestors * * @param ThemeInterface $theme * @param string $filePath * @return \Magento\Framework\View\File[] */ public function getFiles(ThemeInterface $theme, $filePath) { $list = $this->fileListFactory->create(); $list->add($this->baseFiles->getFiles($theme, $filePath)); foreach ($theme->getInheritedThemes() as $currentTheme) { $list->add($this->themeFiles->getFiles($currentTheme, $filePath)); $list->replace($this->overrideBaseFiles->getFiles($currentTheme, $filePath)); $list->replace($this->overrideThemeFiles->getFiles($currentTheme, $filePath)); } return $list->getAll(); } |
Here we create a list where pathways should be added to XML files. As before, it’s not clear where Magento finds XML files. Let’s define what is the method getFiles of the class /lib/internal/Magento/Framework/View/File/Collector/Decorator/ModuleDependency makes:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * Retrieve view files, sorted by the priority of modules they belong to * * @param ThemeInterface $theme * @param string $filePath * @return \Magento\Framework\View\File[] */ public function getFiles(ThemeInterface $theme, $filePath) { $result = $this->subject->getFiles($theme, $filePath); usort($result, [$this, 'compareFiles']); return $result; } |
As you can see, the method is considered as significant and simple. So we should learn one more method getFiles of the class /lib/internal/Magento/Framework/View/File/Collector/Decorator/ModuleOutput:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/** * Retrieve files * * Filter out theme files that belong to inactive modules or ones explicitly configured to not produce any output * * @param ThemeInterface $theme * @param string $filePath * @return \Magento\Framework\View\File[] */ public function getFiles(ThemeInterface $theme, $filePath) { $result = []; foreach ($this->subject->getFiles($theme, $filePath) as $file) { if ($this->moduleManager->isOutputEnabled($file->getModule())) { $result[] = $file; } } return $result; } |
Magento 2 Development
Take your online store to the next level with BelVG Magento 2 Development
Visit the pageAs it is mentioned in the code, from the list of all modules we leave only active. Let’s continue reviewing the method getFiles of the class Magento/Framework/View/File/Collector/Base:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/** * Retrieve files * * @param \Magento\Framework\View\Design\ThemeInterface $theme * @param string $filePath * @return \Magento\Framework\View\File[] */ public function getFiles(ThemeInterface $theme, $filePath) { $result = []; $sharedFiles = $this->componentDirSearch->collectFilesWithContext( ComponentRegistrar::MODULE, "view/base/{$this->subDir}{$filePath}" ); foreach ($sharedFiles as $file) { $result[] = $this->fileFactory->create($file->getFullPath(), $file->getComponentName(), null, true); } $area = $theme->getData('area'); $themeFiles = $this->componentDirSearch->collectFilesWithContext( ComponentRegistrar::MODULE, "view/{$area}/{$this->subDir}{$filePath}" ); foreach ($themeFiles as $file) { $result[] = $this->fileFactory->create($file->getFullPath(), $file->getComponentName()); } return $result; } |
Here we can find something interesting. Let’s learn how XML files are collected:
1 2 3 4 5 6 7 8 9 10 11 12 |
... $sharedFiles = $this->componentDirSearch->collectFilesWithContext( ComponentRegistrar::MODULE, "view/base/{$this->subDir}{$filePath}" ); … $area = $theme->getData('area'); $themeFiles = $this->componentDirSearch->collectFilesWithContext( ComponentRegistrar::MODULE, "view/{$area}/{$this->subDir}{$filePath}" ); ... |
Where:
$area – frontend
$this->subDir – ui_componen/
$filePath – etc/definition.xml
First of all, we get the XML file of the pathway “view/base/ui_componen/etc/definition.xml”. Then it’s necessary to get the XML file of the pathway “view/frontend/ui_componen/etc/definition.xml”.
The method collectFilesWithContext of the class Magento\Framework\Component\DirSearch gets the list of files in the definite directory back. Even a needed file can be given back, as in our case. As a result, we have “view/base/ui_component/etc/definition.xml” file in $sharedFiles array. While $themeFiles array is empty, as the file has not been created on the way. Therefore, we get a file where all UI components have a base declaration.
Magento Webdesign
Take your online store to the next level with BelVG Magento webdesign
Visit the pageIt’s high time to return to the method _loadFileLayoutUpdatesXml we started with. The following line is in it:
1 2 3 |
... $updateFiles = array_merge($updateFiles, $this->pageLayoutFileSource->getFiles($theme, '*.xml')); ... |
Since we know the whole pathway consisting of some getFiles methods, let’s jump of Magento/Framework/View/File/Collector/Base::getFiles
Now we get XML of not a definite XML file, but all XML files in the directory “view/base/layout/”.
Let’s have a look at the screenshots and define what is contained in $sharedFiles:
As a result, we get XML files of the directory “view/frontend/layout/”. Let’s look at the second screenshot and define what is contained in $themeFiles:
We have all xml files of the theme. Let’s summarize which files we’ve got for further processing:
- <Magento_Theme_module_dir>/view/base/ui_component/etc/definition.xml
- <Magento_Theme_module_dir>/view/frontend/ui_component/etc/definition.xml
- <Magento_Theme_module_dir>/view/base/layout/*
- <Magento_Theme_module_dir>/view/frontend/layout/*
Magento Custom Development
Take your online store to the next level with BelVG Magento Custom Development
Visit the page
You are welcome, Rafael!
Thanks for sharing Denis!