Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Customising output DOM data

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 731 Views
  • J2-Paul
    Avatar
    Community Member
    51 Posts

    Customising output DOM data Link to this post

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

    }

  • UncleCheese
    Avatar
    4085 Posts

    Re: Customising output DOM data Link to this post

    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

  • J2-Paul
    Avatar
    Community Member
    51 Posts

    Re: Customising output DOM data Link to this post

    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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Customising output DOM data Link to this post

    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

    731 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.