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

Output DataObject to XML


Reply


5 Posts   1370 Views

Avatar
bunheng

Community Member, 78 Posts

13 June 2011 at 10:32pm

Hi,

I would like to know is there any possible way to output DataObject into XML format, I want to use with my flash slideshow, is there any tutorial or trick, I have to complete my project today.

Thanks
Bunheng

Avatar
UncleCheese

Forum Moderator, 4096 Posts

14 June 2011 at 2:08am

Sure.. you can just do:

return $yourObject->renderWith('YourXMLTemplate');

YourXMLTemplate.ss

<?xml ... .?>
<SomeField>$SomeField</SomeField>
<AnotherField>$AnotherField</AnotherField>
<RelatedStuff>
<% control MyHasManyRelation %>
<Something>$Something</Something>
<% end_control %>
</RelatedStuff>

etc..

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

Avatar
bunheng

Community Member, 78 Posts

14 June 2011 at 5:43pm

Hi UncleCheese,

First of all thanks for your kindness advice. Assuming with your comment I have DataObject call NewsItem with the following code:

--------------------------------------------
NewsItem.php
--------------------------------------------

class NewsItem extends DataObject {

static $db = array(
'Title' => 'Varchar(255)',
'Details' => 'HTMLText',
'Source' => 'Varchar(255)',
'Date' => 'Date'
);
static $has_one = array(
'NewsPage' => 'NewsPage',
'NewsPhoto' => 'Image'
);
static $summary_fields = array(
'Title' => 'Title',
'Source' => 'Source',
'Date' => 'Date'
);

public function getCMSFields() {
return new FieldSet(
new TextField('Title'),
new SimpleTinyMCEField('Details', 'News Details'),
new TextField('Source', 'News Source'),
new DatePickerField('Date', 'Release Date'),
new ImageField('NewsPhoto', 'Photo', Null, Null, Null, 'Uploads/latest-news/photos/')
);
}

function canView() {
return true;
}

//Return the Name as a menu title
public function MenuTitle() {
return $this->Title;
}

public function Link() {
if ($NewsPage = $this->NewsPage()) {
return $NewsPage->Link('show/') . $this->ID;
}
}

public function LinkingMode() {
if ($Controller = Controller::CurrentPage() && Controller::CurrentPage()->ClassName == 'NewsPage') {
if (Controller::CurrentPage()->getAction() == 'show' && $NewsItem = Controller::CurrentPage()->getNewsItem()) {
return ($NewsItem->ID == $this->ID) ? 'current' : 'link';
}
}
}

}

--------------------------------------------
NewsPage.php
--------------------------------------------
class NewsPage extends Page {

static $has_many = array(
'NewsItems' => 'NewsItem'
);

public function getCMSFields() {
$fields = parent::getCMSFields();
$manager = new DataObjectManager(
$this,
'NewsItems',
'NewsItem'
);
$fields->addFieldToTab("Root.Content.News", $manager);
return $fields;
}

}

class NewsPage_Controller extends Page_Controller {

static $allowed_actions = array(
'show','xml'
);

function show() {
if ($NewsItem = $this->getNewsItem()) {
$Data = array(
'NewsItem' => $NewsItem
);
return $this->Customise($Data);
} else {
return $this->httpError(404, 'Not found');
}
}

public function getNewsItem() {
$Params = $this->getURLParams();

if (is_numeric($Params['ID']) && $NewsItem = DataObject::get_by_id('NewsItem', $Params['ID'])) {
return $NewsItem;
}
}

public function Breadcrumbs() {

$Breadcrumbs = parent::Breadcrumbs();

if ($NewsItem = $this->getNewsItem()) {
$Parts = explode(SiteTree::$breadcrumbs_delimiter, $Breadcrumbs);

//Count the parts
$NumOfParts = count($Parts);
$Parts[$NumOfParts - 2] = ("<li><a href=\"" . $this->Link() . "\">" . $this->MenuTitle . "</a></li>");

$Parts[$NumOfParts] = ("<li class=\"current\">" . $NewsItem->Title . "</li>");
$Breadcrumbs = implode(SiteTree::$breadcrumbs_delimiter, $Parts);
}

return $Breadcrumbs;
}

public function init() {
parent::init();
Requirements::css('news/css/news.css');
}

}

---------------------------------------------------------------------------------
Could you please kindly just show me where I can place function to render XML within template file it is in PageController, or I have to create new page controller.

Best Regards,
Bunheng

Avatar
UncleCheese

Forum Moderator, 4096 Posts

15 June 2011 at 2:03am

You can really put it anywhere. You could put it in your NewsPage_Controller class, in which case you'd just have to pass the ID of the news item you want in the url.

/your-news-page/xml/123

public function xml(SS_HTTPRequest $r) {
if($news = DataObject::get_by_id("NewsItem", (int) $r->param('ID'))) {
return $news->renderWith('MyXMLTemplate');
}
return $this->httpError(404);
}

And of course, don't forget to update your $allowed_actions array in the controller.

To tidy things up, I'd probably add a new template accessor method to your NewsItem class, XMLLink(), for instance, that would generate its link to the NewsPage controller.

Good luck!

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

Avatar
bunheng

Community Member, 78 Posts

24 June 2011 at 10:35pm

Hi,

Now I can get my multimedia work with xml generate from DataObject.
Thanks UncleCheese for your comment and support.

Regards,
Bunheng