循环时,我无法从VirtualPage conatiner访问has_one图像

时间:2018-01-18 00:37:34

标签: php silverstripe silverstripe-4

我有一个页面类型

  

ProductPage

$ has_one FeatureImage

有一个虚拟页面引用了其中一个ProductPages

当我在一个循环孩子的页面中,其中一个孩子是虚拟页面时,我可以渲染除FeatureImage字段之外的所有字段。

class ProductPage extends Page
{
    private static $db = [
        'TeaserText'=>'Varchar'
    ];

    private static $has_one = [
        'LinkedProduct'=>'Product', 
        'FeatureImage'=>Image::Class
    ];
    private static $many_many = [

    ];
    private static $owns = [
       'FeatureImage'
    ];

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

        $featureField = UploadField::create('FeatureImage', 'Feature Image')->setFolderName('FeatureImages')->setDescription("This image appears in the category pages. 400x400px");
        $teaserField = TextField::create('TeaserText', 'Teaser Text')->setDescription("This text appears in the category pages");
        $fields->addFieldToTab('Root.Main', $featureField, 'Metadata');
        $fields->addFieldToTab('Root.Main', $teaserField, 'Metadata');

        $productLinkField = DropdownField::create('LinkedProductID', 'Link a Product', Product::get()->map('ID', 'ProductName'));
        $productLinkField->setEmptyString('(Select one)');        

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

        $productLinkField->addExtraClass('stacked');
        $featureField->addExtraClass("stacked");
        $teaserField->addExtraClass("stacked");

        return $fields; 
    }
}

SS模板

<% loop $Children %>
    <div class="category-card mb-5">
        <div class="row">
           <div class="col-md-4">

                <img src="$FeatureImage.URL" class="fluid-image"/>
//This renders for standard ProductPage children

            </div>
            <div class="col-md-8 category-card-body">
               <h4 class="category-card-title"><a href="$Link">$MenuTitle</a></h4>
                <p class="category-card-text">$TeaserText</p>
//This TeaserText renders fine.

                <a href="$Link" class="category-card-link btn btn-info">view details</a>
             </div>                                           
        </div>
    </div>                           
<% end_loop %>

1 个答案:

答案 0 :(得分:0)

这可能是一个边缘用例,但我认为它是有效的,并且会为需要做同样事情的其他人发布我的解决方案。

VirtualPage类将提取所有数据库字段,但不会提取has_one关系,因此您无法从虚拟页面访问它。

来自VirtualPage.php:

* Note: This Only duplicates $db fields and not the $has_one etc..

我的解决方案是在虚拟页面类中包含一个方法,该方法将使用&lt;%with%&gt;

返回实际的父页面对象
public function getTheParentPageObject(){
    $page = DataObject::get_by_id(SiteTree::class, $this->getField('CopyContentFromID'));   
    return $page;
}

这允许我在模板中调用循环中的方法并拉出虚拟化之外的所有字段

    <% loop $Children %>
        <div class="category-card mb-5">
            <div class="row">
                <div class="col-md-4">                       

                    <% if $ClassName == "SilverStripe\CMS\Model\VirtualPage" %>
                        <% with $getTheFeaturedImageById() %>
                            <img src="$FeatureImage.URL" class="fluid-image"/>
                        <% end_with %>

                        <% else %>
                        <img src="$FeatureImage.URL" class="fluid-image"/>

                    <% end_if %>

                </div>
                <div class="col-md-8 category-card-body">                        
                    <h4 class="category-card-title"><a href="$Link">$MenuTitle</a></h4>
                    <p class="category-card-text">$TeaserText</p>
                    <a href="$Link" class="category-card-link btn btn-info">view details</a>
                </div>                                           
            </div>
        </div>                           
    <% end_loop %>