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

issues with Slug


Go to End


2 Posts   1121 Views

Avatar
FredRF

Community Member, 3 Posts

27 July 2016 at 1:26am

Hi,

I am fairly new to silverstripe and need some help with slugs.

I have a database containing products categorised by Type and Range

Slugs are used to access the correct information from the database using the following format
www.mydomain.com/products/thetype/therange/theproduct
ie. www.mydomain.com/products/computers/desktop/computermodel

All of this was written by someone else, and it works very well.

I need to create using the same slug format, some information blocks that would be accessible via www.mydomain.com/products/thetype/therange/informationblock1

In my ProductRange class, I have

private static $has_many = array(
'InformationBlocks' => 'InformationBlock'
);
 

in my InformationBlock class, I have

 private static $has_one = array(
        'Range' => 'ProductRange'
    );
 

This allows me to create multiple information blocks in the backend and it all works well

In the frontend pages, I am able to display all my information blocks, depending on which range I am viewing

<% loop $InformationBlocks %>
	<li<% if $MultipleOf(3) %> class="last"<% end_if %>>
		<h3>$Title</h3>
		<img src="$Image.SetWidth(600).CroppedImage(600,400).URL"  
		alt="$Image.Title" title="$Image.Title" >
		$Body
		<a href="$Top.Link()$Top.Type.Slug/$Top.Range.Slug/$Slug">Visit</a>
	</li>
<% end_loop %>

The issue I have, is that silverstripe does not have a value for $Slug the link to the information block.

<a href="$Top.Link()$Top.Type.Slug/$Top.Range.Slug/$Slug">Visit</a>

I do not understand how the $Slug variable gets created by Silverstripe and why it adds it for the type and Range but not for the information blocks

I set my InformationBlock class as follow thinking it would help, but it has not done the trick

class InformationBlock extends SlugModel

Would anyone have a suggestion?

Avatar
Mo

Community Member, 541 Posts

7 August 2016 at 11:04pm

Hi FredRF,

First off $Slug isn't generated by Silverstripe by default, I am guessing whoever wrote your Type and Range classes added them to it (if you check the php files for each of those classes I am guessing you will see something like:

    static $db = array(
        "Slug" => "Varchar"
    );

Next up, information block needs to extend DataObject (or an extension of dataobject).

Finally, in order to get InformationBlock to load like that you would need to overwrite the default handleAction (or handleRequest) on the controller that renders your range object. It would probably help if you posted the code up for RangeController (or whatever it is called).

Cheers,

Mo