<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zauberfisch / silverstripe-easy-linkfield example snippets
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;
}
}
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;
}
}
$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
}
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/src/model/ProductLink.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';
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.