1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-09 08:17:12 +02:00

Some updates to the wireRegion()/region() function to support locking prepend or append

This commit is contained in:
Ryan Cramer
2016-12-14 11:36:26 -05:00
parent 2b9a7adbcb
commit 7e262fa60d

View File

@@ -801,6 +801,8 @@ function wireIsCallable($var, $syntaxOnly = false, &$callableName = '') {
* - Specify "*" to retrieve all defined regions in an array. * - Specify "*" to retrieve all defined regions in an array.
* - Prepend a "+" to the region name to have it prepend your given value to any existing value. * - Prepend a "+" to the region name to have it prepend your given value to any existing value.
* - Append a "+" to the region name to have it append your given value to any existing value. * - Append a "+" to the region name to have it append your given value to any existing value.
* - Prepend a "++" to region name to make future calls without "+" automatically prepend.
* - Append a "++" to region name to make future calls without "+" to automatically append.
* @param null|string $value If setting a region, the text that you want to set. * @param null|string $value If setting a region, the text that you want to set.
* @return string|null|bool|array Returns string of text when getting a region, NULL if region not set, or TRUE if setting region. * @return string|null|bool|array Returns string of text when getting a region, NULL if region not set, or TRUE if setting region.
* *
@@ -808,6 +810,7 @@ function wireIsCallable($var, $syntaxOnly = false, &$callableName = '') {
function wireRegion($key, $value = null) { function wireRegion($key, $value = null) {
static $regions = array(); static $regions = array();
static $locked = array();
if(empty($key) || $key === '*') { if(empty($key) || $key === '*') {
// all regions // all regions
@@ -822,17 +825,24 @@ function wireRegion($key, $value = null) {
} else { } else {
// set region // set region
$pos = strpos($key, '+'); $pos = strpos($key, '+');
if($pos !== false) $key = trim($key, '+'); if($pos !== false) {
$lock = strpos($key, '++') !== false;
$key = trim($key, '+');
if($lock !== false && !isset($locked[$key])) {
$locked[$key] = $lock === 0 ? '^' : '$'; // prepend : append
}
}
$lock = isset($locked[$key]) ? $locked[$key] : '';
if(!isset($regions[$key])) $regions[$key] = ''; if(!isset($regions[$key])) $regions[$key] = '';
if($pos === 0) { if($pos === 0 || ($pos === false && $lock == '^')) {
// prepend // prepend
$regions[$key] = $value . $regions[$key]; $regions[$key] = $value . $regions[$key];
} else if($pos) { } else if($pos || ($pos === false && $lock == '$')) {
// append // append
$regions[$key] .= $value; $regions[$key] .= $value;
} else if($value === '') { } else if($value === '') {
// clear region // clear region
unset($regions[$key]); if(!$lock) unset($regions[$key]);
} else { } else {
// insert/replace // insert/replace
$regions[$key] = $value; $regions[$key] = $value;