Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » TreeDropdownField in Widget

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 2024 Views
  • Anatol
    Avatar
    126 Posts

    TreeDropdownField in Widget Link to this post

    Hi all,

    I am trying to create a Widget that displays the content of a random sub page, i.e. in the widget I choose a parent page from the site tree, the frontend would then display the content of a random child page of that parent.

    I started as you can see below. The TreeDropdownField gets displayed, but when I try to select it I get the error message "Error Getting Data".

    RandomContentWidget.php

    <?php

    class RandomContentWidget extends Widget {
       static $has_one = array(
          "RandomContent" => "Page"
       );

       static $title = "Random Content";
       static $cmsTitle = "Random Content Widget";
       static $description = "Adds content from a random sub page to the widget sidebar.";

       // the random selection is still missing at this stage ...

       function getCMSFields() {
          return new FieldSet(
             new TreeDropdownField("Page", "Choose a random parent page:", "SiteTree")
          );
       }
    }

    ?>

    RandomContentWidget.ss

    <div id="RandomContentWidget">
    <% control RandomContent %>
    <h3>$Title</h3>
    $Content
    < end_control %>
    </div>

    I am not very experienced with creating Silverstripe widgets. I just wrote my very first one yesterday. If anyone knows why I get the "Error Getting Data" message I would be happy for a hint.

    Cheers!
    Anatol

  • Anatol
    Avatar
    126 Posts

    Re: TreeDropdownField in Widget Link to this post

    It seems the TreeDropDownField does not work properly in widgets yet: http://open.silverstripe.com/ticket/2162.

    I borrowed some code from simon_w who seemed to have similar problems with the Children Pages Widget. Thank you for that!

    This works:

    RandomContentWidget.php

    <?php

    class RandomContentWidget extends Widget {
       static $title = "Random Content";
       static $cmsTitle = "Random Content Widget";
       static $description = "Adds content from a random sub page to the widget sidebar.";
       
       static $db = array(
          "Page"=>"Varchar(255)"
          );
       static $defaults = array(
          "Page"=>"home"
          );

       function getCMSFields() {
          // TODO: Use TreeDropdownField when it's fixed
          return new FieldSet(
             new TextField("Page", _t('RandomContentWidget.PAGE', "The URLSegment of the parent page to display a random child of."))
          );
       }
       function RandomContent() {
          $parent = DataObject::get_one("SiteTree", "URLSegment = '". Convert::raw2sql($this->Page) . "'");
          if(!$parent) {
             return false;
          }
          return DataObject::get("SiteTree", "ParentID = $parent->ID", "RAND()", "", 1);
       }
    }

    ?>

    RandomContentWidget.ss

    <div id="RandomContentWidget">
       <% control RandomContent %>
          $Content
       <% end_control %>
    </div>

    I can upload the two widgets I created once they are cleaned up (the random content widget above and a widget to add html content with the TinyMCE editor).

    Cheers!
    Anatol

    2024 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.