Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » *SOLVED* - Can't seem to use Summary() and other data function on Virtual page properties

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

Page: 1
Go to End
Author Topic: 416 Views
  • Double-A-Ron
    Avatar
    Community Member
    599 Posts

    *SOLVED* - Can't seem to use Summary() and other data function on Virtual page properties Link to this post

    Version 2.4

    I have a virtual page derived from a normal page. This normal page has a text field. You could even take the default Content field on a page as an example.

    I then have a holder page to display a list of these pages and allow the user to click through to the page itself. This holder loops these using:

    <% control Children %>
    $Content.Summary(40).
    <% end_control %>

    In the template, I am truncating the text in Content using $Content.Summary(40).

    This works as expected where the child is a real data object. But when it is a virtual page, the output is blank. However in these cases if I just change the template to be $Content (without calling Summary), the text is output in full.

    I found that the above is true for any native function supported by the datatype (in this case, and HTMLField)

    Is this a framework limitation? Is there a workaround?

  • Double-A-Ron
    Avatar
    Community Member
    599 Posts

    Re: *SOLVED* - Can't seem to use Summary() and other data function on Virtual page properties Link to this post

    Found the solution to this, and it seems that this is a Framework limitation.

    I had to replace $Children with a custom method in the holder class file that detected if the current child was a virtual page, and load the real data object from the master content ID.

    MyHolder.php - MyHolderController Class

    public function realPages() {
          $children = $this->Children();
          $return = new DataObjectSet();
          foreach ($children as $child)
          {
             if($child->ClassName == 'VirtualPage') {
    // Current child is virtual, get the real page
                $virtualPage = DataObject::get_by_id('VirtualPage', $child->ID);
                $realPage = DataObject::get_by_id('Page', $virtualPage->CopyContentFromID);
             } else {
    // Current child is real, just use it as it is
                $realPage = $child;
             }
           $return->push($realPage);
          }
          return $return;
       }

    Then in the template change

    <% control Children %>

    To:

    <% control realPages %>

    The one thing with this is that the URL link and ID is that of the Virtual pages master, so I have to come up with some funky setters there.

  • Double-A-Ron
    Avatar
    Community Member
    599 Posts

    Re: *SOLVED* - Can't seem to use Summary() and other data function on Virtual page properties Link to this post

    This is turning into a bit of a nightmare actually. After doing the above, I cannot find a way to provide the loop with the virtual page's $Link and $ID. Obviously the new method is returning the object of the page the virtual one derives from, but there doesn't seem to be a way to return a mix of the virtual page properties and the real page's properties that actually support using the built in methods.

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