On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • 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
      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
      • Thanks a lot !
        I based my work on yours, and now it works smiley
        • 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