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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, Ed, biapar, Willr, Ingo, swaiba

Translatable Data Objects


Go to End


7 Posts   1530 Views

Avatar
SamSmooth

Community Member, 18 Posts

29 November 2010 at 12:37am

Edited: 29/11/2010 12:54am

Hi.

I like to make data objects translatable just the same way, pages are translatable through the site-tree.

When I hit "Translate", the pages are put into the specified language site-tree, but attached objects e.g. images are not copied.

Below you find my approach so far. I'm using SS 2.4.3

Couldn't find a solution in the forums, yet. Perhaps you can help?


_config.php:
Object::add_extension('HeaderImage', 'Translatable');

CategoryPage:
class CategoryPage extends Page {
   
   static $has_many = array(
      'HeaderImages' => 'HeaderImage',
   );
    
   function getCMSFields() {

      $fields = parent::getCMSFields();      
      
	  $manager = new DataObjectManager(
         $this,
         'HeaderImages', // relation name
         'HeaderImage', // object class
         HeaderImage::$field_names, // fields to show in table
         HeaderImage::getCMSFields_forPopup(), // form that pops up for edit
         "CategoryPageID = {$this->ID}" // a filter to only display item associated with this page
      );

      $manager->setAddTitle('Header');
      $manager->setSingleTitle('Header Image');
      $manager->setPluralTitle('Header Images');
      $manager->setPopupWidth(400);
      $manager->setPerPageMap(array(20,40,80)); 
      
	  $fields->addFieldToTab('Root.Content.Header', $manager);
	}
}

HeaderImage.php:
class HeaderImage extends DataObject {
   
   static $has_one = array(
      'Image' => 'Image',
      'Category' => 'Category',
      'RootFolder' => 'Folder'
   );
   
   static $field_names = array(
      'Thumbnail' => 'Image',
      'Title' => 'Title',
   );
   
   function getCMSFields_forPopup() {
      
      $fields = new FieldSet();
      
	  $picfolderName = $this->RootFolder()->FileName;
	  $picfolderName = str_replace('assets/','',$picfolderName);
	  
	  $fields->push(new TextField('Title', 'Title'));
	  $fields->push(new TextField('Subtitle', 'Subtitle'));
	  $fields->push(new TextField('Text', 'Text'));
	  $fields->push(new DropdownField('Alignment','Alignment',singleton('HeaderImage')->dbObject('Alignment')->enumValues()));
	  $fields->push(new TextField('Link', 'Link'));
      $fields->push(new ImageField('Image', 'Image', null, null, null, $picfolderName));
   
      return $fields;
   }

	function getThumbnail()
	{
	    if ($Image = $this->Image())
	    {
	        return $Image->CMSThumbnail();
	    }
	    else
	    {
	        return '(No Image)';
	    }
	}

	public function onBeforeWrite() {
		parent::onBeforeWrite();
		$this->Locale = Translatable::get_current_locale();
	}

}

Avatar
UncleCheese

Forum Moderator, 4102 Posts

29 November 2010 at 6:52am

My understanding is that Translatable is an extension for SiteTree objects only.

---------------
Silverstripe tips, tutorials, screencasts, and more. http://www.leftandmain.com

Avatar
SamSmooth

Community Member, 18 Posts

29 November 2010 at 9:08am

Thank you, UncleCheese.

That exactly was my fear. Do you know any alternatives?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

29 November 2010 at 11:25am

How many languages? How many fields are you translating on the object?

Avatar
SamSmooth

Community Member, 18 Posts

29 November 2010 at 8:59pm

At the beginning the will be perhaps 10 languages,
and not more than 10 translations per object. E.g. For an Image Gallery

If possible both should be kept flexible for the admin.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

30 November 2010 at 3:34am

OK, never mind, then. :(

Avatar
swaiba

Forum Moderator, 1899 Posts

30 November 2010 at 4:18am