From 47392c5dded651ec558b85e621fc700bffdbdf20 Mon Sep 17 00:00:00 2001 From: Colin Tucker Date: Thu, 25 Feb 2016 12:32:29 +1100 Subject: [PATCH 1/3] Initial readonly field modifications --- code/TagField.php | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/code/TagField.php b/code/TagField.php index 5b60250..3be84e9 100644 --- a/code/TagField.php +++ b/code/TagField.php @@ -406,4 +406,49 @@ class TagField extends DropdownField { return true; } + + /** + * Converts the field to a readonly variant. + * + * @return TagField_Readonly + */ + public function performReadonlyTransformation() + { + $copy = $this->castedCopy('TagField_Readonly'); + + return $copy; + } } + +/** + * A readonly extension of TagField useful for non-editable items within the CMS. + * + * @package forms + * @subpackage fields + */ +class TagField_Readonly extends TagField +{ + protected $readonly = true; + + /** + * Render the field as HTML. + * + * @param array $properties + * @return HTMLText + */ + public function Field($properties = array()) + { + $options = array(); + + foreach ($this->getOptions() as $option) { + $options[] = $option->Title; + } + + $field = ReadonlyField::create($this->name.'_Readonly', $this->title); + + $field->setForm($this->form); + $field->setValue(implode(', ', $options)); + + return $field->Field(); + } +} \ No newline at end of file From d3e636e6474d32f7f3eef78bfc52877eaa8c28c0 Mon Sep 17 00:00:00 2001 From: Colin Tucker Date: Thu, 25 Feb 2016 13:06:21 +1100 Subject: [PATCH 2/3] Filters selected options and renders as readonly --- code/TagField.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/code/TagField.php b/code/TagField.php index 3be84e9..2092e1c 100644 --- a/code/TagField.php +++ b/code/TagField.php @@ -415,7 +415,7 @@ class TagField extends DropdownField public function performReadonlyTransformation() { $copy = $this->castedCopy('TagField_Readonly'); - + $copy->setSource($this->getSource()); return $copy; } } @@ -431,7 +431,7 @@ class TagField_Readonly extends TagField protected $readonly = true; /** - * Render the field as HTML. + * Render the readonly field as HTML. * * @param array $properties * @return HTMLText @@ -440,7 +440,7 @@ class TagField_Readonly extends TagField { $options = array(); - foreach ($this->getOptions() as $option) { + foreach ($this->getOptions()->filter('Selected', true) as $option) { $options[] = $option->Title; } From d8b8c875bb0b16c9716009721dceeea93ef7a361 Mon Sep 17 00:00:00 2001 From: Colin Tucker Date: Thu, 25 Feb 2016 13:18:56 +1100 Subject: [PATCH 3/3] Formatting fix --- code/TagField.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/code/TagField.php b/code/TagField.php index 2092e1c..93db395 100644 --- a/code/TagField.php +++ b/code/TagField.php @@ -448,7 +448,6 @@ class TagField_Readonly extends TagField $field->setForm($this->form); $field->setValue(implode(', ', $options)); - return $field->Field(); } -} \ No newline at end of file +}