是否有更好的方法在页脚中为轮播数据对象编写函数 [英] Is there a better way to write the function for my carousel dataobject in my footer

查看:81
本文介绍了是否有更好的方法在页脚中为轮播数据对象编写函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a dataobject that I use on my IndexPage page which works fine in its footer but it does not render on my other pages in the footer.

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

This is what I have tried so far in the default CWP PageController page and has made the data disappear from the index page (my IndexPage is extended by the CWP Page template):

```<?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;     

  }

}

推荐答案

我添加了一些不同的示例,这些示例可以帮助您决定如何设置与页脚的关系.

I added a few different examples which might help you decide on how you want to setup the relations to the footer.

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

It's also possible to have a relation between the Footer and SiteConfig through an extension. So you would be able to do $SiteConfig.Footeror $SiteConfig.FooterFeedback in your templates.

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

Also, I don't see why the feedback needs to be related to the footer model, you could just define the feedback relation on the page model directly:

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

这篇关于是否有更好的方法在页脚中为轮播数据对象编写函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆