fogbound.net




Tue, 16 Jun 2015

Localized images in Silverstripe with Fluent

— SjG @ 10:50 am

Say you’re building a web site using Silverstripe. And say you need to localize it, and you opted to use the fluent add-on. You have a nice normal page set up, along with a slick user-selected side image. But now, just to finish this hypothetical, say you not only need text on the page to be localized, but you need the image to be localizable too (e.g., a different image depending on someone’s country or language).

Here’s what I ended up doing, and what seems to work for Silverstripe 3.1.

My page model looks like this:


class Page extends SiteTree {
   private static $db = array();
   private static $has_one = array(
      'SideImage'=>'Image'
   );

   public function getCMSFields() {
      $this->beforeUpdateCMSFields(function($fields) {
         $fields->addFieldToTab('Root.Images', UploadField::create('SideImage','Image for Right Panel'));
      });
      return parent::getCMSFields();
    }

The page template has a simple image inclusion:


< % if $SideImage %>
   $SideImage.setWidth(310)
< % end_if %>

In my _config/config.php, I then added


Page:
  extensions:
    - 'FluentFilteredExtension'
  translate:
    - 'SideImageID'
    - 'SideImage'

Run http://www.yoursite.com/dev/build

Now your Page admin will have an Images tab where you can set the Side Image. If you set the Side Image while in the default locale, that image will show for all locales. But if you use the locale menu item to select a different locale, you can override the Side Image for the page.

Slick!


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.