1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-24 15:23:11 +02:00

Add a getNonEmptyValue() method to LanguagesPageFieldValue class and make use of it in ProcessPageEdit to avoid situations where headline was otherwise blank

This commit is contained in:
Ryan Cramer
2019-12-03 08:40:51 -05:00
parent 346218a524
commit 8a5a4b4461
2 changed files with 40 additions and 7 deletions

View File

@@ -3,7 +3,7 @@
/** /**
* Serves as a multi-language value placeholder for field values that contain a value in more than one language. * Serves as a multi-language value placeholder for field values that contain a value in more than one language.
* *
* ProcessWire 3.x, Copyright 2016 by Ryan Cramer * ProcessWire 3.x, Copyright 2019 by Ryan Cramer
* https://processwire.com * https://processwire.com
* *
*/ */
@@ -191,6 +191,32 @@ class LanguagesPageFieldValue extends Wire implements LanguagesValueInterface, \
return $this->data[$this->defaultLanguagePageID]; return $this->data[$this->defaultLanguagePageID];
} }
/**
* Get non-empty value in this order: current lang, default lang, other lang, failValue
*
* @param string $failValue Value to use if we cannot find a non-empty value
* @return string
* @since 3.0.147
*
*/
public function getNonEmptyValue($failValue = '') {
$value = (string) $this;
if(strlen($value)) return $value;
$value = (string) $this->getDefaultValue();
if(strlen($value)) return $value;
foreach($this->wire('languages') as $language) {
$value = $this->getLanguageValue($language->id);
if(strlen($value)) break;
}
if(!strlen($value)) $value = $failValue;
return $value;
}
/** /**
* The string value is the value in the current user's language * The string value is the value in the current user's language
* *
@@ -245,7 +271,7 @@ class LanguagesPageFieldValue extends Wire implements LanguagesValueInterface, \
* *
* Fulfills \IteratorAggregate interface. * Fulfills \IteratorAggregate interface.
* *
* @return ArrayObject * @return \ArrayObject
* *
*/ */
public function getIterator() { public function getIterator() {

View File

@@ -2984,10 +2984,16 @@ class ProcessPageEdit extends Process implements WirePageEditor, ConfigurableMod
public function setupHeadline() { public function setupHeadline() {
$titlePage = null; $titlePage = null;
$page = $this->page;
if($this->page && $this->page->id) { if($page && $page->id) {
$page = $this->page;
$title = $page->get('title'); $title = $page->get('title');
if(is_object($title) && !strlen("$title") && wireInstanceOf($title, 'LanguagesPageFieldValue')) {
/** @var LanguagesPageFieldValue $title */
$title = $title->getNonEmptyValue($page->name);
} else {
$title = (string) $title;
}
if(empty($title)) { if(empty($title)) {
if($this->wire('pages')->names()->isUntitledPageName($page->name)) { if($this->wire('pages')->names()->isUntitledPageName($page->name)) {
$title = $page->template->getLabel(); $title = $page->template->getLabel();
@@ -2995,6 +3001,7 @@ class ProcessPageEdit extends Process implements WirePageEditor, ConfigurableMod
$title = $page->get('name'); $title = $page->get('name');
} }
} }
if(empty($title)) $title = $page->name;
} else if($this->parent && $this->parent->id) { } else if($this->parent && $this->parent->id) {
$titlePage = $this->parent; $titlePage = $this->parent;
$title = rtrim($this->parent->path, '/') . '/[...]'; $title = rtrim($this->parent->path, '/') . '/[...]';
@@ -3004,6 +3011,7 @@ class ProcessPageEdit extends Process implements WirePageEditor, ConfigurableMod
} }
$browserTitle = sprintf($this->_('Edit Page: %s'), $title); $browserTitle = sprintf($this->_('Edit Page: %s'), $title);
$headline = '';
if($this->field) { if($this->field) {
if(count($this->fields) == 1) { if(count($this->fields) == 1) {
@@ -3019,11 +3027,10 @@ class ProcessPageEdit extends Process implements WirePageEditor, ConfigurableMod
} else if($titlePage) { } else if($titlePage) {
$headline = $titlePage->get('title|name'); $headline = $titlePage->get('title|name');
} else {
$headline = $title;
} }
if(empty($headline)) $headline = $title;
$this->headline($headline); $this->headline($headline);
$this->browserTitle($browserTitle); $this->browserTitle($browserTitle);
} }