Jump to:

3445 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » access controller functions inside <% control AllChildren %>

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

Page: 1 2
Go to End
Author Topic: 5005 Views
  • BradPlunkett
    Avatar
    Community Member
    4 Posts

    Re: access controller functions inside <% control AllChildren %> Link to this post

    I was just about to come here and post the solution I came up with. While it's probably a little bit of a dirty hack, it seems to work just fine in this case. What I did was create a method inside the model that I can then access via a control. Here's my method:

       public function ContactForm(){
          $cf = new ContactPage_Controller;
          $cf->init();
          
          return $cf->ContactForm();
       }

    I put this inside the model (duplicate method name as the one inside the controller, so perhaps a bit confusing). To access this, I use the same means as I had originally been attempting with the controller method:

       <% control Page(contact) %>
          <% control ContactForm %>
             ...
          <% end_control %>
       <% end_control %>

    The form seems to work just fine, including any error handling and success pages.

  • moloko_man
    Avatar
    Community Member
    72 Posts

    Re: access controller functions inside <% control AllChildren %> Link to this post

    I have a similar issue where I'm pulling in AllChildren into a holder page. Basically my holder page is a ContactHolderPage and all of its sub pages are contact pages(ContactPage) and forms for different departments for "Company X". So each page needs to have a slightly different form on it and it's form is mailed to someone different.

    I used BradPlunkett's little hack and it pulls in the form, but its the same form for all the pages, which is expected as I only have have one ContactForm() function in ContactPage.php.

    Is there a way to have the ContactForm() function know what page (child page) its on and then add in some variable or name to make each form different?

    I've tried adding in $this->URLSegment to the form name, but when I add it to the allowed_actions array, it throws the following error: Parse error: syntax error, unexpected T_VARIABLE, expecting ')' in /my/web/location

    Any help here is appreciated. Thanks

  • purplespider
    Avatar
    Community Member
    81 Posts

    Re: access controller functions inside <% control AllChildren %> Link to this post

    I too am looking for help with this.

    My pages have a function that returns yes if the current user can view the page.

    It works great in the standard template, as if they don't have access I just display a message in place of the content.

    However I would like an indication of the pages a user doesn't have access to in the site menu. But because I cannot access the function unless I'm on that actual page, I'm not sure how to do this.

    Help appreciated.

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: access controller functions inside <% control AllChildren %> Link to this post

    Moloko_man try taking the Form off of the PageType and making new Form type by extending the form object

    http://doc.silverstripe.org/sapphire/en/topics/forms#using-a-custom-template

    Since the Form is called inside a different controller, in theory, it should return it with enclosing controller instead of the page controller.

    Purplespider what does your permission check look like? As long as its called from within that child block you should be able to pull the child data and do an if filter

    <% control AllChildren %>
    <% if CanView %><a href="$Link">$Title</a><% else %>$Title<% end_if %>
    <% end_if %>

  • purplespider
    Avatar
    Community Member
    81 Posts

    Re: access controller functions inside <% control AllChildren %> Link to this post

    That's not working for me. In my Page_Controller I have:

    function isLocked() {      
          if ($this->canView() == "yes") {
             return false;
          }
             return true;
       }

    If I put this into a template outside of a control, for example in the page title tag, it correctly returns Locked if the page is locked:

    <% if isLocked == true %>Locked<% else %>Unlocked<% end_if %>

    However, if I try to use it in a control, for example:

    <% control Menu(1) %>
    <a href="$Link">$MenuTitle <% if isLocked == true %>Locked<% else %>Unlocked<% end_if %></a>
    <% end_control %>

    It doesn't work, and just always displays "Unlocked".

    Any ideas?

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: access controller functions inside <% control AllChildren %> Link to this post

    Try putting the function in your Page class instead of in the Page_Controller...

  • purplespider
    Avatar
    Community Member
    81 Posts

    Re: access controller functions inside <% control AllChildren %> Link to this post

    Now that works thanks. But my function, relies on another function, that relies on another function. What's the difference between putting a function in the Controller or in the Model?

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: access controller functions inside <% control AllChildren %> Link to this post

    As I understand it: the Page_Controller controls the output (i.e. the template) for the current Page. That's why all Page_Controller functions for the current page are accessible from the template.

    However, within a <% control %> group, the scope switches to whatever DataObjectSet you're traversing. Menu(), Level(), Children() are all DataObjecSets, made up of 'ordinary' DataObjects, or Page objects in this case.

    And from within a Page Object you just cannot access Page_Controller methods. So you need to define your functions within the Page class. In some cases it might work to instantiate the Page_Controller from within a function in the Page class, and then access Page_Controller functions.

    To make things confusing: the other way around, it seems the Page_Controller can access Page class methods and properties...

    5005 Views
Page: 1 2
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.