是否有更好的方法在页脚中为轮播数据对象编写函数

时间:2019-12-10 22:38:10

标签: silverstripe-4

我在IndexPage页面上使用了一个数据对象,该数据对象在页脚中可以正常使用,但不能在页脚中的其他页面上呈现。

到目前为止,这是我在默认的CWP PageController页面中尝试过的操作,并使数据从索引页中消失了(我的IndexPage由CWP Page模板扩展了):

```<?php

namespace SilverStripe\IndexPage;

use Page;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\Assets\Image;
use SilverStripe\Assets\File;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\DropdownField;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;

class IndexPage extends Page {

    private static $description = 'Custom homepage';
    private static $icon = 'cwp/cwp:images/icons/sitetree_images/home.png';

 private static $has_many = [
    'FooterFeedback' => Footer::class,
];

private static $owns = [
    'FooterFeedback',
    ];

private static $table_name = 'IndexPageTB';

$fields->addFieldToTab('Root.FooterFeedback', 
    $gridfield = GridField::create('FooterFeedback', 'FooterFeedback', $this->FooterFeedback(), 
    GridFieldConfig_RecordEditor::create()));
$gridConfigE = $gridfield->getConfig();
$gridConfigE->addComponent(new GridFieldOrderableRows('SortOrder'));
$gridConfigE->addComponent(new GridFieldDeleteAction);
return $fields;
  }


}```




```<?php

namespace SilverStripe\IndexPage;

use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBEnum;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
//use SilverStripe\Assets\Image;
//use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Assets\Image;
use SilverStripe\Assets\File;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\Forms\DropdownField;

class Footer extends DataObject {

private static $db = [      
    'Feedback' => 'HTMLText',
    'ClientAlias' => 'Varchar(255)',
    'SortOrder' => 'Int'
];

private static $has_one = [
    'Project' => IndexPage::class,
    'Avatar' => Image::class,
];

private static $owns = [
    'Avatar',
];

private static $summary_fields = [
    'GridThumbnail' => '',
    'Feedback' => 'Feedback',
    'ClientAlias' => 'Client alias',
];

public function getGridThumbnail() {
    if($this->Avatar()->exists()) {
        return $this->Avatar()->ScaleWidth(120);
    }
    return "(no image)";
}

private static $table_name = 'Footer';


public function getCMSFields() {
    $fields = FieldList::create(
        $uploader = UploadField::create('Avatar'),
        TextField::create('ClientAlias', 'Client name or alias')->setMaxLength(300)->setDescription('Max 300 characters'),
        HTMLEditorField::create('Feedback', 'Feedback')->setDescription('Client feedback')
        );

    return $fields;
    }

}


<div class="col-md-7">
  <h1 class="footerbrand">Client feedback</h1>
  <div id="quotes">
    <% if $FooterFeedback %>

            <% loop $FooterFeedback %>
                <div class="textItem">
                    <div class="avatar">
                        <img src="$Avatar.URL" alt="avatar">
                    </div>
                    $Feedback
                    <p><b> $ClientAlias </b></p>
                </div>
            <% end_loop %>

    <% end_if %>

  </div>
  <div class="clearfix">
  </div>
</div>



<?php

use CWP\CWP\PageTypes\BasePageController;

class PageController extends BasePageController
{
/*public function FooterFeedback()
{
    return Footer::get();
}*/




public function FooterFeedback()
  {
   $Footer = \SilverStripe\IndexPage\IndexPage::get()->first();
   return $Footer;     

  }

}

1 个答案:

答案 0 :(得分:0)

我添加了一些不同的示例,这些示例可能有助于您决定如何设置与页脚的关系。

// ----- Current situation: -----
class IndexPage extends Page
{
//    ...
    private static $has_many = [
        'FooterFeedback' => Footer::class,
    ];
//    ...
}

class Footer extends DataObject
{
//    ...
}

// Does your other pages extend IndexPage or Page?
class MyOtherPageWhichExtendsIndexPage extends IndexPage
{
    // As this page extends IndexPage you are able to loop $FooterFeedback in the templates for this page.
}

class MyOtherPageWhichExtendsPage extends Page
{
    // As this page extends Page you can not loop $FooterFeedback in the templates, as only the IndexPage has the relations to footer feedback.
}


// ----- Alternative 1 solution: -----
class Page extends SiteTree
{
//    ... Add your footer relation on the Page model, or on SiteTree directly through an extension.
    private static $has_many = [
        'FooterFeedback' => Footer::class,
    ];
//    ...
}

class Footer extends DataObject
{
//    ...
}

// Now you can extend any Page which has extended root Page class and have access to the footer feedback.
class MyOtherPageWhichExtendsIndexPage extends IndexPage
{
    // As this page extends IndexPage which extends Page, you are able to loop $FooterFeedback in the templates for this page.
}

class MyOtherPageWhichExtendsPage extends Page
{
    // As this page extends Page, you are able to loop $FooterFeedback in the templates for this page.
}


// ----- Alternative 2 solution: -----
// Footer has no relations to any pages (create a separate model admin for it).
class Footer extends DataObject
{
    // ...
    private static $has_many = [
        'Feedback' => Feedback::class,
    ];
}

class Feedback extends DataObject
{
    private static $db = [
        'Feedback' => 'HTMLText',
        'ClientAlias' => 'Varchar(255)',
        'SortOrder' => 'Int'
    ];

    private static $has_one = [
        'Footer' => Footer::class,
        'Avatar' => Image::class,
        // 'Page' => Page::class, <- If you need to display different feedback for different pages.
    ];

    private static $owns = [
        'Avatar',
    ];
}

// Then in your base page controller you can add a getFooter method which is available to all your controllers which extends PageController.
class PageController extends ContentController
{
    public function getFooter()
    {
        return Footer::get()->first();
    }
}

// Page.ss
// <% if $Footer %> <% with $Footer %> <% if $Feedback %> <% loop $Feedback %> $Feedback $ClientAlias ... <% end_.. %>

也可以通过扩展在页脚和SiteConfig之间建立关系。这样您就可以在模板中执行$SiteConfig.Footer$SiteConfig.FooterFeedback

此外,我不明白为什么反馈需要与页脚模型相关,您可以直接在页面模型上定义反馈关系:

class Page extends SiteTree 
{
    private static $has_many = [
        'Feedback' => Feedback::class,
    ];
}

class Feedback extends DataObject 
{
    private static $has_one = [
        'Page' => Page::class,
    ];
}

// AnyPage.ss <% if $Feedback %> <% loop $feedback %> ...