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.

DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Translatable Data Objects


Reply

7 Posts   901 Views

Avatar
SamSmooth

29 November 2010 at 12:37am (Last edited: 29 November 2010 12:54am), Community Member, 18 Posts

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

29 November 2010 at 6:52am 4085 Posts

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

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

Avatar
SamSmooth

29 November 2010 at 9:08am Community Member, 18 Posts

Thank you, UncleCheese.

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

Avatar
UncleCheese

29 November 2010 at 11:25am 4085 Posts

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

Avatar
SamSmooth

29 November 2010 at 8:59pm Community Member, 18 Posts

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

30 November 2010 at 3:34am 4085 Posts

OK, never mind, then. :(

Avatar
swaiba

30 November 2010 at 4:18am Forum Moderator, 1796 Posts

I'll be taking this further forward soon...

http://www.silverstripe.org/data-model-questions/show/8397#post293889#post293889