I need to loop through contexts because each context has information that I need in it’s settings. Each context represents a chapter in our organization.
On the home page, for example, the script needs to accomplish the following:
1. Get resources only from contexts whose key "site_type" == "chapter_subdomain’
2. Each domain has a resource container that contains events. The id of this events container is specified in the context settings - so I need to get the id of the events resource container from each context.
3. [And this is where I’m having the problems] check the "ShowOnHomePage" TV of each returned resource to determine whether to put in the the upcoming events section of the home page. I have to manually clear the cache to do this.
#1 could be accomplished via your script by specifying a list of contexts in the snippet call, but I kind of like idea of not having to worry about that. The way it’s currently setup, when I add new chapter-contexts I don’t need to touch any other code. Now, I suppose I could run a query that gets the appropriate contexts (key:’site_type’ == value:’chapter_subdomain’) and then write the context-keys to an array and then pass that array as a variable to the snippet you provided. That would solve the problem of not having to update the snippet when I add a new chapter/context.
#2 could be handled by manually listing event resource containers in the snippet call, but then I run into the same issue as #1 where I have to update multiple snippets (or at the very least a property set) every time I add a new chapter-context.
#3 obviously wouldn’t be an issue.
I guess my question is why
not loop through contexts? My script is comparable in size (lines of code) and the logic is pretty straight-forward so its easy to maintain. However, I don’t know the inner workings of ModX like you do. Perhaps looping through contexts is much more expensive as far as processing time? If this is the case, perhaps I will look into using property sets with my snippets to provide the needed information and use something similar to the code you provided. I just really liked the idea of being able to put a bunch of settings in one place (context key-value settings) and then utilizing those values in my scripts.
Actually, now that I think about it I can definitely see where it would be much more time consuming using my approach, especially if our organization grows to 20,30, or more chapters. That’s 30+ queries as opposed to one. Perhaps the best approach then would be to transfer the information that I’m currently storing in context settings to a property set instead and then use some version of the script you provided. Does that sound like a good approach? The only potential downside here is in that in some cases a key-value pair may needed to be associated with a specific context. I haven’t experimented much with property sets (actually, I’ve never used them), so I’m not sure if I can do this or not. I’ll have to experiment and find out.
As far as the cache-clearing question. The non-web-context resource variables may be clearing some cache files on-TV-save, but I know for certain that when looping through contexts it does not recognize a TV Value change unless I manually clear the cache.
UPDATE:
After giving it some more thought, I think I’m simply going to create a TV called "ResourceType" with a dropdown list of values that will initially include "event_resource" and "event_resource_container". The events and their corresponding TV’s are updated and created via a script that pulls from Google Calendar, so the user won’t ever have to worry about adjusting this field. This approach allows for only one query and keeps things pretty simple.
But then again, this wouldn’t work for situations where I need to list all chapters, since there isn’t an actual "chapter" resource. Each context is a chapter. So, I guess I will still need to implement something along the lines of what was mentioned above.
-matt