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   829 Views

Avatar
J2-Paul

Community Member, 51 Posts

15 October 2010 at 7:35am

Edited: 15/10/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;
   }

}

Avatar
UncleCheese

Forum Moderator, 4096 Posts

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

Avatar
J2-Paul

Community Member, 51 Posts

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

Avatar
UncleCheese

Forum Moderator, 4096 Posts

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