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

Auto-populate column within a Dataobject


Go to End


11 Posts   1477 Views

Avatar
nzstephenf

Community Member, 63 Posts

24 December 2014 at 1:29pm

Edited: 24/12/2014 1:32pm

Heya,
So here's the code that is further down on EventSession.php.

----- CODE STARTS HERE -----
public function onBeforeWrite(){
// If there is no URLSegment set, generate one from Title
if((!$this->URLSegment || $this->URLSegment == 'new-event') && $this->Title != 'New Event'){
$Event = Event::get()->byId($this->EventID);
$this->URLSegment = $this->generateURLSegment($Event->Title);
} else if($this->isChanged('URLSegment')) {
// Make sure the URLSegment is valid for use in a URL
$segment = preg_replace('/[^A-Za-z0-9]+/','-',$this->URLSegment);
$segment = preg_replace('/-+/','-',$segment);

// If after sanitising there is no URLSegment, give it a reasonable default
if(!$segment) {
$segment = "event-$this->ID";
}
$this->URLSegment = $segment;
}

// Ensure that this object has a non-conflicting URLSegment value.
$count = 2;
while($this->LookForExistingURLSegment($this->URLSegment)) {
$this->URLSegment = preg_replace('/-[0-9]+$/', null, $this->URLSegment) . '-' . $count;
$count++;
}

parent::onBeforeWrite();
}
----- CODE ENDS HERE -----

How would I go about with doing a "task"? I've never had experience with utlising them but aware of them like the available /dev/tasks :)

Cheers!

Avatar
camfindlay

Forum Moderator, 267 Posts

29 December 2014 at 9:03am

Have a look in the 'cms' > 'Tasks' folder for some examples of tasks. Ideally you'd get a bunch of EventSession objects where the URLSegment is null, then as you already have some onBeforeWrite code you could perhaps simply perform a write on each of the objects in your list which would update the URLSegment for you rather than you doing it manually.

Avatar
nzstephenf

Community Member, 63 Posts

7 January 2015 at 3:00pm

Happy New Year!!
I seemed to have not gotten an email notification or I foolishly marked it as read when I tried to make my inbox ALL READ. Haha. But thanks Cam, I'll have a look into it now. :)

Go to Top