Jump to:

5514 Posts in 1733 Topics by 1219 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Can I put a TreeMultiselectField into a ComplexTableField

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

Page: 1 2
Go to End
Author Topic: 1815 Views
  • LinseyM
    Avatar
    Community Member
    99 Posts

    Can I put a TreeMultiselectField into a ComplexTableField Link to this post

    Hi there,

    Looking for some advice.

    I want to create a new DataObject called "RelatedContent"

    This will take the form of a new tab in the CMS, available from any page, that allows the Admin to add links to related content (pages) within the site.

    E.G. If they were editing a page about "Document Translation into Urdu" then they might want to link to Case Study pages that relate to this.

    I had planned to create a simple data object along the lines of:

    <?php
    class RelatedContent extends DataObject {
       static $db = array(
          'DisplayTitle' => 'Text',
          'PageURL' => '???',
       );

       static $has_one = array(
       );
       
       static $default_sort = "DisplayTitle ASC";
    }
    ?>

    This would then be invoked in the CMS with:

    class Page extends SiteTree {
    ....
       static $has_many = array(
          'RelatedContents' => 'RelatedContent'
       );
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          $RelatedContentsTable = new ComplexTableField($this, 'RelatedContents', 'RelatedContent', array('DisplayTitle'=> 'Show.Link.As', 'PageURL'=> 'Links.To.Page'));
          $fields->addFieldToTab('Root.Content.RelatedContent', $RelatedContentsTable);
          return $fields;
       }
       
    }

    However, I want the PageURL component to be a dropdown in the CMS with links to all the pages in the site tree (and not a textbox, so that the clients isnt copy/pasting links)... basically just mimicing thebehaviour when you add a link from the WYSIWYG editor. I know that there is this "TreeMultiselectField" but I can't work out how to use it to achieve this.

    Any help appreciated, thank you

  • Juanitou
    Avatar
    Community Member
    323 Posts

    Re: Can I put a TreeMultiselectField into a ComplexTableField Link to this post

    Hi Linsey!

    EDIT: It’s not a DataObject you need, but a pointer to SiteTree, I think, so a relation between Pages…

    I’m not sure it’s the best way to do it, or even if it works, but try on the following basis:

    Page.php:

    class Page extends SiteTree {
       // Pages can have many related content
       public static $many_many = array(
          'RelatedContent' => 'RelatedContent'
       );

       function getCMSFields() {
          $fields = parent::getCMSFields();

          /* Add Related Content tab
          ** It's a ManyManyComplexTableField
          */
          $rcTablefield = new ManyManyComplexTableField(
             $this,
             'RelatedContent',
             'RelatedContent',
             array(
                'Title' => 'DisplayTitle'
             ));
          $fields->addFieldToTab('Root.Content.RelatedContent', $rcTablefield );
       }
    }

    RelatedContent.php:

    class RelatedContent extends SiteTree {
       static $db = array(
          'DisplayTitle' => 'Text',
       );

       static $belongs_many_many = array(
          'Pages' => 'Page'
       );
    }

    I’m quite sure that it will break around the ManyManyComplexTableField constructor, but you should debug it quite easily.

    Hope it helps,
    Juan

  • Juanitou
    Avatar
    Community Member
    323 Posts

    Re: Can I put a TreeMultiselectField into a ComplexTableField Link to this post

    I think I could be misleading you, I’m sorry, I did not read you were looking for a TreeMultiselectField. Maybe you could simply make a has-many "RelatedContent" => "SiteTree", without DataObject. Have you tried it?

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Can I put a TreeMultiselectField into a ComplexTableField Link to this post

    As a general note: Any has_many relationship needs a has_one on the other end. So LinseyM your orignal code snippet would need ..

    class RelatedContent extends DataObject {
    static $db = array(
    'DisplayTitle' => 'Text'
    );

    static $has_one = array(
    'Page' => 'SiteTree'
    );

    ...

    But that does raise a point like Juanitou touched on - do you want a has_many (so related content can only be related to one page) or can can related content attach to multiple pages. I think tree multiselectfield should handle many_many relations but not 100% sure.

  • LinseyM
    Avatar
    Community Member
    99 Posts

    Re: Can I put a TreeMultiselectField into a ComplexTableField Link to this post

    Hi there,

    Thanks for the feedback so far...

    Will:
    OK, let me think... I reckon I want a many/many relationship, so say there were 6 case studies in total... some might be referenced by more than one page, some might only be linked to once, and some not at all... just as a really simple example, to make sure I am explaining it correctly:

    e.g.

    - Interpreting Page has links to related content pages:
    Case Study 1
    Case Study 3
    Case Study 5

    - Translation Page has links to related content pages:
    Case study 3

    - Copywriting Page has link to related content pages:
    NONE

    - Typesetting Page has link to related content pages:
    Case Study 1
    Case Study 5
    Case Study 6

    Juanitou:
    I am only assuming that its a treeMultiselect that I need - there might be a better option that I don't know about!

  • LinseyM
    Avatar
    Community Member
    99 Posts

    Re: Can I put a TreeMultiselectField into a ComplexTableField Link to this post

    Should have added - reason i wanted to use data objects and not just do virtual / redirect pages in a 'submenu' is that in the future I might want to expand this to link to external websites, pdf files etc... (and if there are a lot of related items on a lot of pages, then the site tree could get far too big and unmanageable). So, bacl to the main point, where the link is an internal site link, I'd like them to be able to select the page (case study) from the site tree dropdown menu, as they are used to doing that when creating links in the wysiwyg editor.

    Many thanks!

    Linsey

  • LinseyM
    Avatar
    Community Member
    99 Posts

    Re: Can I put a TreeMultiselectField into a ComplexTableField Link to this post

    Hi there,

    Can you offer any further help on how I can get the site tree into a dropdown format on my complextablefield in the CMS?

    Been messing about with it so much that I've just tied myself in knots! It's a many/many, but it doesn't seem to work - just get errors...

    Thanks

  • Juanitou
    Avatar
    Community Member
    323 Posts

    Re: Can I put a TreeMultiselectField into a ComplexTableField Link to this post

    Hi Linsey!

    I’m not going to be very helpful here. I’ve been testing with the code proposed in the following thread: http://www.silverstripe.org/data-model-questions/show/256148

    With the code proposed by Mo, you cannot make each SiteTree page to belong to another one, I don’t know why. Maybe the last post could help you.

    If you want to relate, for example, Case Studies to other pages, you could use:

    Page.php

    class Page extends SiteTree {
       
       public static $db = array(
       );
       
       public static $many_many = array(
          'RelatedContent' => 'CaseStudyPage'
       );
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          $rcTablefield = new ManyManyComplexTableField(
             $this,
             'RelatedContent',
             'CaseStudyPage',
             array(
                'Title' => 'CaseStudyPage'
             ));
          // Remove the Add link, you can add new Case Studies as CaseStudyPages
          $rcTablefield->setPermissions(array('show'));
          $fields->addFieldToTab('Root.Content.RelatedContent', $rcTablefield );
          return $fields;
       }
    }

    CaseStudyPage

    class CaseStudyPage extends Page {
       
       public static $db = array(
       );
       
       public static $belongs_many_many = array(
          'BelongingPages' => 'Page'
       );
       
    }

    class CaseStudyPage_Controller extends Page_Controller {
    }

    There’s something ugly about this, but it works.

    Best regards,
    Juan

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