Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

Customising output DOM data


Reply

4 Posts   793 Views

Avatar
J2-Paul

15 October 2010 at 7:35am (Last edited: 15 October 2010 7:36am), Community Member, 51 Posts

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;
   }

}

Avatar
UncleCheese

15 October 2010 at 8:15am 4085 Posts

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

Avatar
J2-Paul

15 October 2010 at 10:02pm Community Member, 51 Posts

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

Avatar
UncleCheese

16 October 2010 at 2:47am 4085 Posts

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