(MODX)EvolutionCMS.snippets.ddGetDocuments
A snippet for fetching and parsing resources from the document tree or custom DB table by a custom rule., (*1)
Requires
Installation
Just run the following PHP code in your sources or Console :, (*2)
// Include (MODX)EvolutionCMS.libraries.ddInstaller
require_once(
$modx->getConfig('base_path')
. 'assets/libs/ddInstaller/require.php'
);
// Install (MODX)EvolutionCMS.snippets.ddGetDocuments
\DDInstaller::install([
'url' => 'https://github.com/DivanDesign/EvolutionCMS.snippets.ddGetDocuments',
]);
If ddGetDocuments
is not exist on your site, ddInstaller
will just install it.
If ddGetDocuments
is already exist on your site, ddInstaller
will check it version and update it if needed.
Manually
1. Elements → Snippets: Create a new snippet with the following data
Snippet name: ddGetDocuments
.
Description: <b>1.8</b> A snippet for fetching and parsing resources from the document tree or custom DB table by a custom rule.
.
Category: Core
.
Parse DocBlock: no
.
Snippet code (php): Insert content of the ddGetDocuments_snippet.php
file from the archive.
2. Elements → Manage Files:
Create a new folder assets/snippets/ddGetDocuments/
.
Extract the archive to the folder (except ddGetDocuments_snippet.php
).
Parameters description
Core parameters
fieldDelimiter
Description: The field delimiter to be used in order to distinct data base column names in those parameters which can contain SQL queries directly, e. g. providerParams->groupBy
, providerParams->orderBy
and providerParams->filter
.
Valid values: string
Default value: '`'
Data provider parameters
provider
, (*3)
Description: Name of the provider that will be used to fetch documents.
Data provider names are case insensitive (the following names are equal: parent
, Parent
, pArEnT
, etc).
Valid values:
Default value: 'parent'
providerParams
, (*4)
Description: Parameters to be passed to the provider.
Valid values:
stringJsonObject
— as JSON
stringHjsonObject
— as HJSON
stringQueryFormatted
— as Query string
It can also be set as a native PHP object or array (e. g. for calls through $modx->runSnippet
):
Default value: —
providerParams->filter
, (*5)
Description: The filter condition in SQL-style to be applied while resource fetching.
Notice that all fields/tvs names specified in the filter parameter must be wrapped in fieldDelimiter
.
Valid values: string
Default value: '`published` = 1 AND `deleted` = 0'
providerParams->total
, (*6)
Description: The maximum number of the resources that will be returned.
Valid values: integer
Default value: — (all)
providerParams->offset
, (*7)
Description: Resources offset.
Valid values: integer
Default value: 0
providerParams->groupBy
, (*8)
Description: Group items that have the same values into summary item (like SQL GROUP BY
).
Valid values: stringCommaSeparated
Default value: —
providerParams->groupBy[$i]
, (*9)
Description: Document field or TV by which the items will be grouped.
Valid values: string
Required
providerParams->orderBy
, (*10)
Description: A string representing the sorting rule.
TV names also can be used.
Valid values: string
Default value: —
Providers → Parent (&provider=`parent`
)
providerParams->parentIds
, (*11)
Description: Parent ID(s).
Valid values:
array
stringCommaSeparated
Default value: [0]
providerParams->parentIds[i]
, (*12)
Description: Document ID.
Valid values: integer
Required
providerParams->depth
, (*13)
Description: Depth of children documents search.
Valid values: integer
Default value: 1
providerParams->excludeIds
, (*14)
Description: The document IDs which need to exclude.
Valid values:
array
stringCommaSeparated
Default value: —
providerParams->excludeIds[i]
, (*15)
Description: Document ID.
Valid values: integer
Required
Providers → Select (&provider=`select`
)
providerParams->ids
, (*16)
Description: Document IDs to output.
Valid values:
array
stringCommaSeparated
Required
providerParams->ids[i]
, (*17)
Description: Document ID.
Valid values: integer
Required
Providers → Customdbtable (&provider=`customdbtable`
)
Get resources from custom DB table., (*18)
providerParams->resourcesTableName
Description: DB table to get resources from.
Valid values: string
Required
Outputter → String (&outputter=`string`
)
outputterParams->templates->item
, (*22)
Description: Item template.
Available placeholders:
[+
any document field or tv name+]
— Any document field name or TV.
[+
any of extender placeholders+]
— Any extender placeholders (see extenders description below).
[+
any placeholders from placeholders
param+]
— Any custom placeholders (see outputterParams->placeholders
description below).
[+itemNumber+]
— Item number started from 1.
[+itemNumberZeroBased+]
Item number started from 0.
[+total+]
— number of returned items
[+totalFound+]
— number of found items
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Required
outputterParams->templates->itemFirst
, (*23)
Description: Template for the first item.
Has the same placeholders as outputterParams->templates->item
.
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default value: == outputterParams->templates->item
.
outputterParams->templates->itemLast
, (*24)
Description: Template for the last item.
Has the same placeholders as outputterParams->templates->item
.
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default value: == outputterParams->templates->item
.
outputterParams->templates->wrapper
, (*25)
Description: Wrapper template.
Available placeholders:
[+
any document field or tv name+]
— Any document field name or TV.
[+
any of extender placeholders+]
— Any extender placeholders (see extenders description below).
[+
any placeholders from placeholders
param+]
— Any custom placeholders (see outputterParams->placeholders
description below).
[+ddGetDocuments_items+]
[+total+]
— number of returned items
[+totalFound+]
— number of found items
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default value: —
outputterParams->templates->noResults
, (*26)
Description: A chunk or text to output when no items found.
Has the same placeholders as outputterParams->templates->wrapper
.
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default value: —
outputterParams->placeholders
, (*27)
Description: Additional data has to be passed into templates->item
, templates->itemFirst
, templates->itemLast
and templates->wrapper
.
Arrays are supported too: some[a]=one&some[b]=two
=> [+some.a+]
, [+some.b+]
; some[]=one&some[]=two
=> [+some.0+]
, [some.1]
.
Valid values: object
Default value: —
outputterParams->placeholders->{$name}
, (*28)
Description: Key for placeholder name and value for placeholder value.
Valid values: string
Required
outputterParams->itemGlue
, (*29)
Description: The string that combines items while rendering.
Valid values: string
Default value: ''
Outputter → Json (&outputter=`json`
)
outputterParams->docFields
, (*30)
Description: Document fields to output (including TVs).
Valid values:
array
stringCommaSeparated
Default value: 'id'
outputterParams->docFields[i]
, (*31)
Description: Document field or TV.
You can use custom aliases instead of field names for output using the '='
separator (for example: 'pagetitle=title'
, 'content=text'
, etc).
Valid values:
string
— document field
stringSeparated
— document field and it's alias separated by '='
Required
outputterParams->templates->{$docFieldName}
, (*32)
Description: You can use templates for some document fields.
Templates will be used before JSON conversion of results. So you don't need to care about characters escaping.
It is useful for manipulations with doc field values through running snippets.
Available placeholders:
[+value+]
— the field value
[+
any document field or tv name+]
— Any document field name or TV specified in outputterParams->docFields
[+itemNumber+]
— item number started from 1
[+itemNumberZeroBased+]
— item number started from 0
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default value: —
outputterParams->templates->noResults
, (*33)
Description: A chunk or text to output when no items found.
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default value: '[]'
outputterParams->templates->wrapper
, (*34)
Description: Wrapper template.
Available placeholders:
[+ddGetDocuments_items+]
[+total+]
— number of returned items
[+totalFound+]
— number of found items
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default value: '[+ddGetDocuments_items+]'
Outputter → Sitemap (&outputter=`sitemap`
)
Output in Sitemap XML format ., (*35)
outputterParams->priorityTVName
, (*36)
Description: Name of TV which sets the relative priority of the document.
Valid values: stringTvName
Default value: 'general_seo_sitemap_priority'
outputterParams->changefreqTVName
, (*37)
Description: Name of TV which sets the change frequency.
Valid values: stringTvName
Default value: 'general_seo_sitemap_changefreq'
outputterParams->templates->item
, (*38)
Description: Item template.
Available placeholders:
[+
any document field or tv name+]
— Any document field name or TV.
[+
any of extender placeholders+]
— Any extender placeholders (see extenders description below).
[+itemNumber+]
— Item number started from 1.
[+itemNumberZeroBased+]
Item number started from 0.
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default:
xml
<url>
<loc>[(site_url)][~[+id+]~]</loc>
<lastmod>[+editedon+]</lastmod>
<priority>[+[+priorityTVName+]+]</priority>
<changefreq>[+[+changefreqTVName+]+]</changefreq>
</url>
outputterParams->templates->wrapper
, (*39)
Description: Wrapper template.
Available placeholders:
[+
any document field or tv name+]
— Any document field name or TV.
[+
any of extender placeholders+]
— Any extender placeholders (see extenders description below).
[+ddGetDocuments_items+]
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default value:
xml
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
[+ddGetDocuments_items+]
</urlset>
Outputter → Yandexmarket (&outputter=`yandexmarket`
)
Output in YML format ., (*40)
outputterParams->shopData
, (*41)
Description: Shop data.
Valid values: object
Required
outputterParams->shopData->shopName
, (*42)
Description: Short shop name, length <= 20.
Valid values: string
Required
outputterParams->shopData->companyName
, (*43)
Description: Company legal name. Internal data that not be displayed but required by Yandex.
Valid values: string
Required
outputterParams->shopData->agency
, (*44)
Description: Web developers agency name.
Valid values: string
Default value: —
outputterParams->shopData->currencyId
, (*45)
Description: Currency code (see Yandex docs ).
Valid values: string
Default value: 'RUR'
outputterParams->shopData->platform
, (*46)
Description: <platform>
tag content.
Valid values: string
Default value: '(MODX) Evolution CMS'
outputterParams->shopData->version
, (*47)
Description: <version>
tag content.
Valid values: string
Default value: '[(settings_version)]'
outputterParams->categoryIds_last
, (*48)
Description: Allows to add additional parent elements in the category section. If empty only immediate parents of goods will be returned.
Valid values: stringCommaSeparated
Default value: —
outputterParams->categoryIds_last[i]
, (*49)
Description: Document ID.
Valid values: integer
Required
outputterParams->offerFields
, (*50)
Description: Offer fields parameters.
Valid values: object
Required
outputterParams->offerFields->{$fieldName}
, (*51)
Description: Offer field parameter.
Valid values:
string
— the parameter can be set as a document field name
object
— or as an object with additional params (see below)
Default value: —
outputterParams->offerFields->{$fieldName}->docFieldName
, (*52)
Description: A document field name that contains offer field value.
Valid values: stringTvName
Required
outputterParams->offerFields->{$fieldName}->disableEscaping
, (*53)
Description: You can disable escaping special characters ('
, "
, &
, <
, >
) in the field value.
Valid values: boolean
Default value: false
outputterParams->offerFields->{$fieldName}->valuePrefix
, (*54)
Description: You can set custom string that will be added before the field value.
Valid values: string
Default value: —
outputterParams->offerFields->{$fieldName}->valueSuffix
, (*55)
Description: You can set custom string that will be added after the field value.
Valid values: string
Default value: —
outputterParams->offerFields->price
, (*56)
Description: A document field name, that contains offer price.
If a document field value is empty, but outputterParams->offerFields->priceOld
is set, the last will be used instead.
Valid values: stringTvName
Required
outputterParams->offerFields->priceOld
, (*57)
Description: A document field name, that contains old offer price (must be less then outputterParams->offerFields->price
or will not be used).
Valid values: stringTvName
Default value: —
outputterParams->offerFields->picture
, (*58)
Description: A document field name, that contains offer picture.
Valid values: stringTvName
Default value: —
outputterParams->offerFields->name
, (*59)
Description: A document field name, that contains offer name.
If a document field value is empty, the pagetitle
field will be used instead.
Valid values:
stringDocFieldName
stringTvName
Default value: 'pagetitle'
outputterParams->offerFields->model
, (*60)
Description: A document field name, that contains offer model.
Valid values:
stringDocFieldName
stringTvName
Default value: —
outputterParams->offerFields->model
, (*61)
Description: A document field name, that contains offer vendor.
Valid values:
stringDocFieldName
stringTvName
Default value: —
outputterParams->offerFields->available
, (*62)
Description: A document field name, that contains offer availability status (true
|false
).
Valid values:
stringTvName
''
— always display 'true'
.
Default value: ''
outputterParams->offerFields->description
, (*63)
Description: A document field name, that contains offer description (less than 3 000 chars).
Valid values:
stringDocFieldName
stringTvName
Default value: —
outputterParams->offerFields->salesNotes
, (*64)
Description: A document field name, that contains offer sales_notes " title="See online sales_notes ">sales_notes .
Valid values:
stringDocFieldName
stringTvName
Default value: —
outputterParams->offerFields->manufacturerWarranty
, (*65)
Description: A document field name, that contains offer manufacturer warraynty status (true
|false
).
Valid values: stringTvName
Default value: —
outputterParams->offerFields->countryOfOrigin
, (*66)
Description: A document field name, that contains offer country of origin.
Valid values:
stringDocFieldName
stringTvName
Default value: —
outputterParams->offerFields->homeCourierDelivery
, (*67)
Description: A document field name, that contains offer courier delivery status (true
|false
).
Valid values: stringTvName
Default value: —
outputterParams->offerFields->dimensions
, (*68)
Description: A document field name, that contains offer dimensions (length, width, height) including packaging.
Specify dimensions in centimeters. Format: three positive numbers with accuracy of 0.001, using a dot as the decimal separator.
The numbers must be separated by the slash character /
without spaces.
Valid values:
stringDocFieldName
stringTvName
Default value: —
outputterParams->offerFields->weight
, (*69)
Description: Item weight in kilograms including packaging.
Some categories have limits on the minimum or maximum weight.
Download a list of minimum and maximum weight values .
In any category, the weight can be specified accurate to one thousandth (for example, 1.001, using a dot as a decimal point).
If the minimum value is set to 0
, there is no minimum weight limit and it can be specified starting from one gram (0.001 kg).
Valid values:
stringDocFieldName
stringTvName
Default value: —
outputterParams->offerFields->additionalParams
, (*70)
Description: A document field name, that contains offer param " title="See online param ">param elements.
Valid values:
stringDocFieldName
stringTvName
Default value: —
outputterParams->offerFields->customData
, (*71)
Description: A document field name, that contains custom text that must be inserted before </offer>
.
Valid values:
stringDocFieldName
stringTvName
Default value: —
outputterParams->templates->wrapper
, (*72)
Description: Wrapper template.
Available placeholders:
[+
any document field or tv name+]
— Any document field name or TV.
[+
any of extender placeholders+]
— Any extender placeholders (see extenders description below).
[+ddGetDocuments_items+]
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default value:
xml
<?xml version="1.0" encoding="utf-8"?>
<yml_catalog date="[+generationDate+]">
<shop>
<name>[+shopData.shopName+]</name>
<company>[+shopData.companyName+]</company>
<url>[(site_url)]</url>
<platform>[+shopData.platform+]</platform>
<version>[+shopData.version+]</version>
[+shopData.agency+]
<currencies>
<currency id="[+shopData.currencyId+]" rate="1" />
</currencies>
<categories>[+ddGetDocuments_categories+]</categories>
<offers>[+ddGetDocuments_items+]</offers>
</shop>
</yml_catalog>
outputterParams->templates->categories_item
, (*73)
Description: Category item template.
Available placeholders:
[+id+]
— Category doc ID.
[+value+]
— Category name.
[+parent+]
— Category parent ID.
[+attrs+]
— Attributes string (e. g. parentId="42"
).
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default value:
xml
<category id="[+id+]"[+attrs+]>
[+value+]
</category>
outputterParams->templates->offers_item
, (*74)
Description: Offer item template.
Available placeholders:
[+
any document field or tv name+]
— Any document field name or TV.
[+
any of extender placeholders+]
— Any extender placeholders (see extenders description below).
[+itemNumber+]
— Item number started from 1.
[+itemNumberZeroBased+]
Item number started from 0.
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default:
xml
<offer id="[+id+]" available="[+available+]">
<url>[(site_url)][~[+id+]~]</url>
[+name+]
[+price+]
[+priceOld+]
[+shopData.currencyId+]
[+categoryId+]
[+picture+]
[+vendor+]
[+model+]
[+description+]
[+salesNotes+]
[+manufacturerWarranty+]
[+countryOfOrigin+]
[+delivery+]
[+dimensions+]
[+weight+]
[+additionalParams+]
[+customData+]
</offer>
outputterParams->templates->{'offers_item_elem' . $FieldName}
, (*75)
Description: You can set custom template for any offer element.
Specify an element name in accordance with offerFields->
parameters, e. g. outputterParams->templates->offers_item_elemCountryOfOrigin
.
Available placeholders:
[+tagName+]
— Element tag name.
[+value+]
— Element value.
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default: —
Extenders parameters
extendersParams->pagination->wrapperTpl
, (*80)
Description: Chunk to be used to output the pagination.
Available placeholders:
[+previous+]
— HTML code of navigation block to the previous page (see parameters description below).
[+next+]
— HTML code of navigation block to the next page (see parameters description below).
[+pages+]
— HTML code of pages navigalion block (see parameters description below).
[+totalPages+]
— Total number of pages.
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default value:
html
<div class="pagination_container">
<div class="pagination clearfix">
<div class="pagination_links">[+previous+]</div>
<div class="pagination_pages">[+pages+]</div>
<div class="pagination_links">[+next+]</div>
</div>
</div>
extendersParams->pagination->pageTpl
, (*81)
Description: Chunk to be used to output pages within the pagination.
Available placeholders:
[+url+]
— Page URL.
[+page+]
— Page number.
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default value:
html
<a href="[~[*id*]~][+url+]" class="strl">[+page+]</a>
extendersParams->pagination->currentPageTpl
, (*82)
Description: Chunk to be used to output the current page within the pagination.
Available placeholders:
[+url+]
— Page URL.
[+page+]
— Page number.
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default value:
html
<a href="[~[*id*]~][+url+]" class="strl active">[+page+]</a>
extendersParams->pagination->nextTpl
, (*83)
Description: Chunk to be used to output the navigation block to the next page.
Available placeholders:
[+url+]
— Next page URL.
[+totalPages+]
— Total number of pages.
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default value:
html
<a href="[~[*id*]~][+url+]" class="pagination_next strl"><span>Следующая</span> →</a><br>
<small><a href="[~[*id*]~]?page=[+totalPages+]" class="pagination_last strl"><span>Последняя</span> →</a></small>
extendersParams->pagination->nextOffTpl
, (*84)
Description: Chunk to be used to output the navigation block to the next page if there are no more pages after.
Available placeholders:
[+totalPages+]
— Total number of pages.
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default value:
html
<span class="pagination_next"><span>Следующая</span> →</span><br>
<small><span class="pagination_last"><span>Последняя</span></span> →</small>
extendersParams->pagination->previousTpl
, (*85)
Description: Chunk to be used to output the navigation block to the previous page.
Available placeholders:
[+url+]
— Next page URL.
[+totalPages+]
— Total number of pages.
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default value:
html
<a href="[~[*id*]~][+url+]" class="pagination_prev strl">← <span>Предыдущая</span></a><br>
<small><a href="[~[*id*]~]" class="pagination_first strl">← <span>Первая</span></a></small>
extendersParams->pagination->previousOffTpl
, (*86)
Description: Chunk to be used to output the navigation block to the previous page if there are no more pages before.
Available placeholders:
[+totalPages+]
— Total number of pages.
Valid values:
stringChunkName
string
— use inline templates starting with @CODE:
Default value:
html
<span class="pagination_prev">← <span>Предыдущая</span></span><br>
<small><span class="pagination_first">← <span>Первая</span></span></small>
Extenders → Tagging (&extenders=`tagging`
)
extendersParams->tagging->tagsDocumentField
, (*87)
Description: The document field (TV) contains tags.
Valid values: stringTvName
Default value: 'tags'
extendersParams->tagging->tagsDelimiter
, (*88)
Description: Tags delimiter in the document field.
Valid values: string
Default value: ', '
extendersParams->tagging->tagsRequestParamName
, (*89)
Description: The parameter in $_REQUEST
to get the tags value from.
Valid values: string
Default value: 'tags'
Extenders → Search (&extenders=`search`
)
extendersParams->search->docFieldsToSearch
, (*90)
Description: Document fields to search in (including TVs).
Valid values:
array
stringCommaSeparated
Default value: 'pagetitle,content'
extendersParams->search->docFieldsToSearch[i]
, (*91)
Description: Document fields or TV.
Valid values:
stringDocFieldName
stringTvName
Required
Extenders → SortFromURL (&extenders=`sortFromURL`
)
$_GET['orderBy']
Description: A string representing the sorting rule similar to providerParams->orderBy
.
Valid values: string
Default value: —
Examples
All examples are written using HJSON , but if you want you can use vanilla JSON instead., (*92)
Simple fetching child documents from a parent with ID = 1
[[ddGetDocuments?
&providerParams=`{
parentIds: 1
depth: 1
}`
&outputterParams=`{
templates: {
item:
'''
@CODE:<div>
<h2>[+pagetitle+]</h2>
<p>[+introtext+]</p>
[+someTV+]
</div>
'''
}
}`
]]
Simple fetching child documents from a parent with ID = 1
with the providerParams->filter
Add a filter that would not output everything. Let's say we need only published documents., (*93)
Don't forget about fieldDelimiter
. , (*94)
[[ddGetDocuments?
&fieldDelimiter=`#`
&providerParams=`{
parentIds: 1
depth: 1
filter: "#published# = 1"
}`
&outputterParams=`{
templates: {
item: documents_item
}
}`
]]
So we can filter as much as we like (we can use AND
and OR
, doucument fields and TVs):, (*95)
[[ddGetDocuments?
&fieldDelimiter=`#`
&providerParams=`{
parentIds: 1
depth: 1
filter:
'''
#published# = 1 AND
#hidemenu# = 0 OR
#SomeTVName# = 1
'''
}`
&outputterParams=`{
templates: {
item: documents_item
}
}`
]]
Sorting by TV with the date
type (providerParams->orderBy
)
Dates in DB stored in specific format (01-02-2017 08:59:45
) and sorting works unexpectedly at first sight.
So, we can't just type:, (*96)
&providerParams=`{
orderBy: "#TVName# DESC"
}`
For correct working we need to convert date from DB to Unixtime for sorting:, (*97)
&providerParams=`{
orderBy: "STR_TO_DATE(#TVName#, '%d-%m-%Y %H:%i:%s') DESC"
}`
When TVName
— TV name for sorting by., (*98)
Outputters → JSON (&outputter=`json`
): Fetch documents and output in JSON
[[ddGetDocuments?
&providerParams=`{parentIds: 1}`
&outputter=`json`
]]
Returns:, (*99)
[
{"id": "2"},
{"id": "3"},
{"id": "4"}
]
Outputters → JSON (&outputter=`json`
): Set documents fields to output
[[ddGetDocuments?
&providerParams=`{parentIds: 1}`
&outputter=`json`
&outputterParams=`{
docFields: id,pagetitle,menuindex,someTV
}`
]]
Returns:, (*100)
[
{
"id": "2",
"pagetitle": "About",
"menuindex": "0",
"someTV": "Some value"
},
{
"id": "3",
"pagetitle": "Partners",
"menuindex": "1",
"someTV": ""
},
{
"id": "4",
"pagetitle": "Contacts",
"menuindex": "2",
"someTV": ""
}
]
Outputters → JSON (&outputter=`json`
): Use custom aliases instead of field names
[[ddGetDocuments?
&providerParams=`{
parentIds: 1
}`
&outputter=`json`
&outputterParams=`{
docFields: pagetitle=name,menuindex=position
}`
]]
Returns:, (*101)
[
{
"name": "Denial",
"position": "0",
},
{
"name": "Anger",
"position": "1",
},
{
"name": "Bargaining",
"position": "2",
},
{
"name": "Depression",
"position": "3",
},
{
"name": "Acceptance",
"position": "4",
}
]
Group items that have the same field values into summary item (providerParams->orderBy
)
For example we have the following documents with TV gender
:, (*102)
Mary Teresa, female
Mahatma Gandhi, male
Tenzin Gyatso, male
Dmitry Muratov, male
ICAN, none
And we want to make a gender list with unique items:, (*103)
[[ddGetDocuments?
&fieldDelimiter=`#`
&providerParams=`{
// The parent of our documents
parentIds: 42
// The field by which the items will be grouped
groupBy: "#gender#"
}`
&outputter=`json`
&outputterParams=`{
docFields: gender
}`
]]
Returns:, (*104)
[
{"gender": "female"},
{"gender": "male"},
{"gender": "none"}
]
[[ddGetDocuments?
&fieldDelimiter=`#`
&providerParams=`{
parentIds: "[*id*]"
filter: "#published# = 1"
total: 3
orderBy: "#pub_date# DESC`"
}`
&outputterParams=`{
templates: {
item: documents_item
wrapper:
''''
@CODE:[+ddGetDocuments_items+]
[+extenders.pagination+]
'''
noResults: "@CODE:"
}
}`
&extenders=`pagination`
&extendersParams=`{
pagination: {
wrapperTpl: general_pagination
nextTpl: general_pagination_next
previousTpl: general_pagination_prev
nextOffTpl: general_pagination_nextOff
previousOffTpl: general_pagination_prevOff
pageTpl: general_pagination_page
currentPageTpl: general_pagination_pageCurrent
}
}`
]]
&providerParams=`{parentIds: "[*id*]"}`
— fetch current doc children.
&providerParams=`{filter: "#published# = 1"}`
— only published.
&providerParams=`{total: 3}`
— items per page.
&providerParams=`{orderBy: "#pub_date# DESC"}
— sort by publish date, new first.
&outputterParams=`{templates: {item: documents_item}}`
— item template (chunk name).
&outputterParams=`{templates: {wrapper: "@CODE:[+ddGetDocuments_items+][+extenders.pagination+]"}}`
— we need set where pagination will being outputted.
&outputterParams=`{templates: {noResults: "@CODE:"}}`
— return nothing if nothing found.
&extendersParams=`{pagination: {}}`
— pagination templates (see the parameters description).
Extenders → Search (&extenders=`search`
)
Call the snippet at the page with search results.
Let's specify where and how deep we will search.
Set up filter to get only necessary documets., (*105)
[[ddGetDocuments?
&fieldDelimiter=`#`
&providerParams=`{
parentIds: 1
depth: 3
filter:
'''
#published# = 1 AND
#deleted# = 0 AND
#template# = 11
'''
}`
&extenders=`search`
&extendersParams=`{
docFieldsToSearch: pagetitle,content,someTv
}`
&outputterParams=`{
templates: {
item: documents_item
}
}
]]
Then just add to the page URL ?query=Some query text
and the snippet returns only documents contain that string in the pagetitle
, content
or someTv
fields., (*106)
We recommend to use cashed snippet calls and turn on document caching type with $_GET parameters in CMS configuration., (*107)
// Include (MODX)EvolutionCMS.libraries.ddTools
require_once(
$modx->getConfig('base_path')
. 'assets/libs/ddTools/modx.ddtools.class.php'
);
// Run (MODX)EvolutionCMS.snippets.ddGetDocuments
\DDTools\Snippet::runSnippet([
'name' => 'ddGetDocuments',
'params' => [
// It is convenient to set the parameter as a native PHP array or object
'providerParams' => [
'parentIds' => 1,
],
'outputter' => 'json',
],
]);
Links
, (*108)