I need to generate an .xml file from my extension. Inside Namespace/Module/view/adminhtml/ui_component/ folder,
I need to do this pro-grammatically because the .xml file will be generated based on data collection, is there a way to do this?
-
1it's never a good idea to generate the layout files on the fly. It screws up your versioning. Explain the problem you are trying to solve instead of the approach you've taken. Maybe there is a different way of solving it without generating ui-components file.Marius– Marius2017年07月10日 09:18:21 +00:00Commented Jul 10, 2017 at 9:18
-
@Marius it related with my other question basically, i need to add custom column in sales order grid dynamicallyIdham Choudry– Idham Choudry2017年07月10日 09:21:07 +00:00Commented Jul 10, 2017 at 9:21
2 Answers 2
For now i'm using php original function to write/create file inside my extension directory like this:
public function __construct(
\Magento\Framework\Module\Dir\Reader $moduleReader
)
{
$this->customAttribute = $customAttribute;
$baseDir = $moduleReader->getModuleDir('', 'Namespace_Module');
$this->dir = $baseDir . '/view/adminhtml/ui_component';
}
public function writeFile() {
$dir = $this->dir;
$fileName = 'test.xml';
$content = '<test>Test</test>';
$myfile = fopen($dir . '/' . $fileName, "w") or die("Unable to open file!");
try {
fwrite($myfile, $content);
fclose($myfile);
} catch (Exception $e) {
$this->_logger($e->getMessage());
}
return;
}
if there's more proper way to do it in Magento 2 please let me know, and i will accept the answer for this question, but for now if anyone want to use this as a solution it's working properly for me but i do not recommend it
-
That is a really bad practice - using native PHP functions eliminates the advantage of using Magento's built-in framework. I would suggest taking a look at core modules, especially import/export ones.Bery– Bery2018年03月21日 13:07:24 +00:00Commented Mar 21, 2018 at 13:07
If you would like to try another way, maybe use Magento\Framework\Filesystem\Io\File and Magento\Framework\Convert\ConvertArray. ConvertArray is useful to make an xml file from a multidimensional array and File can write it for you (and check permissions, create directories and more). Here is a basic example:
public function __construct(
\Magento\Framework\Filesystem\Io\File $file,
\Magento\Framework\Convert\ConvertArray $convertArray
)
{
$this->file = $file;
$this->convertArray = $convertArray;
}
public function createMyXmlFile($assocArray, $rootNodeName, $filename = 'file.xml')
{
// ConvertArray function assocToXml to create SimpleXMLElement
$simpleXmlContents = $this->convertArray->assocToXml($assocArray,rootNodeName);
// convert it to xml using asXML() function
$content = $simpleXmlContents->asXML();
$this->file->write($filename, $contents);
}
if my array is :
$myArray = array(
'fruit' => 'apple',
'vegetables' => array('vegetable_1' => 'carrot', 'vegetable_2' => 'tomato'),
'meat' => 'none',
'sweets' => 'chocolate');
and I call my function:
$simpleXmlContents = $this->convertArray->assocToXml($myArray, 'diner');
$this->file->write($myXmlFile,$simpleXmlContents->asXML());
I would get the following in myfile.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<diner><fruit>apple</fruit><vegetables><vegetable_1>carrot</vegetable_1>
<vegetable_2>tomato</vegetable_2></vegetables><meat>none</meat>
<sweets>chocolate</sweets></diner>
-
how to add xml tag attribute in array. example:- <Events count="2">er</Events> how to add count="2" .Jigar Patel– Jigar Patel2023年02月21日 06:51:15 +00:00Commented Feb 21, 2023 at 6:51
-
that is a good question @jigarPatel I have no answer to that yet and I think you may need to do a plugin solution for that because Magento does not seem to provide an out-of-the-box solution.Isolde– Isolde2023年03月10日 13:55:11 +00:00Commented Mar 10, 2023 at 13:55