Jump to:

23003 Posts in 11856 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » How to turn a link id (data object) into an URL?

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

Page: 1
Go to End
Author Topic: 1197 Views
  • dacar
    Avatar
    Community Member
    168 Posts

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

    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 %>

  • Fuzz10
    Avatar
    Community Member
    786 Posts

    Re: How to turn a link id (data object) into an URL? Link to this post

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

  • dacar
    Avatar
    Community Member
    168 Posts

    Re: How to turn a link id (data object) into an URL? Link to this post

    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'));
       }

    ?>

  • Fuzz10
    Avatar
    Community Member
    786 Posts

    Re: How to turn a link id (data object) into an URL? Link to this post

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

    1197 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.