tx_t3blog_pi1 Kaktusteam: Mimi's Blog

Mimis Blog

25.12.2009
13:14

ExtBase Cookbook 6 - How to generate RSS Feeds with ExtBase

As I wanted to add Cooliris functionality to my gallery extension, I had to create a RSS-Feed. As this has not yet been done within the Blog-Extension, I wanted to show how I implemented this.

 

First of all, we create a new page type inside the 'Configuration/TypoScript/setup.txt' file:

 

 

  1. xml = PAGE
  2. xml {
  3. typeNum = 100
  4. 10 = USER
  5. 10 {
  6. userFunc = tx_extbase_dispatcher->dispatch
  7. pluginName = Pi1
  8. extensionName = Yag
  9. controller = Album
  10. switchableControllerActions {
  11. 1 {
  12. controller = Album
  13. actions = rss
  14. }
  15. }
  16. action = rss
  17. settings =< plugin.tx_yag.settings
  18. persistence =< plugin.tx_yag.persistence
  19. view =< plugin.tx_yag.view
  20. }
  21.  
  22. config {
  23. disableAllHeaderCode = 1
  24. additionalHeaders = Content-type:application/xml
  25. xhtml_cleaning = 0
  26. admPanel = 0
  27. }
  28. }

 

 

There is one other important line to add to your setup.txt in order to make persistence work. Without this line, I got an "Expected parameter 1 to be object, NULL given" exception:

 

 

  1. plugin.tx_yag {
  2. settings {
  3.  
  4. }
  5. view {
  6. templateRootPath = EXT:yag/Resources/Private/Templates/
  7. partialRootPath = EXT:yag/Resources/Private/Partials/
  8. layoutRootPath = EXT:yag/Resources/Private/Layouts/
  9. }
  10. persistence {
  11. storagePid = 6
  12. }
  13. }

 

 

We generate a new page type 100, which is responsible for showing our RSS XML. We use a USER object to register an ExtBase dispatcher that should handle the RSS Request. The next lines are taken from the 'Tx_Extbase_Utility_Extension' class, which is normally used to register ExtBase based extions as page contents. The last lines prevents rendering of headers by typo3.

 

I'm not sure, whether the next step is required, but I registered the RSS action for the controller inside 'ext_localconf.php':

 

 

  1. Tx_Extbase_Utility_Extension::configurePlugin(
  2. $_EXTKEY,
  3. 'Pi1',
  4. #...
  5. 'Album' => 'rss',
  6. #...
  7. ),
  8. 'Album' => 'rss'
  9. )
  10. );

 

 

Let's know create an action method inside the Album controller:

 

 

  1. /**
  2.  * Rss Feed Action rendering a RSS Feed of media
  3.  *
  4.  * @param Tx_Yag_Domain_Model_Album $album Album to generate rss feed for
  5.  * @return string The rendered RSS Feed
  6.  */
  7. public function rssAction(Tx_Yag_Domain_Model_Album $album = null) {
  8. if ($album != null) {
  9. $this->view->assign('album', $album);
  10. return $this->view->render();
  11. } else {
  12. return "Kein Album --> kein RSS!";
  13. }
  14. }

 

 

Last but not least, we need a template for rendering the feed. Here's what I wrote for Cooliris plugin ('Resources/Private/Templates/Album/rss.html'):

 

 

  1. <?xml version="1.0" encoding="utf-8" standalone="yes"?>
  2. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/"
  3. xmlns:atom="http://www.w3.org/2005/Atom">
  4. <channel>
  5. <f:for each="{album.images}" as="image"><item>
  6. <title>{image.title}</title>
  7. <media:description>{image.description}</media:description>
  8. <link>{image.single.filePath}</link>
  9. <media:thumbnail url="{image.thumb.filePath}"/>
  10. <media:content url="{image.single.filePath}"/>
  11. </item>
  12. </f:for></channel>
  13. </rss>

 

 

Here it is very important, that you spare the newline behind the '<f:section...>' tag, otherwise, the generated XML is not parsed by the browser!

25.12.2009
04:59

YAG unterstützt jetzt Cooliris

Bis jetzt stand ja die Darstellung der Photos in meiner Gallery immer ganz oben auf der TODO-Liste. Dann bin ich heute durch Zufall wieder auf das Cooliris Plugin gestoßen und habe mich dann dran gemacht, den benötigten RSS-Feed mit in meine Gallery aufzunehmen.

 

Es war nicht ganz leicht, ExtBase einen RSS nach meinen Wünschen zu entlocken, schließlich habe ich es aber hingekriegt und es folgt morgen ein Cookbook-Eintrag dazu!

 

Hier ein erster Screenshot der Darstellung in Cooliris:

 

 

Cooliris Darstellung der YAG Gallery Bilder
24.12.2009
19:01

Fröhliche Weihnachten

Bevor's im väterlichen Gehäus' jetzt gleich so richtig weihnachtlich zur Sache geht und Pink Floyd der festlichen Musik weichen muss, möchte ich die Gelegenheit noch nutzen und euch allen fröhliche Weihnachten und besinnliche Feiertage zu wünschen. Zu diesem Anlass erschien mir das folgende Bild irgendwie passend :-)

 

 

22.12.2009
16:41

ExtBase Cookbook 5 - How to make new domain objects persistent

Today I faced the problem, that I could not access a new-created object after redirect. Regard the following two actions inside a controller:

 

 

  1. /**
  2.  * Shows all images of a album
  3.  *
  4.  * @param Tx_Yag_Domain_Model_Album $album Album object to show images from
  5.  * @param Tx_Yag_Domain_Model_Gallyer $gallery Gallery that holds album
  6.  * @return string The rendered index action
  7.  */
  8. public function indexAction(Tx_Yag_Domain_Model_Album $album=NULL,
  9. Tx_Yag_Domain_Model_Gallery $gallery=NULL) {
  10. $this->view->assign('album', $album);
  11. $this->view->assign('gallery', $gallery);
  12. }
  13.  
  14.  
  15.  
  16. /**
  17.  * Adds a new album to repository
  18.  *
  19.  * @param Tx_Yag_Domain_Model_Album $newAlbum New album to add
  20.  * @return string The rendered create action
  21.  */
  22. public function createAction(Tx_Yag_Domain_Model_Album $newAlbum,
  23. Tx_Yag_Domain_Model_Gallery $gallery = NULL) {
  24. $this->albumRepository->add($newAlbum);
  25. if ($gallery != NULL) {
  26. $gallery->addAlbum($newAlbum);
  27. }
  28. $this->flashMessages->add('Your new album was created.');
  29. $this->redirect('index','Album', NULL, array('album' => $newAlbum, 'gallery' => $gallery));
  30. }

 

 

As I had to find out, this doesn't work. When redirecting from the "createAction" to the "indexAction" I always got an empty object for "album". So the trick is, to make this object persistent right after you created it from a form input:

 

 

 

 

  1. $persistenceManager = t3lib_div::makeInstance('Tx_Extbase_Persistence_Manager');
  2. /* @var $persistenceManager Tx_Extbase_Persistence_Manager */
  3. $persistenceManager->persistAll();

 

 

So putting it together, the second method should look like that in order to work. Thanks to Robert Böttner for helping me out with that on the ExtBase Mailinglist!

 

 

  1. /**
  2.  * Adds a new album to repository
  3.  *
  4.  * @param Tx_Yag_Domain_Model_Album $newAlbum New album to add
  5.  * @return string The rendered create action
  6.  */
  7. public function createAction(Tx_Yag_Domain_Model_Album $newAlbum,
  8. Tx_Yag_Domain_Model_Gallery $gallery = NULL) {
  9. $this->albumRepository->add($newAlbum);
  10. if ($gallery != NULL) {
  11. $gallery->addAlbum($newAlbum);
  12. }
  13. $this->flashMessages->add('Your new album was created.');
  14. $persistenceManager = t3lib_div::makeInstance('Tx_Extbase_Persistence_Manager');
  15. /* @var $persistenceManager Tx_Extbase_Persistence_Manager */
  16. $persistenceManager->persistAll();
  17. $this->redirect('index','Album', NULL, array('album' => $newAlbum, 'gallery' => $gallery));
  18. }
21.12.2009
17:44

ExtBase Cookbook 4 - Accessing arguments from requests

The blog-example shows multiple ways to pass objects to controller actions from forms and links. It does not show how to access "normal" form parameters from within a controller's action, so here is a little tutorial on how to do that.

 

In your form you might have something like that:

 

 

  1. <f:form method="post" controller="AlbumContent" action="addImages"
  2. name="addImages" arguments="{gallery : gallery, album : album}">
  3. <label for="path">Path (from fileadmin/) <span class="required">(required)</span></label><br />
  4. <f:form.textbox name="path" size="30" /><br />
  5. <p>If you already have created thumbnails and single images, you can chose the
  6. sub-directories here:</p>
  7. <label for="thumbsDir">Directory for thumbnails (relative to upper path):</label><br/>
  8. <f:form.textbox name="thumbsDir" size="20" /><br />
  9. <label for="singlesDir">Directory for singles (relative to upper path):</label><br/>
  10. <f:form.textbox name="singlesDir" size="20" /><br />
  11. <f:form.submit class="submit" value="Submit"/>
  12. </f:form>

 

 

so there is no magic in generating "normal" form elements. Btw you can see how to "forward" further objects to the controller's action via the "arguments" attribute of your form viewhelper. You can now access your parameters in your controller's action via the following lines of code:

 

 

  1. $path = $this->request->getArgument('path');

 

 

Everything is quite simple though you get an error, if a requested parameter is not available in the current request. So it's better to write code like that:

  1. public function addImagesAction(
  2. Tx_Yag_Domain_Model_Gallery $gallery=NULL,
  3. Tx_Yag_Domain_Model_Album $newAlbum=NULL) {
  4.  
  5. $path = $thumbsPath = $singlesPath = '';
  6.  
  7. if ($this->request->hasArgument('basePpath')) {
  8. $basePath = $this->request->getArgument('path');
  9. }
  10. if ($this->request->hasArgument('thumbsPath')) {
  11. $thumbsPath = $this->request->getArgument('thumbsPath');
  12. }
  13. if ($this->request->hasArgument('singlesPath')) {
  14. $singlesPath = $this->request->getArgument('singlesPath');
  15. }
  16.  
  17. $this->view->assign('gallery', $gallery);
  18. $this->view->assign('newAlbum', $newAlbum);
  19. }

 

 

to make things more simple, I decided to add a function to my controller that handles parameter access more simple:

 

 

  1. protected function getParametersSafely($parameterName) {
  2. if ($this->request->hasArgument($parameterName)) {
  3. return $this->request->getArgument($parameterName)
  4. }
  5. return NULL
  6. }
< < September 2010 > >
S M T W T F S
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    

Kategorien

  • [-](J)DAV(28)
  • [-]Bergsteigen(30)
  • [-]Computer & Informatik(63)
    • PHP(2)
    • [-]Typo3(54)
  • [-]Fotografie(47)
  • Kaktusteam(3)
  • [-]Laufen(4)
  • Musik & Filme(3)
  • [-]Radfahren(0)
  • [-]Reisen(11)
  • Sonstiges...(4)
  • Studium...(6)

Letzte Kommentare

Using Extbase Controller and Action in TemplaVoila TS Lib
26.08.2010 22:02
insert in TS
26.08.2010 11:27
Gut geschildert
23.08.2010 22:05
Good Timing
21.08.2010 01:59

Archiv

Kopieren Sie diesen Link in Ihren RSS Reader

RSS 0.91Posts
RSS 2.0Posts

Social Bookmarking

Bookmark bei: Mr. Wong Bookmark bei: Webnews Bookmark bei: Icio Bookmark bei: Oneview Bookmark bei: Linkarena Bookmark bei: Favoriten Bookmark bei: Seekxl Bookmark bei: Favit Bookmark bei: Social Bookmarking Tool Bookmark bei: Power Oldie Bookmark bei: Bookmarks.cc Bookmark bei: Newskick Bookmark bei: Newsider Bookmark bei: Linksilo Bookmark bei: Readster Bookmark bei: Folkd Bookmark bei: Yigg Bookmark bei: Digg Bookmark bei: Del.icio.us Bookmark bei: Reddit Bookmark bei: Simpy Bookmark bei: StumbleUpon Bookmark bei: Slashdot Bookmark bei: Netscape Bookmark bei: Furl Bookmark bei: Yahoo Bookmark bei: Spurl Bookmark bei: Google Bookmark bei: Blinklist Bookmark bei: Blogmarks Bookmark bei: Diigo Bookmark bei: Technorati Bookmark bei: Newsvine Bookmark bei: Blinkbits Bookmark bei: Ma.Gnolia Bookmark bei: Smarking Bookmark bei: Netvouz Information