Hi Ganesh,
Thanks for your reply.
Sorry, I should have said, I’m doing this inside a snippet, not in a php file. Its actually the server side of some Ajax functionality that eventually returns JSON. One of the things that it does is return a link to a just created document (like in my example).
I’m actually building a ’scrapbook system’. There are loads of images on a site and users can add them to a scrapbook. It all works with AJAX. A scrapbook is a document in the modx tree. The items in the scrapbook are stored as a comma separated list of doc ids in a tv.
The first time a user clicks ’Add to scrapbook’ the id is sent to the server, the server checks if a scrapbook is already live (as in a doc has been created). If there is a live one it just modifies the list stored in the tv. If there isn’t a live one a doc is created and some info about whats just happened (including the link) is returned. I then use JS to update the UI including a ’view my scrapbook’ link, thats the bit that I need the link for. There’s loads of other functionality in this system and it’s all working sweetly, am loving ’ajaz’ coding and the JSON format with jQuery. This is the ONLY issue I’m having.
So another strange thing about this is (I’m finding it hard to make this seem understandable)....
I get the link when I create the doc... and it doesn’t work as per my first post. When a page loads it checks, through the same ajax system, to see if there is a scrapbook already created (I store the doc id in a session variable/cookie). If there is it gets the link, executing the same code in the same way. Sometimes this works, and sometimes it doesn’t, in the same way I describe in my first post but without the doc creation bit. So here’s an example of what I mean:
1. Click add to scrapbook.
-Ajax passes the id up to the server, server finds that there IS NO scrapbook, creates one and returns (including link in error)
2. Refresh page
-Ajax passes the id upto the server, server finds that there IS a scrapbook, gathers some info returns (including link in error)
3. Go into Manager, find correct doc by link, open it and save it
4. Refresh page again
-Same as step 2 but this time when it returns the link is correct
So what this seems to say is that if I create a doc in code, something goes with the modx object where despite being able to find a doc by ID with $modx->getDocument it cant make a url with $modx->makeUrl..... unless I save the doc in the manager. I’ve been looking at the source files for the manager and I can see that is missing is event invocation.
So, looking at the source for makeUrl:
$alias= $id;
if ($this->config['friendly_alias_urls'] == 1) {
$al= $this->aliasListing[$id];
$alPath= !empty ($al['path']) ? $al['path'] . '/' : '';
if ($al && $al['alias'])
$alias= $al['alias'];
}
Line 1549 is this:
$alias= $id;
So it looks to me that $alias never gets modified. I think this test: !empty ($al[’path’]) ... is failing, but I have no idea why.
Sorry about the long post, wanted to try to explain everything.