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

Add $input->setUrlSegments() convenience method to WireInput

This commit is contained in:
Ryan Cramer
2021-10-01 13:19:38 -04:00
parent 62a64de93b
commit fc1b252d04

View File

@@ -673,8 +673,10 @@ class WireInput extends Wire {
* *
*/ */
public function setUrlSegment($num, $value) { public function setUrlSegment($num, $value) {
$config = $this->wire()->config;
$sanitizer = $this->wire()->sanitizer;
$num = (int) $num; $num = (int) $num;
$maxLength = $this->wire('config')->maxUrlSegmentLength; $maxLength = $config->maxUrlSegmentLength;
if($maxLength < 1) $maxLength = 128; if($maxLength < 1) $maxLength = 128;
if(is_null($value)) { if(is_null($value)) {
// unset // unset
@@ -687,14 +689,31 @@ class WireInput extends Wire {
$this->urlSegments = $urlSegments; $this->urlSegments = $urlSegments;
} else { } else {
// sanitize to standard PW name format // sanitize to standard PW name format
$urlSegment = $this->wire('sanitizer')->name($value, false, $maxLength); $urlSegment = $sanitizer->name($value, false, $maxLength);
// if UTF-8 mode and value changed during name sanitization, try pageNameUTF8 instead // if UTF-8 mode and value changed during name sanitization, try pageNameUTF8 instead
if($urlSegment !== $value && $this->wire('config')->pageNameCharset == 'UTF8') { if($urlSegment !== $value && $config->pageNameCharset == 'UTF8') {
$urlSegment = $this->wire('sanitizer')->pageNameUTF8($value, $maxLength); $urlSegment = $sanitizer->pageNameUTF8($value, $maxLength);
} }
$this->urlSegments[$num] = $urlSegment; $this->urlSegments[$num] = $urlSegment;
} }
}
/**
* Set/replace all URL segments
*
* #pw-group-URL-segments
* #pw-internal
*
* @param array $urlSegments Regular/non-indexed PHP array where first element is first URL segment
* @since 3.0.186
*
*/
public function setUrlSegments(array $urlSegments) {
$n = 1;
foreach($urlSegments as $urlSegment) {
$this->setUrlSegment($n, $urlSegment);
$n++;
}
} }
/** /**