循环时无法从虚拟页面容器访问 has_one 图像
I can not access a has_one image from a VirtualPage conatiner when looping
我有一个页面类型
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 %>
这可能是一个边缘用例,但我认为它是有效的,并且 post 我的解决方案适用于需要做同样事情的任何其他人。
VirtualPage class 将提取所有数据库字段,但不会提取 has_one 关系,因此您无法从虚拟页面访问它。
来自VirtualPage.php:
* Note: This Only duplicates $db fields and not the $has_one etc..
我的解决方案是在虚拟页面 class 中包含一个方法,它将 return 实际的父页面对象 using <% with %>
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 %>
我有一个页面类型
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 %>
这可能是一个边缘用例,但我认为它是有效的,并且 post 我的解决方案适用于需要做同样事情的任何其他人。
VirtualPage class 将提取所有数据库字段,但不会提取 has_one 关系,因此您无法从虚拟页面访问它。
来自VirtualPage.php:
* Note: This Only duplicates $db fields and not the $has_one etc..
我的解决方案是在虚拟页面 class 中包含一个方法,它将 return 实际的父页面对象 using <% with %>
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 %>