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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

sub classing and overriding function - basic problem


Go to End


1498 Views

Avatar
nakashu

Community Member, 24 Posts

27 September 2010 at 3:08am

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.