zauberfisch / silverstripe-easy-linkfield
SilverStripe 行内链接字段,允许向任何对象添加一个或多个链接,并将它们保存到一个单独的数据库字段中
v1.0.6
2023-05-10 22:48 UTC
Requires
- php: ^7.1 || ^8
- ext-json: *
- silverstripe/framework: ^4.5 || ^5.0
- silverstripe/vendor-plugin: *
- zauberfisch/silverstripe-serialized-dataobject: ^4.0.5
README
允许向任何对象添加一个或多个链接并将它们保存到一个单独的数据库字段中。编辑在表单字段内进行,不使用GridField或弹出窗口。
屏幕截图
已知问题/限制(正在修复中)
- 目前,无法限制用户可以添加的链接数量
- 仅在使用弹出窗口上传文件时才有效。直接“上传新文件”链接不起作用(1)
- 使用具有缩进的普通DropdownField而不是TreeDropdownField进行页面选择(1)
- 文件不会自动发布
(1) 问题是在底层依赖中的错误。ArrayListField中的子路由目前不起作用
维护者联系方式
- Zauberfisch code@zauberfisch.at
需求
- php >=7.1
- silverstripe/framework >=4.5
- zauberfisch/silverstripe-serialized-dataobject >=4
安装
composer require "zauberfisch/silverstripe-easy-linkfield"
- 重建清单(刷新)
文档
<?php class Page extends SilverStripe\CMS\Model\SiteTree { private static $db = [ 'Buttons' => \zauberfisch\LinkField\DBLinkList::class, ]; public function getCMSFields() { $fields = parent::getCMSFields(); $fields->addFieldsToTab( 'Root.Main', [ (new \zauberfisch\LinkField\LinkListField( 'Buttons', 'My Buttons')) ->setOrderable(true), ]); return $fields; } }
限制链接类型
您还可以限制允许的链接类型(内置链接类型包括:'内部', '外部', '文件', '电子邮件', '电话')
<?php class MyClass extends \SilverStripe\ORM\DataObject { private static $db = [ 'ContactDetails' => \zauberfisch\LinkField\DBLinkList::class, ]; public function getCMSFields() { $fields = parent::getCMSFields(); $fields->addFieldsToTab( 'Root.Main', [ (new \zauberfisch\LinkField\LinkListField( 'ContactDetails', 'My Contact Details', ['email', 'phone'])) ->setOrderable(true), ]); return $fields; } }
在PHP中访问值
$list = $page->obj('Buttons')->getValue(); // $page being a Page object with a field Buttons from the example above foreach($list as $button) { /** @var \zauberfisch\LinkField\Link\AbstractLink $button */ // Always available Variables: getLink(), getAbsoluteLink(), getLinkType(), getTitle(), getNewTab() // And depending on the type: getPage() (internal), getPageID() (internal), getURL() (external), getFile() (file), getFileID() (file), getEmail() (email), getCountryPrefix() (phone), getNumber() (phone), getPhoneNumber() (phone) $link = $button->getLink(); $absoluteLink = $button->getAbsoluteLink(); $type = $button->getLinkType(); // one of 'internal', 'external', 'file', 'email', 'phone' $title = $button->getTitle(); $openInNewTab = $button->getNewTab(); // use the values here }
在模板中访问值
<% loop $Buttons.getValue %> <%-- Always available Variables: $Link, $AbsoluteLink, $LinkType, $Title, $NewTab --%> <%-- And depending on the type: $Page (internal), $PageID (internal), $URL (external), $File (file), $FileID (file), $Email (email), $CountryPrefix (phone), $Number (phone), $PhoneNumber (phone) --%> <%-- If you use fields depending on the type, you have to check for the type first, otherwise you will get an error that the field was not found --%> <%-- For example <% if $LinkType == 'internal' %>The Link is $Link and the PAGE URLSegment is $Page.URLSegment<% end_if %> --%> <a href="$Link" <% if $NewTab %>target="_blank"<% end_if %>>$Title</a> <% end_loop %>
创建自定义链接类型(例如,用于DataObject)
<?php declare(strict_types=1); namespace app\model\shop; class Product extends DataObject { public function Link() { return "/shop/product-{$this->ID}/"; } public function AbsoluteLink() { return \SilverStripe\Control\DIrector::absoluteURL($this->Link()); } }
# /app/_config/extensions.yml zauberfisch\LinkField\LinkListField: link_types: product: 'app\model\ProductLink'
# /app/src/model/ProductLink.php <?php declare(strict_types=1); namespace app\model; use app\shop\Product; use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\FieldList; use zauberfisch\LinkField\Link\AbstractLink; /** * @property string $ProductID * @method string|int getProductID() * @method static setProductID(int $productID) */ class ProductLink extends AbstractLink { private static $fields = [ 'ProductID', ]; public function getCMSFields(): FieldList { $fields = parent::getCMSFields(); $fields->insertBefore( 'NewTab', new DropdownField('ProductID', $this->fieldLabel('Product'), Product::get()->map()->toArray()) ); return $fields; } /** * @return \SilverStripe\ORM\DataObject|null|Product */ public function getProduct(): ?Product { return $this->getProductID() ? Product::get()->byID($this->getProductID()) : null; } /** * @return \SilverStripe\ORM\DataObject|null|Product */ public function Product(): ?Product { return $this->getProduct(); } public function getLink(): string { $product = $this->getProduct(); return $product && $product->exists() ? $product->Link() : ''; } public function getAbsoluteLink(): string { $product = $this->getProduct(); return $product && $product->exists() ? $product->AbsoluteLink() : ''; } public function getLinkType(): string { return 'product'; } }