PHx/CustomModifiers
From MODx Wiki
[edit]
Installation
- Login to the MODx manager
- Go to Resources -> Manage Resources -> Snippets
- Click "new snippet"
- Prefix the snippet name with "phx:"
[edit]
Contributed
[edit]
phx:parseLinks
- usage: [+placeholder:parseLinks+]
<?php /* * phx:parseLinks */ $t = $output; return $t; ?>
[edit]
phx:get
- description: get value by name from superglobal $_GET array
- usage: [+phx:get=`name`+]
<?php ?>
[edit]
phx:post
- description: get value by name from superglobal $_POST array
- usage: [+phx:post=`name`+]
<?php ?>
[edit]
phx:request
- description: get value by name from superglobal $_REQUEST array
- usage: [+phx:request=`name`+]
<?php ?>
[edit]
phx:bbcode
- description: parse bb code (also escapes all html and MODx tags characters)
- usage: [+variable:bbcode+]
<?php $string = preg_replace('~\[u\](.+?)\[/u\]~is', '<span style="text-decoration: underline;">\1</span>', $string); $string = preg_replace('~\[link\]www.(.+?)\[/link\]~is', '<a href="http://www.\1" target="_blank">www.\1</a>', $string); $string = preg_replace('~\[link\](.+?)\[/link\]~is', '<a href="\1" target="_blank">\1</a>', $string); $string = preg_replace('~\[link=(.+?)\](.+?)\[/link\]~is', '<a href="\1" target="_blank">\2</a>', $string); $string = preg_replace('~\[url\]www.(.+?)\[/url\]~is', '<a href="http://www.\1" target="_blank">www.\1</a>', $string); $string = preg_replace('~\[url=(.+?)\](.+?)\[/url\]~is', '<a href="\1" target="_blank">\2</a>', $string); $string = preg_replace('~\[img\](.+?)\[/img\]~is', '<img src="\1" alt="[image]" style="margin: 5px 0px 5px 0px" />', $string); $string = preg_replace('~\[img-l\](.+?)\[/img\]~is', '<img src="\1" alt="[image]" style="border: thin solid #DFE5F2; FLOAT: left; MARGIN-RIGHT: 20px" />', $string); $string = preg_replace('~\[img-r\](.+?)\[/img\]~is', '<img src="\1" alt="[image]" style="border: thin solid #DFE5F2; FLOAT: right; MARGIN-LEFT: 20px;" />', $string); $string = preg_replace('~\[color=(.+?)\](.+?)\[/color\]~is', '<font color="\1">\2</font>', $string); $string = preg_replace('~\[left\](.+?)\[/left\]~is', '<div style="text-align:left;">\1</u>', $string); $string = preg_replace('~\[center\](.+?)\[/center\]~is', '<div style="text-align:center;">\1</u>', $string); $string = preg_replace('~\[right\](.+?)\[/right\]~is', '<div style="text-align:right;">\1</u>', $string); return $string; ?>
[edit]
phx:parent
- description: get specified document field from parent document (id)
- usage: [+variable:parent=`field`+]
- defaults to pagetitle
<?php $parent = $modx->getParent($output,1,$field); return $parent[$field]; ?>
[edit]
phx:rating
- description: user rating system based on Jot post count
- usage: [+comment.userpostcount:rating+]
<?php $defaultValue = " <img src=assets/images/icons/star.png />"; $newvalue = '<img src=assets/images/icons/bronze.png />'; } $newvalue = '<img src=assets/images/icons/silver.png />'; } $newvalue = '<img src=assets/images/icons/gold.png />'; } else { $newvalue = $defaultValue; } return $newvalue; ?>
[edit]
phx:url
- description: takes a document id and creates the corresponding modx link (similar to [~ ~])
- usage: [*id:url*]
<?php return $modx->makeUrl($output); ?>
[edit]
phx:uri
- description: takes a string and prepares it to be a part of a valid URI.
- usage: [+string:uri+]
<?php ?>
[edit]
phx:7bit
- description: returns the 7bit representation of a string
- usage: [+string:7bit+]
<?php $text = preg_replace(array('/ß/','/&(..)lig;/','/&([aouAOU])uml;/','/&(.)[^;]*;/'),array('ss',"$1","$1".'e',"$1"),$text); return $text; ?>
[edit]
phx:nohttp
- description: Removes the http:// from a URL, to create a display-friendly web address
- usage: [+string:nohttp+]
<?php return $url; ?>
[edit]
phx:phpthumb
- [+myimage+] contains the full path to the image
- [+myimage:phpthumb=`w=450`+]
- [+myimage:phpthumb=`w=450&ar=x&fltr{}=gray&fltr{}=rcd|8|1`+]
- [+myimage+] contains only the filename
- [+myimage:phpthumb=`w=450#[(base_path)]assets/images/`+]
- [+myimage+] contains no data referring to the image
- [+myimage:phpthumb=`w=450#[(base_path)]assets/images/myimage.jpg#1`+]
- added by: maFF
[edit]
phx:thumb
- description: Similar to maFF's phx:phpthumb but just uses a standard phpThumb installation. Requires phpThumb. I'm adding this in addition to the version above because options are always good, and this method just uses an unmodified version of phpThumb. (It also doesn't use the hashing that maFF's version does, so if you need that, use the above version.)
- usage: (basically the same as maFF's version)
- [+myimage+] contains the full path to the image
- [+myimage:phpthumb=`w=450`+]
- [+myimage:phpthumb=`w=450&ar=x&fltr{}=gray&fltr{}=rcd|8|1`+]
- installation: change the path in the code to wherever you like to put your phpThumb. Make sure you make phpThumb's cache directory writable and set phpThumb's config file to how you want it.
<?php $output = '/assets/snippets/thumb/phpThumb.php?src=' . $output . $options; return $output; ?>
- added by: themancan
[edit]
phx:stripsnippet
- description: Removes cached and uncached snippet in output (usefull in Ditto summarize)
- usage: [+string:stripsnippet+]
<?php $string=$output; return $string; ?>
[edit]
phx:genitive
- description: Creates the genitive of a name - appends either ′s or ′
- usage: [+myname:genitive+] or [+myname:genitive=`endchar|normalchar|otherchar`+]
- added by: maFF
<?php /** * Creates the genitive of a name. * * Examples: * Joe => Joe's * Mary => Mary's * Lucas => Lucas' * * Usage: * * [+myname:genitive+] * [+myname:genitive=`endchar|normalchar|otherchar`+] * */ $name = $output; $endchar = (!empty($options[0])) ? $options[0] : 's'; // char a name has to end with to get treated specially $otherchar = (!empty($options[2])) ? $options[2] : '′'; // char to append to names which end with $endchar else $genitive = $name.$normalchar; return $genitive; ?>
[edit]
phx:ifnotempty
- description: The opposite of the native PHX "isempty" function. Returns the option value ONLY if the input value is empty (excluding whitespace)
- usage: [+string:ifnotempty=`String to return if not empty`+]
<?php return $options; } ?>
[edit]
phx:timesince
- description: Converts a unix timestamp into the number of years, months, days, hours, minutes, and optionally seconds from the current time.
- usage: [+date:timesince+]
- added by: apoxx
<?php { function timeSince($timestamp) { { $timeago = '0 seconds ago.'; return $timeago; } 'year' => 31556926, 'month' => 2629743, 'day' => 86400, 'hour' => 3600, 'minute' => 60, // 'second' => 1 ); $timeago = ''; foreach ($units as $key => $val) { if ($seconds >= $val) { $seconds = ($seconds-($results*$val)); $timeago .= ($results >= 2) ? $results . ' ' . $key . 's, ' : $results . ' ' . $key . ', '; } } } } return timeSince($output); ?>
[edit]
phx:smileys
- description: Converts text patterns into smiley images.
- usage: [*content:smileys*]
- notes: it works with the silk iconset by famfamfam out of the box, if you put the icons into assets/images/smileys. If you want to use other smileys/replacement tags just edit the smileys array.
- added by: maFF
<?php $smiley_basepath = $modx->config['base_url'].'assets/images/smileys/'; $smiley_imagetag = '<img src="'.$smiley_basepath.'##smiley##" alt="##text##" title="##text##" />'; 'emoticon_evilgrin.png', // file name ':D'), // text version for alt and title tags 'emoticon_happy.png', ':)'), 'emoticon_unhappy.png', ':('), 'emoticon_wink.png', ';)'), 'emoticon_tongue.png', ':P'), 'emoticon_surprised.png', ':O') ); foreach($smileys as $search => $smiley) { $thistag = $smiley_imagetag; } return $output; ?>
[edit]
phx:ordinal
- description: Ordinal numbers.
- usage: [*number:ordinal*]
- notes: Adds 'st' to 21 to make it the 21st or 'nd' to 132 for 132nd.
- added by: bwente
<?php { function ordinal($cardinal){ : (($test_c < 4) ? ($test_c < 3) ? ($test_c < 2) ? ($test_c < 1) ? 'th' : 'st' : 'nd' : 'rd' : 'th')); return $cardinal.$ext; } } return ordinal($output); ?>
[edit]
phx:textile
- description: Format content using the textile parser class.
- usage: [*content:textile*]
- notes: You need to download Textile and save the class file into assets/snippets/textile (or change the path in the snippet).
- added by: maFF
<?php $classTextile = $modx->config["base_path"].'assets/snippets/textile/classTextile.php'; require_once($classTextile); } } $textile = new Textile(); $output = $textile->TextileThis($output); return $output; ?>
[edit]
phx:word_limit
- description: limits content to specified number of words (good for news summary)
- usage: [+content:word_limit=`10`+] - limits content to 10 words
- added by: yentsun
<?php $retval = $output; { $retval = $output; } else { } return $retval; ?>
[edit]
phx:character_limit
- description: limits content to specified number of characters (good for news summary)
- usage: [+content:character_limit=`35`+] - limits characters to 35
- added by: themancan
<?php return $output_truncated; ?>
[edit]
phx:alternateClass
- description: Allows rows or other repeating elements to alternate provided the $output is an integer generated by a loop.
- usage: [+phx:alternateClass=`class name`+]
- example: <li class="[+maxigallery.picture.pos:alternateClass=`alt`+]"> outputs <li class="alt"> for every other <li> in the loop.
- notes: Originally designed for use with MaxiGallery to apply an alternating class to the <li> galleryPictureTpl template chunk.
- added by: smashingred
[edit]
phx:docfield
- description: get specified field from document (id)
- usage: [+docid:docfield=`field`+]
- defaults to pagetitle
- added by: bwente
<?php return $docfield[$field]; ?>
[edit]
phx:tv
- description: get a template variable from any page id
- usage: [+phx:tv=`<docid>?<templatevar>`+] , where <docid> is the document id (e.g. 25) and <templatevar> is a template variable associated to the given docid.
- default: if the document id or the template variable don't exist no output is produced
- added by: natalino
<?php $result = $modx->getTemplateVar($tv, 'name', $id, 1); return $result['value']; } ?>
example: placing the place of birth from page id 14, as part of the content in page id 22. Place this content while editing page id 22
[+phx:tv=`14?place_of_birth`+]
[edit]
phx:evenodd
- description: Returns even if even, otherwise odd. Nearly identical to the 'alternateClass' above.
- usage: [+ditto_iteration:evenodd]. Useful for creating alternate templates in Ditto without needing an additional chunk. For example, class="news_item_[+ditto_iteration:evenodd+]" will return news_item_even or news_item_odd
- default: Defaults to "odd", as there is no test to ensure the input is an integer.
- added by: themancan
<?php if ($output % 2) { return 'even'; } else { return 'odd'; } ?>
[edit]
phx:firstlast
- description: Set the number of elements per row. Returns $class_first if it's the first element in a row; $class_last if it's the last.
- usage: [+ditto_iteration:firstlast=`4`+]. Useful for applying classes to the first or last element per row. Kind of like alternate classes, but suited for multiple rows.
<?php if (!$options) { $options = 2; } $class_first = 'alpha'; $class_last = 'omega'; if (($output % $options) == '0') { return $class_first; } elseif (($output % $options) == ($options - 1)) { return $class_last; } else { return; } ?>
[edit]
phx:price_format
- description: Formats a number (TV) to a specific format with a specific number of decimals. Useful for allowing users to enter a "price" TV, and making sure you have a consistent XX.YY format, even if they just enter XX.
- usage: [+product_price:price_format+].
- default: Defaults to 2 decimal places.
- added by: themancan
<? function format_number($str,$decimal_places='2',$decimal_padding="0"){ /* taken from http://us2.php.net/manual/en/function.number-format.php#54799 */ /* firstly format number and shorten any extra decimal places */ /* Note this will round off the number pre-format $str if you dont want this fucntionality */ $number[1] = (isset($number[1]))?$number[1]:''; // to fix the PHP Notice error if str does not contain a decimal placing. return (float) $number[0].'.'.$decimal; } } return format_number($output); ?>
[edit]
phx:make_url
- description: Takes a string and makes it a URL if it's not.
- example: If a user enters 'www.example.com', but you want to link to that, if you did 'href="www.example.com"' it wouldn't work. Yeah, you should be validating that on the server side anyway, but this modifier is useful in case you can't. It will make "www.example.com" into "http://www.example.com", but won't touch things that start with an http:// or https://
- usage: [+member_url:make_url+]
- default: Adds "http://" to the beginning of the string.
- added by: themancan
<? return $output; } else { return 'http://' . $output; } ?>
[edit]
phx:return_domain
- description: Takes a string and returns the domain.
- example: 'http://wiki.modxcms.com/index.php?title=PHx/CustomModifiers&action=edit' would return just 'wiki.modxcms.com'.
- note: This is designed to be used on a MODx TV with an Input Type of 'URL'. It doesn't do any sort of error checking.
- usage: [+url:return_domain+].
- added by: themancan
<?php return $matches[1]; ?>
[edit]
phx:zeropad
- description: Zero-padding a string. Takes the number of total digits as the input.
- example: This can be useful when you want to output some kind of ID numbers with fixed digits, or US zip codes: "00001", "00124", "90210"
- usage: [+ditto_iteration:zeropad=`3`] [*zipcode:zeropad=`5`*]
- default: No zero-padding.
- added by: ryanlwh
<? ?>
[edit]
phx:mydate
- description: Date modified for non-Unix timestamp dates.
- example: Useful when the date is stored differently, e.g. Friday, May 12, 2008 etc.
- usage: [+date:mydate=`%d-%m-%Y %H:%M`+]
- added by Legatissima who found it in the support forum, written by Doze
<? ?>
[edit]
phx:age
- description: Takes a unixtime date input and calculates the age in years. It kind of fakes leap years but it's probably close enough for most uses.
- example: Set a TV called "date_birth" with the unixtime widget, then output it as [+date_birth:age+]
- usage: [+date_birth:age+]
- added by: themancan
<?php return $output; ?>
[edit]
phx:striptags
- description: Strips all HTML and PHP tags. You can use the optional parameter to specify tags which should _not_ be stripped
- usage: [+comment.content:striptags=`<a><img>`+]
<?php return($modx->stripTags($output, $options)); ?>
[edit]
phx:outer
- description: Surround not empty string with text.
- usage: [+string:outer=`before|after`+]
- example: Ditto has no outer template, but you can use this [*phx:input=`[!Ditto? &noResults=` `!]`:outer=`<div class="xyz">|</div>`*]
- added by: Jako
<?php $outer = ''; return $outer; ?>
[edit]
phx:cdata
- description: Surround content with CDATA without the whitespace
- usage: [+content:cdata+]
- added by: Hori
<?php function cdata($inhoud){ $before='<![CDATA['; $after=']]>'; return $before.$inhoud.$after; } } return cdata($output); ?>
