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.

Widgets

Discuss SilverStripe Widgets.

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

Change Widget Title


Reply

2 Posts   1730 Views

Avatar
kheeteck

24 June 2009 at 7:37pm Community Member, 7 Posts

Hi

I am writing my own custom links widget that can display a list of links

However, under this group of html links, i wish to put a Widget Title on top.

By default, i need to set a static value to $title.

But i would like user to define this title at run time - configure through the widget, just like they input their links.

How can i do that?

My code --

class LinkWidget extends Widget {
   static $db = array(
      "Links_23" => "Varchar",
      "Name1" => "Varchar",
      "Link1" => "Varchar",
      "Name2" => "Varchar",
      "Link2" => "Varchar",
      "Name3" => "Varchar",
      "Link3" => "Varchar",
      "Name4" => "Varchar",
      "Link4" => "Varchar",
      "LinkTitle" => "Varchar"
      );
   
   
   static $title = "Top Links";
   static $cmsTitle = "Links";
   static $description = "Puts links into the side bar for you blog readers to access. Please remember to put http:// before links unless they are internal links.";

function output() {
$linkTitle = $this->LinkTitle;

    $link1 = $this->Link1;
    $test1 = $this->Link1;
   
    $name1 = $this->Name1;
    $link2 = $this->Link2;
   
    $test2 = $this->Link2;
    $name2 = $this->Name2;
   
    $link3 = $this->Link3;
   
    $test3 = $this->Link3;
   
    $name3 = $this->Name3;
    $link4 = $this->Link4;
   
   
    $test4 = $this->Link4;
    $name4 = $this->Name4;
   
   if(empty($linkTitle)){
   
   }else{
    $title = $linkTitle;
   }
   
   if (empty($test1)) {
   $output_1 = "";
   } else {
$output_1 = "<li><a href='$link1'>$name1</a><br></li>";
   }
   
   if (empty($test2)){
   $output_2 = "";
   }else{
$output_2 = "<li> <a href='$link2'>$name2</a><br></li>";
   }

   if (empty($test3)) {
   $output_3 = "";
   } else {
$output_3 = "<li><a href='$link3'>$name3</a><br></li>";
   }
   
   if (empty($test4)){
   $output_4 = "";
   }else{
$output_4 = "<li> <a href='$link4'>$name4</a><br></li>";
   }
   
   return "$output_1 $output_2 $output_3 $output_4";
   
      }
      
      function getCMSFields() {
       $links_choose = $this->Links_23;

       return new FieldSet(
       new TextField("LinkTitle", "Title "),
         new TextField("Name1", "Name "),
         new TextField("Link1", "Link "),
         new TextField("Name2", "Name "),
         new TextField("Link2", "Link "),
       new TextField("Name3", "Name "),
         new TextField("Link3", "Link "),
       new TextField("Name4", "Name "),
         new TextField("Link4", "Link "));
}

}

Thanks

Avatar
ajshort

24 June 2009 at 7:52pm Community Member, 244 Posts

You can create a Title() method on your Widget class that overloads the default Title method - which just returns the static title.

public function Title() {
   return $this->LinkTitle ? $this->LinkTitle : 'Default Title';
}