https://github.com/arillo/silverstripe-links
https://github.com/arillo/silverstripe-links
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/arillo/silverstripe-links
- Owner: arillo
- License: mit
- Created: 2018-11-02T14:54:38.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2025-03-21T13:55:45.000Z (over 1 year ago)
- Last Synced: 2025-10-21T07:43:50.701Z (9 months ago)
- Language: PHP
- Size: 35.2 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: License.md
Awesome Lists containing this project
README
# Arillo\Links
[](https://packagist.org/packages/arillo/silverstripe-links)
[](https://packagist.org/packages/arillo/silverstripe-links)
Add links to any DataObject.
### Requirements
SilverStripe CMS ^5.0
## Installation
```bash
composer require arillo/silverstripe-links
```
## Usage
Attach the `Arillo\Links\LinkExtension` to your DataObject via `config.yml`:
```
MyDataObject:
extensions:
- Arillo\Links\LinkExtension
```
```php
use SilverStripe\ORM\DataObject;
use Arillo\Links\Link;
use Arillo\Links\LinkExtension;
class MyDataObject extends DataObject
{
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function($fields) {
// in case you use Link::EDITMODE_PLAIN, you might need
// to remove the link relation field generated by the scaffolder.
$fields->removeByName(LinkExtension::FIELD . 'ID')
$fields->addFieldsToTab(
'Root.Main',
// add link fields directly to the belonging DataObject.
Link::edit_fields(
$this,
[
'mode' => Link::EDITMODE_PLAIN,
'showLinkTitle' => true,
]
)
// or use default editing via HasOneButtonField
Link::edit_fields($this)
);
});
return parent::getCMSFields();
}
}
```
Inspect `Arillo\Links\Link::DEFAULT_FIELDS_CONFIG` for all available config keys.
A link can be rendered in templates like this:
```
<% if $LinkObject.Exists %>
<% with $LinkObject %>
$Title
<% end_with %>
<% end_if %>
```
or use the template of the module:
```
<% include Link Link=$LinkObject, CssClass=btn btn-primary %>
```
## Extending
Since version 1.x extensibility should have become easier.
In this example we are adding the ability to create anchor links to [elements](https://github.com/arillo/silverstripe-elements):
```php
ElementBase::class,
];
// alter cms fields
public function updateLinkCMSFields(
FieldList $fields,
DataObject $holderRecord,
array $config = []
) {
$fieldsPrefix = $config['fieldsPrefix'];
if ($this->owner->PageID) {
$fields->push(
DropdownField::create(
"{$fieldsPrefix}AnchorElementID",
'Anker-Element',
$this->owner
->Page()
->Elements()
->map()
->toArray()
)
->setEmptyString('[keins]')
->displayIf("{$fieldsPrefix}Type")
->isEqualTo('internal')
->end()
);
}
}
// alter href
public function updateLinkHref($href)
{
if (
$href &&
$this->owner->Type == 'internal' &&
$this->owner->AnchorElement()->exists()
) {
$href .= "#{$this->owner->AnchorElement()->URLSegment}";
}
return $href;
}
}
```
Add extension via config:
```yaml
Arillo\Links\Link:
extensions:
- Arillo\Extensions\LinkExtension
```
## Changelog
**1.0.3**
- upgraded `silvershop/silverstripe-hasonefield`
**1.0.0**
- improved extensibility