opengeek / getresources Goto Github PK
View Code? Open in Web Editor NEWA MODx Snippet for iterating a collection of MODx Resources
Home Page: http://rtfm.modx.com/display/ADDON/getResources
A MODx Snippet for iterating a collection of MODx Resources
Home Page: http://rtfm.modx.com/display/ADDON/getResources
Enable sorting of getResources results by TV value
e.g. &sortby=tv.value
This is my snippet call
[[getResources? &parents=0
&where={"template:=":3}
&tvFilters=productInSlider==true
&includeTVs=1
&tpl=productHomeSlider
&limit=&sortbyTV=
productSliderPos &sortby=
]]
Where my 'productInSlider' tv is a checkbox to determine if a resource is need to be shown here (i'm planning to make a content-slider) and the 'productSliderPos' is a number tv for the sort-direction.
I'm getting 5 times wat I want a single time and getting once the same wat I want but onundered.
Regards, Bert
Add a parameter to show content if getResources results in zero resources.
I have noticed that using getResources to aggregate documents based on TVs, in particular, can be very slow.
It looks like this is because every resource field and every TV on each of the specified resources is being returned to the array.
A useful feature might be the ability to specify, or limit, which TVs are to be queried, in order to speed things up a bit.
Allow to select resources directly (not by their parents) and add them to the collection by, say, &resources=
id[, id]``? It should be possible not to select any parents, and collect only resources of choice. Of course, the &depth
parameter should be ignored in this case.
That will allow to collect resources into a hand made list and, in special case, return value for a single resource similarly to getField snippet for Evo.
change &context to support multiple ones by making it an IN clause
resources are returned multiple times
see http://modxcms.com/forums/index.php/topic,50644.msg321148.html#msg321148
and http://modxcms.com/forums/index.php/topic,52736.msg321214.html#msg321214
I've noticed, if I use sortBy to give getResources a list of resource id's, when it comes to output them, it does so in the reverse order to that which is stated in the list. For example, say we have
[[!getResources? &resources=[6,1,55,7,25
&limit=``&tpl=my_tpl
&includeTVs=`1`&processTVs=`1`&sortby=`FIELD(modResource.id, 6,1,55,7,25 )`]]
..what we get back are resources ordered as 25,7,55,1,6
This is with getResources 1.3.1
I have a website with pages which contain 3 getResources calls. The site has become really slow now. I've turned the getResources calls off and on and they're definitely the source of my problems. Can getResources' caching ability be vastly improved?
Case #1.
I've created a property set myPropertySet
for getResources and added, e.g., tpl_5
parameter. When calling a snippet by [[getResources@myPropertySet]]
everything is OK despite tpl_5 parameter is ignored and every 3d item is considered 'odd' (as by default).
Case #2.
I've created a property set myPropertySet
for getResources and added tpl_5
parameter when calling a snippet: [[getResources@myPropertySet? &tpl_5='myTplOdd']]
everything is OK but tpl_5
parameter is ignored again and every 3d item is considered 'odd' (as by default).
'options' => array(
array('text' => 'ascending','vaue' => 'ASC'),
array('text' => 'descending','value' => 'DESC'),
Causing troubles in propety sets
I've probably found a bug...
When I have a snippet call in context A and my parent folder is in context B, then the children of my parent folder are not displayed (as they would if the snippet call is also in context B).
This behavior was also described by an other user at the forum: http://forums.modx.com/thread/70160/getresources---multiple-contexts-children-wrong#dis-post-392986
My snippet call (in context "api"):
[[!getResources:default=
No results found? &parents=
4 &limit=
0 &sortbyTV=
event-date &sortdirTV=
ASC &where=
{"template:=":3} &hideContainers=
1 &includeContent=
1 &includeTVs=
1 &processTVs=
1 &tvPrefix=`` &tpl=
api-event &context=
data ]]
If I use this in the context "data" it works like expected.
I just tried getResources for the first time. Tried calling it with no arguments and I get an SQL error:
[2010-09-10 12:02:45](ERROR @ /index.php) Error 42000 executing statement:
Array
(
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1,4,12,16,18,19,20,21,22,23,5,6,7,8,9,10,11,13,14,15,17) AND (modResource
.`con' at line 1
)
Side note: this error goes to the error log in 1.1.0ga, but outputs in the calling resource in 1.1.0pl.
The problem seems to be with the 'parents' value. It is defined as a property but with no value. I think because it is defined as a property, it is passing "isset($parents)" in the definition line.
Setting the property value to 0 fixes the problem.
After updating to 1.1.0 pl I get the following PH warning when processing TVs:
[2010-08-02 17:18:14] (ERROR @ /secret/folders/core/model/modx/modtemplatevar.class.php : 422) PHP warning: DirectoryIterator::__construct(/secret/folders/core/model/modx/processors/element/tv/renders/SecretContextName/output/) [directoryiterator.--construct]: failed to open dir: No such file or directory
The TV which is being processed looks like this:
@SELECT pagetitle FROM modx_site_content WHERE parent = 73
I use this as an alternative to the "tags" TV of the blog example (http://svn.modxcms.com/docs/display/revolution20/Creating+a+Blog+in+MODx+Revolution). Basically I want the tags to be documents within a folder (73).
Actually, I'm using 2.2-dev but this one appeared after upgrade thus the title. I fetch a descriptive name for content_type
via Rowbot and it doesn't work now. The Rowbot call sits inside template chunk used by getResources:
[[[email protected]?where=`{"id:=":"[[+content_type]]"}`]]
However, once I place [[+content_type]]
in the chunk directly then both the Rowboat output and content_type
display! I had to cloak Rowboat call like this:
[[+content_type:ifnotempty=`[[[email protected]?where=`{"id:=":"[[+content_type]]"}`]]`]]
That works but is not a solution as content_type
is always present and I guess a similar issue may happen with other nested snippet calls.
I also tried a TV with the first Rowboat call as default value in place of the direct snippet call inside a chunk. This didn't work either in getResources output while worked correctly when called directly from a resource.
I'd like to be able to return a collection of resources that use a given template. Adding a 'templates' property that takes a comma-delimited list of template IDs would be really helpful.
Make &where able to be a JSON array that can be used in filtering
Hi there,
Here is the Document layout i have:
Root
-home
-page1
---factorsandquestionscontainer
------factorandquestion1 (template id = 7)
------factorandquestion2 (template id = 7)
---audiocontainer
------audio1 (template id = 6)
------audio2 (template id = 6)
Here are the calls that are within the same template, one after the other as below:
[[!getResources? &parents=[[*id]]
&tpl=CMM-CC-factorsandquestionstpl
&where={"template:=":7}
&includeTVs=1
&processTVs=1
&sortby={"pagetitle":"ASC"}
&cache=0
]]
[[!getResources? &parents=[[*id]]
&tpl=audiotpl
&where={"template:=":6}
&includeTVs=1
&processTVs=1
&sortby={"pagetitle":"ASC"}
&cache=0
]]
The first call brings back as expected, the second doesn't. Here is the debug mysql generated, it appears that the latter call does not correctly pass the parent ids in as the parent ids of the audio is 36 in the site_content table:
[2011-05-05 19:26:07](ERROR @ /csquared/index.php) SELECT modResource
.id
, modResource
.type
, modResource
.contentType
, modResource
.pagetitle
, modResource
.longtitle
, modResource
.description
, modResource
.alias
, modResource
.link_attributes
, modResource
.published
, modResource
.pub_date
, modResource
.unpub_date
, modResource
.parent
, modResource
.isfolder
, modResource
.introtext
, modResource
.richtext
, modResource
.template
, modResource
.menuindex
, modResource
.searchable
, modResource
.cacheable
, modResource
.createdby
, modResource
.createdon
, modResource
.editedby
, modResource
.editedon
, modResource
.deleted
, modResource
.deletedon
, modResource
.deletedby
, modResource
.publishedon
, modResource
.publishedby
, modResource
.menutitle
, modResource
.donthit
, modResource
.haskeywords
, modResource
.hasmetatags
, modResource
.privateweb
, modResource
.privatemgr
, modResource
.content_dispo
, modResource
.hidemenu
, modResource
.class_key
, modResource
.context_key
, modResource
.content_type
FROM site_content
AS modResource
WHERE ( ( modResource.parent IN (14,16,24,26) AND (modResource.context_key IN ('web') OR EXISTS(SELECT 1 FROM context_resource
ctx WHERE ctx.resource = modResource.id AND ctx.context_key IN ('web'))) ) AND modResource
.deleted
= 0 AND modResource
.published
= 1 AND modResource
.hidemenu
= 0 AND modResource
.template
= 6 ) ORDER BY pagetitle ASC LIMIT 5
if you need anymore info to help, just let me know,
thanks,
Lee
Chunk parameters ie. equivalent to [[$chunk?param=
value]]
can't be used in &tpl
s.
That would particularly useful for altering template chunks for use with &tplFirst
, &tplLast
etc., where it would allow to use a single chunk with a variable.
[[!getResources? &parents=0,-25
&limit=0
&tpl=sitemap
&sortby={"pagetitle":"ASC"}
]]
-25
but to no availNo idea why this is happening, it should hide documents under that parent :)
I just did a update of my site (from 2.1.1-pl to 2.1.2-pl) and now all elements generated by getResources are not visible...
Tried a reinstall of getResources but it does not work. There are no errors in the log.
My call:
[[!getResources? &resources=[[$settings_page_id]]
&tpl=chunk_footer_contact
&includeTVs=1
&processTVs=1
&tvPrefix=``]]
It worked before the update....
I also tried:
[[getResources? &resources=[[$settings_page_id]]
&tpl=chunk_footer_contact
&includeTVs=1
&processTVs=1
&tvPrefix=``]]
It worked before the update...
Anybody else got this?
I'm having some problems selecting a single TV to be included and processed. When I run the below, the TV called articleThumbnail is not returned (although it does return resources sans that). If I don't include the TVLists, the TV comes back fine. Why is this?
Below I specify the TV name in double-quotes as it's a string but no joy with it coming back
[[!getResources? &resources=`[[*hpHighlightsIDlist:default=`3`]]` &limit=`` &tpl=`tpl_SubPageDirList` &includeTVs=`1` &processTVs=`1` &includeTVList=`"articleThumbnail"` &processTVList=`"articleThumbnail"` &sortby=`{"FIELD(modResource.id,[[*hpHighlightsIDlist:default=`3`]])","ASC"}` &parents=`-1`]]
With debug mode on, the SQL is
[2011-10-29 06:12:12] (ERROR @ /index.php) context for -1 is [2011-10-29 06:12:12] (ERROR @ /index.php) SELECT `modResource`.`id`, `modResource`.`type`, `modResource`.`contentType`, `modResource`.`pagetitle`, `modResource`.`longtitle`, `modResource`.`description`, `modResource`.`alias`, `modResource`.`link_attributes`, `modResource`.`published`, `modResource`.`pub_date`, `modResource`.`unpub_date`, `modResource`.`parent`, `modResource`.`isfolder`, `modResource`.`introtext`, `modResource`.`richtext`, `modResource`.`template`, `modResource`.`menuindex`, `modResource`.`searchable`, `modResource`.`cacheable`, `modResource`.`createdby`, `modResource`.`createdon`, `modResource`.`editedby`, `modResource`.`editedon`, `modResource`.`deleted`, `modResource`.`deletedon`, `modResource`.`deletedby`, `modResource`.`publishedon`, `modResource`.`publishedby`, `modResource`.`menutitle`, `modResource`.`donthit`, `modResource`.`privateweb`, `modResource`.`privatemgr`, `modResource`.`content_dispo`, `modResource`.`hidemenu`, `modResource`.`class_key`, `modResource`.`context_key`, `modResource`.`content_type`, `modResource`.`uri`, `modResource`.`uri_override` FROM `modx_site_content` AS `modResource` WHERE ( ( modResource.parent IN (-1) AND `modResource`.`deleted` = 0 AND `modResource`.`published` = 1 AND `modResource`.`hidemenu` = 0 ) OR `modResource`.`id` IN (13,14,25,4) )
Specifying it without quotes yields the following error:
[2011-10-29 06:09:52] (ERROR @ /path_to_site/core/cache/includes/elements/modsnippet/10.include.cache.php : 384) PHP warning: in_array(): Wrong datatype for second argument
If I specify just the id numbers of the TV, it returns nothing.
When I'm rolling out a new set of pages, it would be very useful to be able to
test my getResources calls against unpublished sources. Ideally, getResources
would figure out the published status of the calling page.
For a project we have made a list with resources inside a category page. In an other category page we have made a symlink to a resource somewhere else in the website. This works great but only the the overview of symlinks doesn't have all information from the symlink page.
What I would like to have is all information (like fields and tvs) from the resource where the symlink was made to. I hope you will understand and maybe this is a good fix for working with symlinks
For now I have manually fixed this in the getresources snippet
I just found that the [[+id]] placeholder doesn't work when it's placed inside a link tag if I used a @inline template code.
It does work on @file or chunk.
[[!getResources? &tpl=
@INLINE <a href="[[~[[+id]]]]" title="[[+pagetitle]]">[[+pagetitle]] ([[+id]]) </a>
]]
It is only the [[+id]] inside the link that is missing.
I have a getResources tag like
[[!getResources? &resources=1,12,13,14,6,7,20
&tpl=myChunk
&limit=``&depth=0
]]
I would like select the specified resources but somehow the current page his childs will be added and the order of the items is on menuindex or publishedon field.
It should be great if the order is the same as the resources I entered in the parameter and to not loading the subpages of the current page. Currently I have added the parents parameter with the ID of the homepage (1) wich has no subpages to load no other pages.
I hope you understand
Add missing &where property in default propertyset.
Hi there,
With this getResources call
[[getResources? &depth=0
&includeTVs=1
&includeTVList=general-page-img
&processTVList=general-page-img
&sortby=menuindex
&sortdir=DESC
&parents=[[*id]]
&limit=100
&tpl=getResources_subseclist_menuindex
]]
The general-page-img TV is being returned but not processed. From the documentation it appears that processTVs=1
is not required (although to be honest it seems strange that you need both includeTVs and includeTVList together. Surely if you are being specific the includeTVList alone would be enough).
In the getResources snippet (1.4.0) on line 378 it has
if ($processTVs && (empty($processTVList) || in_array($templateVar->get('name'), $processTVs)))
I changed mine to
if (!empty($processTVList) && in_array($templateVar->get('name'), $processTVList))
and my TV began processing again. Whether or not this is the correct fix I am not 100% sure however it seems that in teh currecnt code it is looking for the name in the processTVs variable (it should be the processTVList variable shouldnt it?
Secondly the first part of the if seems to be requiring processTVs to be set to 1 before it will allow it past that condition.
Thoughts?
Currently I am assuming this area is bugged. Please let me know otherwise.
kind regards
Marcushouse
An incredible addition to MODx Revolution would be a natural way to generate a filter/search form, based on getResources's &tvfilters parameter. Think of the "TV Explorer" addon in Evolution.
For example, look at this MODx site: http://caravanking.nl/caravan-verkoop-actueel-aanbod.html. The search-form is made with TV Explorer and based on different TV's. The same could and should be done with getresources.
I'm using first and last tpl. But they differs from original template that they have a class.
I prefer 2 more params than 2 more chunks.
It's not possible to keep explicit order in output. I expect it with &resources=
2, 4, 3 and `&sortby=
`.
Two issues in as many days - apologies however this is a big one. One of my getResources requests takes 2.5 seconds to complete but more worrying is the below which takes nearly 7 seconds and only has to return 5 items. How can this be optimised??
[[!getResources? &resources=`[[*hpHighlightsIDlist:default=`3`]]` &limit=`` &tpl=`tpl_SubPageDirList` &includeTVs=`1` &processTVs=`0` &sortby=`{"FIELD(modResource.id,[[*hpHighlightsIDlist:default=`3`]])","ASC"}` &parents=`-1` &debug=`1`]]
The TV hpHighlightsIDlist contains five resource ID's
From the error log
[2011-10-31 15:43:14] (ERROR @ /index.php) SELECT `modResource`.`id`, `modResource`.`type`, `modResource`.`contentType`, `modResource`.`pagetitle`, `modResource`.`longtitle`, `modResource`.`description`, `modResource`.`alias`, `modResource`.`link_attributes`, `modResource`.`published`, `modResource`.`pub_date`, `modResource`.`unpub_date`, `modResource`.`parent`, `modResource`.`isfolder`, `modResource`.`introtext`, `modResource`.`richtext`, `modResource`.`template`, `modResource`.`menuindex`, `modResource`.`searchable`, `modResource`.`cacheable`, `modResource`.`createdby`, `modResource`.`createdon`, `modResource`.`editedby`, `modResource`.`editedon`, `modResource`.`deleted`, `modResource`.`deletedon`, `modResource`.`deletedby`, `modResource`.`publishedon`, `modResource`.`publishedby`, `modResource`.`menutitle`, `modResource`.`donthit`, `modResource`.`privateweb`, `modResource`.`privatemgr`, `modResource`.`content_dispo`, `modResource`.`hidemenu`, `modResource`.`class_key`, `modResource`.`context_key`, `modResource`.`content_type`, `modResource`.`uri`, `modResource`.`uri_override` FROM `modx_site_content` AS `modResource` WHERE ( ( modResource.parent IN (-1) AND `modResource`.`deleted` = 0 AND `modResource`.`published` = 1 AND `modResource`.`hidemenu` = 0 ) OR `modResource`.`id` IN (13,14,25,4) ) [2011-10-31 15:43:20] (ERROR @ /index.php) modSnippet: getResources executed in 6.3166 s
I've done some modification to getResources to accomodate more TVFilters condition operators such as :
!=
EXISTS
NOTEXISTS
| : for OR operator
And changed that the "OR" "|" condition will only effect after the main condition is meet, etc. published='1' and deleted='0'
Examples:
tvFilters= mytv!=%xxx%|mytv==NOTEXISTS
Setting condition to TV mytv does not contain tag "xxx" or no mytv variable exists
getResources2.php
http://www.mercstudio.net/assets/media/getResources2.php.zip
Would be nice if you can sort the resources by a TV
Even on an uncached page and within an uncached snippet call the sortorder
parameter set to RAND()
doesn’t produce a new sort order on every page reload (as one would expect it) as long as the system setting cache_db
is enebled.
Disabling the database cache is a possible fix. But that means that database caching is not available for the whole website – even if there is just one single getResources call with a random sortorder. It would be great if database caching could always be bypassed by getResources calls which include a sortorder
parameter set to RAND()
.
As briefly discussed here: http://modxcms.com/forums/index.php/topic,59365.msg352465.html#msg352465
Currently, sorting TVs containing numerical data does not give expected results.
Eg. sorting the numbers 1,5,13,6,55 in DESC order outputs: 6,55,5,13,1
See: http://modxcms.com/forums/index.php/topic,52337.msg304369.html#msg304369
&tvFilters=kategorie==kategorie2||kategorie==kategorie3
returns ALL resources instead of resources with TV set to "kategorie2" OR "kategorie3"`
ex: &sortBy=publishedon ASC,createdon
If I have a checkbox TV with a value of 1 and the default is 1 I cannot use the tvFilters parameter to filter on that value. For example a TV set up as:
Variable Name: test_var Input Type: checkbox Input Option Values: True==1 Default Value: 1 Output Type: Default
This call should pull up resources with this TV checked:
[[!getResources? &parents=
[[*id]] &tpl=
my_tpl &includeTVs=
1 &processTVs=
1 &tvFilters=
test_var==1]]
It does not work. I think MODX stores a flag to indicate default value instead of the actual default value.
getResources processing yields a flat view of a resource tree.
There are application where a tree view would be beneficial and a tree view cannot be achieved by chaining getResources calls as parameters (eg filter parameters) cannot be passed down the chain. Other snippets which create a tree, like WayFinder on the other hand do not support filtering.
There is currently a work around using a snippet called getResourcesTree (refer to http://modxcms.com/forums/index.php/topic,43832.msg333619.html#msg333619) but it would be nice to have such a functionality integrated into getResources.
I am happy to provide a patch with that feature integrated using sortby='tree' as a way to configure this feature. I also would then add an additional template tplContainer so containers can be renderd differently.
Setting a custom "idx" start value of 0 ( [[getResources? .. &idx=0
]] ) doesn't work, because the value is checked using empty, which returns true for a 0 value and sets the idx to the default value of 1. The snippet code at about line 264 needs to change to this:
$idx = (empty($idx) && $idx !== '0') ? 1 : intval($idx);
Having the snippet dump the fields array as a default behavior freaks out new users and gives them little feedback about what is wrong.
Two thoughts:
Setting the &limit parameter to '5' doesn't make much sense. I expect to see ALL the results coming back to me, not just the last 5. Make it so that the default value of &limit is set to unlimited. Just like Ditto.
When cherry-picking resources for selection, it is sometimes necessary to use only &resources, with no &parent specified. If you do this, however, getResources apparently defaults to the ID of the current resource for &parent, with annoying consequences. In the situation where I encountered this, I had to work around it by specifying a resource for &parent that has no children - however, that isn't a permanent solution.
If workable, I suggest that the logic be changed to have no default value for &parent, but to use only the resources specified in &resources, if it is present and &parent is not.
Factor based tpl's would allow users to define a tpl for every nth item in the result, providing the tpl for the greatest factor of the current item specified. For example, you might have:
&tpl_n3=`EveryThirdItemTpl` &tpl_n6=`EverySixthItemTpl`
This should allow easy creation of markup to support columns and other complex layouts.
With &parents
unset the following PHP error is returned:
(ERROR @ /index.php) Error 42000 executing statement: Array ( [0] => 42000 [1] => 1064 [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘1,2,3,4,5,7) AND (modResource.context_key IN (‘web’) OR EXISTS(SELECT 1 FROM’ at line 1 )
When unset &parents
should default to [[*id]]
or be ignored if &resources
is set.
I have a multi-lingual blog (french / english)
I use getResources for my RSS feeds.
The english feed works perfectly, since no special characters (letters with accents) are used in english.
how-ever, my french feed would only fetch a rare few blog entries.
After fighting for quite a while with it, I discovered that a special character in the "longtitle" of the document prevents that document from loading into the rss feed.
in my case the character was "é"
Allow to use in-line template without need for the @INLINE
binding, which is not documented nor standard for MODx. Simply, &tpl=
my [[+id]]`` should be enough.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.