Page DropDownField automatically defaulting to parent Page of object in Silverstripe

StackOverflow https://stackoverflow.com/questions/23644561

  •  22-07-2023
  •  | 
  •  

문제

I am wanting to make simple configurable "Navigation Blocks" in a Silverstripe site. These have text, image, and link to another Page in the site.

Here's my (simplified) code:

class NavBlock extends DataObject {

  private static $db = array(
      'Text' => 'Text'
  );
  private static $has_one = array(
      'NavBlockPhoto' => 'Image',
      'LinksTo' => 'Page'
  );

  public function getCMSFields() {

    $linksToField = new DropdownField('LinksToID', 'Page this block links to', Page::get()->map('ID', 'Title'));

    $fields->addFieldToTab('Root.Main', $linksToField);

    return $fields;
  }

}

Currently the HomePage page type has a $has_one relationship with NavBlock:

class HomePage extends Page {

  private static $has_many = array(
      'NavBlocks' => 'NavBlock'
  );

When I view a NavBlock in the CMS I get the following options: enter image description here

Where "Page this block links to | Home" is I'd expect to see a drop down menu, but it seems to have defaulted/ locked to "Home" which is the parent of the NavBlock object.

Creating a new NavBlock and checking the database strongly suggests this is the case - the PageID of "Home" is 1. enter image description here

How do I get it so I can select any page from the "LinksToID" dropdown?

도움이 되었습니까?

해결책

This was the addition that worked for me:

private static $has_one = array(
    'NavBlockPhoto' => 'Image',
    'ParentPage' => 'Page',
    'LinksTo' => 'SiteTree'
);

ParentPage automatically defaults to the HomePage as read-only.

LinksTo is then editable in the CMS.

다른 팁

What should be the second item in the dropdown? It is an expected behavior because you have one HomePage only and you are setting that it can have one home page. You can remove if you like by using remove

 public function getCMSFields() {
    $fields=parent::getCMSFields();
    $fields->removeByName('HomePageID'); 
}

It will still save it as HomePage behind the scenes. If you want to have many, then you should use something which is more than one and it will offer you a dropdown.

Shouldn't the code on HomePage read:

class HomePage extends Page {

    private static $has_one = array(
        'NavBlocks' => 'NavBlock'
    );

...meaning HomePage has one block, with each block comprising one menu of many pages?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top