Jump to:

23552 Posts in 19349 Topics by 2891 members

General Questions

SilverStripe Forums » General Questions » sub classing and overriding function - basic problem

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 877 Views
  • nakashu
    Avatar
    Community Member
    24 Posts

    sub classing and overriding function - basic problem Link to this post

    Hi.
    This shouldnt be a problem to solve for experienced php codes, which I am not. I will welcome help and guidence.

    The Thing:
    I am subclassing SelectionGroup.php with MyFunc.php ...
    In SelectionGroup.php is a function FieldSet() ...
    I want to add one line of code into it, but left all the stuff doing its work.. the line I want to add is $item->addExtraClass('selected');
    all relevant code also on pastie: http://pastie.org/1182475

    function FieldSet() {
          $items = parent::FieldSet()->toArray();

          $count = 0;
          $firstSelected = $checked ="";
          foreach($items as $key => $item) {
             if(strpos($key,'//') !== false) {
                list($key,$title) = explode('//', $key,2);
             } else {
                $title = $key;
             }
             if($this->value == $key) {
                $firstSelected = " class=\"selected\"";
                $checked = " checked=\"checked\"";
             }
             
             $itemID = $this->ID() . '_' . (++$count);
             $extra = array(
                "RadioButton" => "<input class=\"selector\" type=\"radio\" id=\"$itemID\" name=\"$this->name\" value=\"$key\"$checked />",
                "RadioLabel" => "<label for=\"$itemID\">$title</label>",
                "Selected" => $firstSelected,
             );
             if(is_object($item)) $newItems[] = $item->customise($extra);
             else $newItems[] = new ArrayData($extra);

             $firstSelected = $checked ="";         
          }
          return new DataObjectSet($newItems);
       }

    /// I want to edit section
    if($this->value == $key) {
          $firstSelected = " class=\"selected\"";
          $checked = " checked=\"checked\"";
       }
    // adding my line
    if($this->value == $key) {
          $firstSelected = " class=\"selected\"";
          $checked = " checked=\"checked\"";
          $item->addExtraClass('selected');
       }

    the class is added on the right place when I edit the core file SelectionGroup.php.
    But when I copy the function to MyFunc.php, my label and other code disapear, as it will be working with other set of data.

    So if anyone can drop the code of the FieldSet() that should be in MyFunc.php for it to work same as in SelectionGroup.php. that would be awesome..
    Even better would be if you can also enlighten me what is going behind the scenes, and why it wont work, and / or point me to a solution for checking the selected radio button in the __constructor of MyFunc.php

    Thank you all in advance.
    Daniel.

    877 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.