Is there a better way to write the function for my carousel dataobject in my footer

亡梦爱人 提交于 2019-12-24 18:54:26

问题


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.

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;     

  }

}

回答1:


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

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


来源:https://stackoverflow.com/questions/59276382/is-there-a-better-way-to-write-the-function-for-my-carousel-dataobject-in-my-foo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!