Home

In this section you will find short snippets of code that we have found useful and often hard to uncover.

Using canCreate(), canEdit(), canDelete() and canPublish() to manage page type permissions

Often you will want to control which users can create, edit, delete and publish certain page types. For example you may only want high level users to be able to create ContactPage page types, or prevent low level users from deleting HomePage page types. This can easily be achieved by adding these functions to the page type model (usually just before getCMSFields()). Then within the function you can define conditionals which decide whether to return true or false.

So let's say you have created the permission code in this snippet. We can now use that permission code and the canCreate() function to decide whether a particular user can create this page type:

 

Read More >>

Change the CMS "Help" Link

Usually you can change the CMS menu settings from your site configuration, but the "Help" link is hard-coded to allow for translations. This snippet, placed in your site configuration file will override the "Help" link with a link back to your site:

Read More >>

Displaying Fields From a Set of Pages on Another Page

Although ultimately requiring very little code, displaying data from a set of pages on another page can seem confusing for those new to SilverStripe. This example will display all of the Images from all the staff pages on our current page. This works by first returning all the staff pages to our template and cycling through each of them, drawing the Image for each one.

Add the following function to your current pages Controller:

Then inside your template you can simply cycle through the results using <% control GetStaffPages %> like so:

 

Read More >>

Adding a Thumbnail to a DataObjectManager or Complex Table Field

This snippet lets you add a thumbnail to items in UncleCheese's Data Object Manager module or a regular Complex Table field. All you need to do is create a function that returns a thumbnail and refer to this function in your DOM or CTF definition.

So add a function like this to your DataObject class:

Read More >>

Using @import in your _config.php for Live site Database Details

Have you ever mistakenly overwritten your live site _config.php with your local one? Even if you haven't it can be a pain to keep copying new lines from your local config file to the live one. This little trick means you won't ever have to worry about it again!

First create a file on your live server with the relevant Database info:

mysite/_liveConfig.php

then simply add this line to the bottom of your regular _config.php file:

mysite/_config.php

The @ infront of the include allows it to fail silently when the file does not exist (i.e. on your local server). You can now upload your _config.php file as much as you like without interfering with the live database settings!

Read More >>

Creating a Page Export Function

Need to create an 'export' function for a page? Do it quickly and cleanly with HTTP::sendFileToBrowser. In your page controller, add a new action 'export':

Now you just need to create Page_Export.ss, and direct the user to the URL "/export/". Your page will be returned to the browser as an attachment to download.

 

Read More >>

Dynamically Generating a Dropdown List

If you want to create a drop down list (or option set) dynamically, for example from a set of pages or DataObjects, you can use the DataObject::get() call along with the function toDropdownMap() to generate it like so:

Read More >>

Create a Field in 'Page' but not it's Decendents

This snippet allow you to add a field only to a page of type 'Page' and not those pages which extend Page.

Read More >>

Create a Permission Code

Ever wanted to add an extra permission code so that when you add permissions to a group you can create a custom option? Here's how it's done; Simply add this code to your Page_Controller class as well as adding "implements PermissionProvider" on the end of the class definition like so:

That will create two new permission codes: 'USER' and 'SUPERUSER', so you can then call permission::check('USER') anywhere in your code to see if the current user is in a group with USER permissions.

 

Read More >>

getting objects from multiple child pages

When you want to create a DataObjectSet encompasing objects from all the current pages children you can do something like this:

The first line creates an array of the ID's from all, with the current page and any descendant page ID's.

The second line onwards then gets all Objects that have an ObjectPageID that is either the current page's ID, or any child page of the current page.

You can then add the usual filter, join or limit clauses to the DataObject::get call.

Read More >>

Check to see if a widget area has any widgets

If you want to check that a WidgetArea actually has widgets, and only display it if it has, you can add this function to your Page_Controller (where widgetBarID is the name of your widgetArea + ID):

Then in your template file, MyPage.ss you can use HasWidgets to check whether the WidgetArea should be rendered:

Read More >>

Create an 'Edit this Page' link

For some sites you may want your client to be able to click a link on a particlar page and be taken strait to that page in the admin area. Here's what a link that does just that would look like:

If you wanted this to only show up when a user is logged in you could use the CurrentMember call to check. So it would look like this:

You could also take this a step further and write a function in your page controller to check whether the current user has permission to access the CMS using Permission::check('CMS_ACCESS_CMSMain') call.

Read More >>

Get Errors E-mailed to you

If your site is live it means you are probebly not getting errors reported in the view, so it's handy to have them emailed to you so you can still debug.

Add this line to your mysite/_config.php file and expect some mail!

mysite/_config.php

Read More >>

Dynamic Year Function

Using this function you can create a dynamic year in the footer of your site. Simply add the function to your Page_Controller class and use $Year in your footer template.

This will output "2009" for this year and then "2009-2010" next year etc.

Read More >>

Resizing an image in a custom <img> tag

You can resize images from within the template quite easily. Using an image defined as $Image1 in the class this is how we would create a few different resizes of it.

Read More >>
1 2 3 4 5 |