8

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?

  • it'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 Jul 10 '17 at 9:18
  • @Marius it related with my other question basically, i need to add custom column in sales order grid dynamically – Idham Choudry Jul 10 '17 at 9:21
1

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 Mar 21 '18 at 13:07
0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.