Tag: Site Settings

  • Install Tidy on Ubuntu

    Not something big or important, but I just tried several ways to get tidy library on my Ubuntu box, and none other then this worked. Might save some searching to others.

    sudo apt-get install php5-tidy

    Tidy is recommended for running SilverStripe (not required).

    php5 comes with Tidy library preinstalled but it has to be compiled –with-tidy which is sometimes skipped (well, on the box I’m setting up for example).

  • Silverstripe 3 Grid Field Config Options

    The grid field system in Silverstripe 3 has a number of preset config options – here’s what they do, and what they look like:

    No Config

    If we don’t add any config options, the grid field just displays a list of items…  We can’t view the record details, or edit anything though…

    $gridfield =newGridField("RegisterEvents","RegisterEvent", $this->RegisterEvents());
    $fields->addFieldToTab('Root.Events', $gridfield);

    grid1

    $gridfield = new GridField(“RegisterEvents”, “RegisterEvent”, $this->RegisterEvents());
    $fields->addFieldToTab(‘Root.Events’, $gridfield);

    GridFieldConfig_RecordViewer

    $gridFieldConfig =GridFieldConfig_RecordViewer::create();
    $gridfield =newGridField("RegisterEvents","RegisterEvent", $this->RegisterEvents(), $gridFieldConfig);
    $fields->addFieldToTab('Root.Events', $gridfield);

     

    This option just adds in the ability to click the magnifying glass and view the details of each item – but not edit anything.

    grid2

    GridFieldConfig_RecordEditor

    $gridFieldConfig =GridFieldConfig_RecordEditor::create();
    $gridfield =newGridField("RegisterEvents","RegisterEvent", $this->RegisterEvents(), $gridFieldConfig);
    $fields->addFieldToTab('Root.Events', $gridfield);

     

    Now we’re starting to get something useful – a list that lets us Add, View, and Remove records…

    grid3

    GridFieldConfig_RelationEditor

    The final option is the Relation Edition set up. This adds features to work with ‘has-many’ and ‘many-many’ relationships.

    See our article about Many_Many relations for more details

    Custom Config

    Or, you can just create an empty configuration, and add the bits you need….

    $gridFieldConfig =GridFieldConfig::create()->addComponents(
    newGridFieldToolbarHeader(),
    newGridFieldAddNewButton('toolbar-header-right'),
    newGridFieldSortableHeader(),
    newGridFieldDataColumns(),
    newGridFieldPaginator(15),
    newGridFieldEditButton(),
    newGridFieldDeleteAction(),
    newGridFieldDetailForm(),newGridFieldFilterHeader(),
    );

     

  • Extending SilverStripe 3 Site Settings

    When you first install your SilverStripe site, the ‘Settings’ menu is pretty limited, offering just Title, Tagline, and a choice of templates.

    This is fine, but it’s often useful to add other options, such as logo upload, links to facebook, uploading a banner image that appears on every page.

    MySiteConfig.php

    In your ‘mysite/code/’ folder, create a new file called ‘MySiteConfig.php’, and insert the following text:

    <?php
    
    class MySiteConfig extends DataExtension {     
    
    	 public static $has_one = array(
    		'HeaderImage' => 'Image',
    		'LogoImage' => 'Image',		
    	);
    
    	 public static $db = array(			
    		'FacebookURL' => "Varchar(250)",
    		'VimeoURL' => "Varchar(250)",				
    	  );
    
        public function updateCMSFields(FieldList $fields) {
    	   $fields->addFieldToTab("Root.Main", new UploadField("LogoImage", "Choose an image for your site logo"));
    	   $fields->addFieldToTab("Root.Main", new UploadField("HeaderImage", "Choose an image for the site header"));
    	   $fields->addFieldToTab("Root.Main", new TextField("FacebookURL", "Enter the full URL of your Facebook page"));	 
    	   $fields->addFieldToTab("Root.Main", new TextField("VimeoURL", "Enter the full URL of your Vimeo page"));	
        }
    }

    This file creates a ‘DataExtension’. A DataExtension, as the name suggests, is used to extend any set of data. You declare the additional databse fields and relations just as you would when creating a new page.

    And you use the function ‘public function updateCMSFields {}’ to add the fields to the CMS page, just as you would when creating a new page.

    _config.php

    In your ‘mysite’ folder, open the ‘_config,php’ file, and add the following line:

    Object::add_extension('SiteConfig', 'MySiteConfig');

    This line simple tells SilverStripe to take the ‘SiteConfig’ (the basic settings that already exist) and add ‘MySiteConfig’ to it.

    Dev/Build

    Go to your site, log in, and add ‘/dev/build’ to the URL – eg ‘mysite.com/dev/build’ to refresh the database.

    Now, in the admin area, go to the site settings page and you should see your new options.

    Using these fields in the Template

    You can now add a logo, banner image, and enter the facebook / vimeo links – the data is stored in the database, but we need to show it on our page.

    We do this by adding ‘$SiteConfig.’ in front of the names of the fields. Eg:

    $SiteConfig.LogoImage.SetRatioSize(250,250)
    <a href="$SiteConfig.FacebookURL">Visit my facebook page</a>