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

Update MarkupFieldtype to auto-link page references when page is viewable

This commit is contained in:
Ryan Cramer
2019-02-22 16:34:37 -05:00
parent 41e2bbd22b
commit c6410c6478

View File

@@ -107,9 +107,14 @@ class MarkupFieldtype extends WireData implements Module {
$a = array(); $a = array();
foreach($value as $page) { foreach($value as $page) {
$v = $page->getFormatted($property); $v = $page->getFormatted($property);
$a[] = $field->type->markupValue($page, $field, $v); $v = $field->type->markupValue($page, $field, $v);
if($page->viewable()) {
$a[] = "<a href='$page->url'>$v</a>";
} else {
$a[] = $v;
} }
return $this->valueToString($a); }
return $this->arrayToString($a, false);
} else { } else {
$value = $value->explode($property, array('getMethod' => 'getFormatted')); $value = $value->explode($property, array('getMethod' => 'getFormatted'));
} }
@@ -248,10 +253,11 @@ class MarkupFieldtype extends WireData implements Module {
* Convert any value to a string * Convert any value to a string
* *
* @param mixed $value * @param mixed $value
* @param bool $encode
* @return string * @return string
* *
*/ */
protected function valueToString($value) { protected function valueToString($value, $encode = true) {
if(is_object($value) && ($value instanceof Pagefiles || $value instanceof Pagefile)) { if(is_object($value) && ($value instanceof Pagefiles || $value instanceof Pagefile)) {
return $this->objectToString($value); return $this->objectToString($value);
} else if(WireArray::iterable($value)) { } else if(WireArray::iterable($value)) {
@@ -259,7 +265,7 @@ class MarkupFieldtype extends WireData implements Module {
} else if(is_object($value)) { } else if(is_object($value)) {
return $this->objectToString($value); return $this->objectToString($value);
} else { } else {
return $this->wire('sanitizer')->entities1($value); return $encode ? $this->wire('sanitizer')->entities1($value) : $value;
} }
} }
@@ -267,14 +273,15 @@ class MarkupFieldtype extends WireData implements Module {
* Render an unknown array or WireArray to a string * Render an unknown array or WireArray to a string
* *
* @param array|WireArray $value * @param array|WireArray $value
* @param bool $encode
* @return string * @return string
* *
*/ */
protected function arrayToString($value) { protected function arrayToString($value, $encode = true) {
if(!count($value)) return ''; if(!count($value)) return '';
$out = "<ul class='MarkupFieldtype'>"; $out = "<ul class='MarkupFieldtype'>";
foreach($value as $v) { foreach($value as $v) {
$out .= "<li>" . $this->valueToString($v) . "</li>"; $out .= "<li>" . $this->valueToString($v, $encode) . "</li>";
} }
$out .= "</ul>"; $out .= "</ul>";
return $out; return $out;
@@ -288,8 +295,16 @@ class MarkupFieldtype extends WireData implements Module {
* *
*/ */
protected function objectToString($value) { protected function objectToString($value) {
if($value instanceof WireArray && !$value->count()) return ''; if($value instanceof WireArray) {
if($value instanceof Page) return $value->get('title|name'); if(!$value->count()) return '';
}
if($value instanceof Page) {
if($value->viewable()) {
return "<a href='$value->url'>" . $value->get('title|name') . "</a>";
} else {
return $value->get('title|name');
}
}
if($value instanceof Pagefiles || $value instanceof Pagefile) { if($value instanceof Pagefiles || $value instanceof Pagefile) {
$out = $this->renderInputfieldValue($value); $out = $this->renderInputfieldValue($value);
} else { } else {