We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51450
    • 6 Posts
    Hello everybody !

    I had exported a custom XML file Policy from the manager of a specific project.
    I want to import this file dynamicly from a snippet in a new project. To do that, I used modAccessPolicy method.

    here's my code :

    $data = file_get_contents('custom.policy.xml');
    $xml = simplexml_load_string($data, "SimpleXMLElement", LIBXML_NOCDATA);
    $json = json_encode($xml);
    
    $policy = $modx->newObject('modAccessPolicy');
    $policy->set('data', $json);
    $policy->save();
    


    If any one could tell me the right way,
    Thanks for your help laugh

    This question has been answered by BobRay. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      Your $xml variable is an object (not an array) and I suspect that it may need to be converted to an array, then used to set the fields of the Policy individually. I don't think you'll need json_encode() because the 'data' member is probably already JSON and it's the only JSON value in a Policy. You may also need to create a Policy Template and use its ID for the 'template' field of the Policy.

      Here's what the Policy object looks like: http://bobsguides.com/modx-object-quick-reference.html#modAccessPolicy.

      You might also look at the code of the NewsPublisher snippet. The third and fourth files here have code to create a Policy and Policy Template.

      If you have a code editor that will let you run and debug php scripts (e.g., PhpStorm), you'll save a lot of time if you write the code in it and step through it to see what you're getting until you can get things in the correct form.

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 51450
        • 6 Posts
        Thanks a lot !
        I based my work on yours, and now it works smiley
          • 3749
          • 24,544 Posts
          I'm glad it worked for you.

          Actually, I think Markus Schlegel is the author of that bit of code, so he deserves most of the credit.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting