7913 Posts in 1355 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » Customising output DOM data
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 631 Views |
-
Customising output DOM data

15 October 2010 at 7:35am Last edited: 15 October 2010 7:36am
I have a dataobject FAQItem and I want the Active data to display as Y (1) or N (0) in the CMS DOM list.
Below is my code for the object with the function 'getActiveState' creating the custom values. However, when I call this in the field list array on the page all the rows show as "Y" dispite the fact that some are set to false (0).
Bit of newbit to PHP and SS. Any help much appreciated.
Thanks
Paul
<?php
class FAQItem extends DataObject {static $db = array(
'Question' => 'Text',
'Answer' => 'HTMLText',
'Active' => 'Boolean'
);static $has_one = array(
'FAQs' => 'FAQ'
);function getCMSFields_forPopup() {
$fields = new FieldSet();
$fields->push( new Checkboxfield('Active'));
$fields->push( new TextField( 'Question' ));
$fields->push( new SimpleWysiwygField( 'Answer' ) );
return $fields;}
public function getActiveState() {
if ($this->Active=1)
{
$activestate="Y";
}
else
{
$activestate="N";
}
return $activestate;
}}
-
Re: Customising output DOM data

15 October 2010 at 8:15am
First, let's clean up that function a bit. No need to have 12 lines when all you need is 1.
public function getActiveState() {
return $this->Active ? "Y" : "N";
}Then, in your headings array, add your custom getter:
'ActiveState' => 'Active?'
--------------------
SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com -
Re: Customising output DOM data

15 October 2010 at 10:02pm
Many thanks Sir Cheese. All working now.
Couple of points.
Agreed, 1 line is a little more efficient than my code!
. However, why was mine not working?
With the function defined as 'getActiveState' putting 'ActiveState' => 'Active?' did not work. I needed to put 'getActiveState' => 'Active?'. Was this just a typo or am I missing something important?
Loving the module by the way!
Paul
-
Re: Customising output DOM data

16 October 2010 at 2:47am
No, "getActiveState" is incorrect. The "get" is implicit in the headers array. That's really weird. I've never come across that one before..
--------------------
SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com
| 631 Views | ||
|
Page:
1
|
Go to Top |
