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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

How to turn a link id (data object) into an URL?


Reply


4 Posts   1339 Views

Avatar
dacar

Community Member, 168 Posts

14 August 2009 at 2:10am

Hi, i am trying to get the .Link from a data object

How can i pass the link (.Link) from $verlinkung to my template?

my code from page.php:

[...]

static $has_one = array(
      'MoodImage' => 'Page_MoodImage',
      'BannerImage' => 'Page_BannerImage',
      'BannerLinkIntern' => 'SiteTree'
   );

[...]

$fields->addFieldToTab('Root.Content.Banner', new TreeDropdownField('BannerLinkInternID', 'Bitte eine Seite waehlen', 'SiteTree'));

[...]

// Funktion zum rekursiven Auslesen der Banner
   function getBannerImageRecursive() {
      $page = $this;
      $BannerImage = $this->BannerImage();
      $headline = $this->Headline;
      $copytext = $this->Copytext;
      $BannerLinkIntern = $this->BannerLinkInternID;
      $BannerLinkExtern = $this->BannerLinkExtern;
      $BannerEinblenden = $this->BannerEinblenden;
      while($BannerEinblenden != 1 && $page->ParentID != 0) {
         $page = $page->Parent();
         $BannerImage = $page->BannerImage();
         $headline = $page->Headline;
         $copytext = $page->Copytext;
         $BannerLinkIntern = $page->BannerLinkInternID;
         $BannerLinkExtern = $page->BannerLinkExtern;
      }
      
      if(isset($BannerLinkExtern) && ($BannerLinkExtern != '')) {
         $verlinkung = $BannerLinkExtern;
         $blank = 1;
      } else {
         $verlinkung = $BannerLinkIntern;
         $blank = 0;
      }
      
      return new ArrayData(array(
         "bild" => $BannerImage,
         "headline" => $headline,
         "copytext" => $copytext,
         "verlinkung" => $verlinkung,
         "blank" => $blank
         )
      );
   }

sidebar.ss:

<% control getBannerImageRecursive %>
<div id="banner">
<div class="banner_top">&nbsp;</div>
<div class="banner_inner">
<h1>$headline</h1>
<p>$copytext
<br />// $BannerLinkIntern.Link // $blank<br />
<a href="$verlinkung" title="Lesen Sie mehr in $verlinkung.MenuTitle"><span>&gt; mehr</span></a>
</p>
<p>
<a href="$verlinkung" title="Lesen Sie mehr in $verlinkung.MenuTitle">
$bild.FullWidth
</a>
</p>
</div>
<div class="banner_end">&nbsp;</div>
</div>
<% end_control %>

Avatar
Fuzz10

Community Member, 787 Posts

14 August 2009 at 3:03am

$bla = DataObject::get_by_id("Page",$this->BannerLinkIntern)
return $bla->URLSegment;

Avatar
dacar

Community Member, 168 Posts

14 August 2009 at 9:29am

Thanks Fuzz10!!!

After some more modifications i now have a nice banner and mood picture administration. My first footsteps on Silverstripe....

<?php

class Page extends SiteTree {
   static $db = array(
      'Headline'          => 'Text',
      'Copytext'          => 'HTMLText',
      'BannerLinkExtern'       => 'Text',
      'BannerLinkInternID'    => 'Text',
      'BannerEinblenden'       => 'Boolean',
      
   );
   
   static $has_one = array(
      'MoodImage' => 'Page_MoodImage',
      'BannerImage' => 'Page_BannerImage',
      'BannerLinkIntern' => 'SiteTree'
   );
   
   function getCMSFields() {
      $fields = parent::getCMSFields();
      // Kopfbild
      $fields->addFieldToTab("Root.Content.Kopfbilder", new ImageField("MoodImage"));
      // Banner
      $fields->addFieldToTab('Root.Content.Banner', new ImageField('BannerImage'));
      $fields->addFieldToTab('Root.Content.Banner', new TextField('Headline'));
      $fields->addFieldToTab('Root.Content.Banner', new TextareaField('Copytext'));
      $fields->addFieldToTab('Root.Content.Banner', new TreeDropdownField('BannerLinkInternID', 'Bitte eine Seite waehlen', 'SiteTree'));
      $fields->addFieldToTab('Root.Content.Banner', new TextField('BannerLinkExtern'));
      $fields->addFieldToTab('Root.Content.Banner', new CheckboxField('BannerEinblenden'));

      return $fields;
   }
}

class Page_Controller extends ContentController {

   // Funktion zum Auslesen der aktuellen ID
   function Banner(){
      $page = $this;
      return DataObject::get(
         "Page",
         "`Page`.ID = $page->ID",
         null,
         null,
         null
      );
   }
   
   // Funktion zum rekursiven Auslesen der Banner
   function getBannerImageRecursive() {
      $page = $this;
      $BannerImage = $this->BannerImage();
      $headline = $this->Headline;
      $copytext = $this->Copytext;
      $BannerLinkIntern = $this->BannerLinkInternID;
      $BannerLinkExtern = $this->BannerLinkExtern;
      $BannerEinblenden = $this->BannerEinblenden;
      while($BannerEinblenden != 1 && $page->ParentID != 0) {
         $page = $page->Parent();
         $BannerImage = $page->BannerImage();
         $headline = $page->Headline;
         $copytext = $page->Copytext;
         $BannerLinkIntern = $page->BannerLinkInternID;
         $BannerLinkExtern = $page->BannerLinkExtern;
      }
      
      if(isset($BannerLinkExtern) && ($BannerLinkExtern != '')) {
         $verlinkung = $BannerLinkExtern;
         $verlinkungTitle = "auf ".$verlinkung;
         $blank = 1;
      } else {
         // $verlinkung = $BannerLinkIntern;
         $verlinkung = DataObject::get_by_id("Page",$page->BannerLinkInternID);
         $verlinkung = $verlinkung->URLSegment;
         $verlinkungTitle = "&uuml;ber ".$headline;
         $blank = 0;
      }
      
      return new ArrayData(array(
         "bild" => $BannerImage,
         "headline" => $headline,
         "copytext" => $copytext,
         "verlinkung" => $verlinkung,
         "blank" => $blank,
         "verlinkungTitle" => $verlinkungTitle
         )
      );
   }
   
   function init() {
      parent::init();
      // Note: you should use SS template require tags inside your templates
      // instead of putting Requirements calls here. However these are
      // included so that our older themes still work
      Requirements::themedCSS("layout");
      Requirements::themedCSS("typography");
      Requirements::themedCSS("form");

   }
}

class Page_MoodImage extends Image {
   function generatePageMood($gd) {
      return $gd -> croppedResize(830,200);
   }
}
   
class Page_BannerImage extends Image {
   function generateFullWidth($gd) {
      return $gd -> croppedResize(180,120);
   }
}
   
function SearchForm() {
      $searchText = isset($_REQUEST['Search']) ? $_REQUEST['Search'] : 'Search';
      $fields = new FieldSet(
       new TextField("Search", "", $searchText)
       );
      $actions = new FieldSet(
       new FormAction('results', 'Search')
       );

       return new SearchForm($this, "SearchForm", $fields, $actions);
   }
   
    /**
    * Process and render search results
    */
   function results($data, $form){
       $data = array(
       'Results' => $form->getResults(),
       'Query' => $form->getSearchQuery(),
       'Title' => 'Search Results'
       );

       return $this->customise($data)->renderWith(array('Page_results', 'Page'));
   }

?>

Avatar
Fuzz10

Community Member, 787 Posts

14 August 2009 at 6:33pm

Edited: 14/08/2009 6:33pm

Glad to help .. And welcome to the SilverStripe club ... ;-)