Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » ManyMany Complex Table Field -- Setting Default Checked Checkboxes

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: 2887 Views
  • Daminipo
    Avatar
    Community Member
    14 Posts

    ManyMany Complex Table Field -- Setting Default Checked Checkboxes Link to this post

    Hello!

    If got a problem with the ManyManyComplexTableField.
    I'm trying to find out how to make certain Entries constantly checked.

    I have a Pagetype "Faq" that is always Child of "ThemeHolder".
    "Faq" represents an Question-Answer pair that was made.
    "ThemeHolder" represents the Themes that the Faq entry can belong to.
    In the ManyMany ComplexTableField theres a List of all "ThemeHolders" and you can correlate "Faq" with multiple "ThemeHolders".

    Now I want that the "ThemeHolder" that is the parent to "Faq" is always checked and is not to change by the CMS user.
    All the other ones can be checked or unchecked as normal but this one correlation has to be always there, otherwise it makes no sense.
    Cause I want to make it possible that a Faq entry is able to correspond to many Themes, but the Theme it is Child of should always be checked and unable to get unchecked.

    Is there anyone who might can help me with this Problem?
    I would be very thankful!

  • Daminipo
    Avatar
    Community Member
    14 Posts

    Re: ManyMany Complex Table Field -- Setting Default Checked Checkboxes Link to this post

    Well... finally I got the answer to my Problem. In any case someone is interessted what I've done let you know here:

    class Faq extends Page
    {
    ...
       static $many_many = array(
          'Themes' => 'ThemeHolder' //Relations name is "Themes"
       );
    protected $myThemeArray = array();
       protected $myString = "";
    ...
    //WHATCH OUT: The function onAfterWrite() is only useable when you use the Daily builds, it's not included in the Version 2.2.2
    function onAfterWrite()
       {
          $myID = $this->ParentID;
          $this->getMyParents($myID);
          $myThemes = $this->Themes(); //be shure to use the Name here you gave your many_many relation before
          $myThemes->addMany($this->myThemeArray);
          
          parent::onAfterWrite();
       }
       
       function getMyParents($myID)
       {
          if( DataObject::get_by_id('SiteTree', $myID))
          {
             $myObj = DataObject::get_by_id('SiteTree', $myID);
             
             if( $myObj->ClassName != 'FaqHolder' )
             {
                $myThemeID = $myObj->ID;
                $this->myThemeArray[] = $myThemeID;
                foreach($this->myThemeArray as $PID)
                {
                   $this->myString .= $PID;
                }
                $myParentID = $myObj->ParentID;

                $this->getMyParents($myParentID);
             }
          }
       }
    ...
    }

    As you hoefully have noticed is that the Function onAfterWrite() is beeing used. This is only availeble when you use the Daily builds. I don't know since when it isicluded, just use the newest one.
    ;-)

    -->http://dailybuilds.silverstripe.com/core-tarballs/

    The problem was that with the function onBeforeWrite() you are unable to use $this->ID or $this->ParentID, because the actual entry hasn't been made yet, as you are Before the writing progress...

    That way I have all my Themes Selected by default, even when someone deselects them, they will be there the next time he visits the Many_Many Table.

    If anyone is interested in the FAQ Module I'm creating just let me know.

    Cheers,
    Daminipo

  • Daminipo
    Avatar
    Community Member
    14 Posts

    Re: ManyMany Complex Table Field -- Setting Default Checked Checkboxes Link to this post

    Hi RNHurt!

    Sorry that I took so long to answer your question!
    Well, I think what you have in mind is something completely different than my FAQ.
    I have actually build something very untypical, for that my FAQ looks more like an Forum.
    You can make up themes and Subthemes and make your FAQ posts there.
    The main idea was to make an FAQ that we in our development Team can make entries to specific themes, so if another one has the same issue he can look it up fast.
    An FAQ like you see it on most of the Pages with a list of about 10 questions and beneath all the Question/Answer pairs, with the back to top links etc. isn't what I had to develop.

    But if you have some more infos in what your problem is, I can try to help you.

    For now I would suggest to make one Page that "holds" all the FAQ entries.
    So you can easly list them on top and afterward make a clear Template for them.
    For example:

    MyFAQ
    ---Question1
    ---Question2
    ---Question3
    ---...etc

    So that MyFAQ is the Parent.
    Now you can use the MyFAQ.ss to <% control Children %> and firstly give out a list of Questions with the link to them.
    and afterwards another control to give out the complete Question/Answer pair.
    So you could work with anchors in your templates that makes it easy to jump around in the site.

    Could I help you with this?

    Best wishes,
    Daminipo

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