This is a key method in Moski2.net - you use it to retrieve content from the system and is probably what you will use the most.
$contentType
is the id attribute of the root node
(the contentType
element) in your XML specification of a content
type. Omitting the $options
parameter you will retrieve
all content nodes of the type you are asking for, sorted according to
the sorting
element of your XML specification.
What you get back is an array of content items. Each array is in turn another array, having the field names as keys and their content as values. For example, using M2_Content::get() to retrieve a set of content items from the example type in Section 4, “Defining Content Types” could return something like this (when piped through var_dump()):
array(7) { [0]=> array(10) { ["id"]=> string(4) "9369" ["created"]=> string(10) "1235936255" ["created_by"]=> string(1) "1" ["updated"]=> string(0) "" ["updated_by"]=> string(0) "" ["sort_value"]=> string(4) "9369" ["heading"]=> string(6) "Here's a heading for ya'" ["published"]=> string(10) "1236034801" ["illustration"]=> array(7) { ["id"]=> string(4) "1399" ["bytes"]=> string(3) "181" ["filename"]=> string(12) "glotextb.gif" ["mime"]=> string(9) "image/gif" ["visibility"]=> string(6) "PUBLIC" ["web_path"]=> string(33) "/files/bin/1399/orig/glotextb.gif" } ["body"]=> string(8) "<p>spweee</p>" } [1]=> array(10) { ["id"]=> string(4) "9370" ["created"]=> string(10) "1235936270" ["created_by"]=> string(1) "1" ["updated"]=> string(0) "" ["updated_by"]=> string(0) "" ["sort_value"]=> string(4) "9370" ["heading"]=> string(6) "Head Me!" ["published"]=> string(10) "1233442801" ["illustration"]=> array(0) { } ["body"]=> string(10) "<p>sdf</p>" } [2]=> array(10) { ["id"]=> string(5) "10995" ["created"]=> string(10) "1244208471" ["created_by"]=> string(1) "1" ["updated"]=> string(0) "" ["updated_by"]=> string(0) "" ["sort_value"]=> string(5) "10995" ["heading"]=> string(12) "My third heading" ["published"]=> string(10) "1213308001" ["illustration"]=> array(0) { } ["body"]=> string(0) "<p>Not much here.</p>" } }
Content items are returned in the way they appear in the administration, unless you specify the orderBy parameter as described below.
A number of the fields are internal Moski2.net fields that you may or may not find useful
id
created
created_by
updated
updated_by
sort_value
Following these fields are the ones defined by you in the XML specification for the type.
As you can tell from the illustration
field above,
file based fields, like file
and image
will
contain an array of information, instead of a string:
Table 7. Associative array returned for binary fields
Key | Description |
---|---|
id | ID number of the binary. This is used internally by Moski2.net and has no meaning to you. |
bytes | File size in bytes. |
filename | Original filename of the uploaded file. |
mime | Mime type of the file, as detected when uploaded. |
visibility | PUBLIC or SECURE. If SECURE, web_path will be empty and you must use M2_Content::streamBinary() to stream out the file. |
web_path | Path to the file on your web site - this can be used to create a link directly to the file. NoteYou can not access the file via file system
functions, as it is really proxied from the administrative
pages of your site. If you need to do things with the
file, like scaling it if it is an image, see the
|
This is also true for the special filemanager field type,
except that it has no visibility
key, as there is no way
to determine visibility for files uploaded through the file
manager.
Following is a list of keys that you can send along with the
$options
array to
M2_Content::get($options)
in order to modify the result
set.
whereAdd
The whereAdd
parameter lets you specify
something similar to a where clause in SQL. There Moski2.net
where clause is described as an array of arrays, each holding
a single condition.
Here's an example that retrieves all records for a type,
where the date
field is before August 1. 2008
(converted to a unix timestamp), and the id
field
is not 1, 2 or 3.:
<?php $conditionA = array('fieldName' => 'date', 'operator' => '<', 'value' => 1217566800); $conditionB = array('fieldName' => 'id', 'operator' => 'notin', 'value' => array(1,2,3); $options = array('whereAdd' => array($conditionA, $conditionB)); $data = M2_Content::get('mytype', $options); ?>
You can specify as many conditions as you like, but only content that satisfies all conditions will be returned. In other words, the conditions are ANDed and not ORed.
Below is a list of available operators when creating
where clauses for a getContent
query.
Table 8. Values for the operator field in where clauses
Operator | Explanation |
---|---|
< | Less than |
<= | Less than or equal to |
= | Equal to |
>= | Greater than or equal to |
> | Greater than |
!= | Not equal to |
in | When supplying an array to the
This mimics the |
notin | This operator works in the opposite way
of the |
like | This operator matches records where
This mimics the
|
There is a limitation on multidimensional fields
(checkbox
and text
variants of
the link
type):
The text
variant can currently not be
queried with whereAdd
at all, and
checkbox
variants can be queried with the
operators '=
', '!=
' and
'in
'.
orderBy
This is an implementation of the standard SQL
order
by expression - you simply supply the field name
that the results should be sorted by, and then supply an
orderDirection
key to specify whether to sort
ascending or descending.
If omitted, the results will be returned as they appear in the administration (that is, as defined in the type definition).
orderDirection
Goes hand in hand with the orderBy
key -
see explanation above.
Can be one of asc
or
desc
.
limit
Use this to limit the number of records you want to retrieve.
This is similar to the standard LIMIT
keyword from standard SQL.
offset
Use this to start retrieval with an offset - similar to
the standard OFFSET
SQL keyword.
idsAsKeys
By default results are returned in a flat array containing a hash for each entry. If you set idsAsKeys to 1, you will get back a hash with the node IDs as keys instead.
This effectively disables any sorting, as the result array is sorted by key.
cache
Set to 1 for retrieving cached results and 0 to perform a fresh query against the Moski2Engine. Disabling the cache is only relevant if you suspect that the cache is corrupt, as the cache is automatically maintained.
filter
Moski2.net has a number of built-in filters that you can
pipe content through before retrieving it. The
filter
parameter is an array with field names as
keys and an array of filters to apply as keys. These keys are
also arrays, using the filter name as keys, and their
arguments as values. Confused? Check out this example:
<?php $filter = array( 'myfilefield' => array( 'ImageTag' => array( 'width' => 600, 'height' => 400, 'attributes' => array( 'title' => 'mytitle', 'alt' => 'myalt' ) ) ) ); $opts = array('binariesV2' => 1, 'filter' => $filter); $data = M2_Content::get('mytype', $opts); ?>
This example pipes all myfilefield
values
through the ImageTag
filter with
width
, height
and
attributes
arguments. This means that the data
returned by M2_Content::get()
now has
ready-to-use <img>
elements in all the
myfilefield
slots, instead of just an array of
information about the file.
An example <img>
element could look
like this:
<img src="/files/bin/6428/600/400/abm6.png" style="width:596px;height:400" />
You can apply as many filters to as many fields as you like. However, we currently only have one filter, and you would only want to apply it to image fields:
Table 9. Available filters
Filter name | Description | Arguments |
---|---|---|
ImageTag | Turns an image field into an
HTML img element. When requested by a
client afterwards, the image will automatically be
scaled if it doesn't fit within the maximum dimensions
of the element. |
|
ImageUseAltAsCaption | Extracts the ALT attribute from an image and presents it beneath the image. |
Example: <?php $filter = array( 'myimagefield' => array( 'ImageUseAltAsCaption' => array( 'selector' => 'myclass', 'method' => 'div', 'wrapperClass' => 'mywrapper' ) ) ); $opts = array('filter' => $filter); $data = M2_Content::get('mytype', $opts); ?> The result of applying this filter to a string like this: <img class="myclass" src="/path/to/img.png" alt="Me love alt" /> Will be something like this: <div class="mywrapper"> <div class="image"> <img class="myclass" src="/path/to/img.png" alt="Me love alt" /> </div> <div class="caption">Me love alt</div> </div> |
ObfuscateEmail | Obfuscates email addresses to prevent spammer harvesting of your emails. |
Example: <?php $filter = array( 'myfield' => array( 'ObfuscateEmail' => array( 'method' => 'rtl' ) ) ); $opts = array('filter' => $filter); $data = M2_Content::get('mytype', $opts); ?> If you just send an empty array along as argument Moski2.net will pick an obfuscation method for you. |
Ideas for future filters include automatic URL2link generation and custom filter possibilities.