bigfork/silverstripe-linkset

Silverstripe module that allows for easy adding of links to the CMS

Installs: 2

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 0

Open Issues: 0

Type:silverstripe-vendormodule

2.0.0 2023-08-18 08:24 UTC

This package is auto-updated.

Last update: 2024-10-18 10:54:08 UTC


README

Adds easy to implement link options to the CMS

Features

  • Choose from the following options: None, Page, File, URL
  • Option to remove fields that don't apply to your implementation
  • Can output link straight to template

Installation

$ composer require bigfork/silverstripe-linkset

How to use

You will need to create a $has_one relation on your data object

private static array $has_one = [
    'ButtonLink' => LinkSet::class,
];

Then add the LinksetField to your fieldlist

$fields->addFieldsToTab(
    'Root.Main',
    [
        ...otherFields,
        LinksetField::create($this, 'ButtonLink'),
        ...otherFields,
    ]
);

LinkSetField constructor accepts five params:

  • DataObject $data - The object the relation is on $this
  • string $name - Name of the field should match the relation key
  • ?string $title (optional) - Title of the field, will set the heading above the fields
  • array $fieldsToRemove (optional) - To remove Title from the list of fields pass in array for example ['Title']. Possible options to remove are:
    • Title
    • LinkType
    • LinkURL
    • LinkTarget
    • LinkedPageID
    • LinkedFile
  • array $typesToRemove (optional) - Remove link types from the radio, to remove the option for None pass in array for example ['None']. Possible options to remove are:
    • None
    • Page
    • File
    • URL

With all the parameters your code may look like:

$fields->addFieldsToTab(
    'Root.Main',
    [
        ...otherFields,
        LinksetField::create($this, 'ButtonLink', 'Button link', ['Title'], ['None']),
        ...otherFields,
    ]
);

Using in templates

You can output the link in the template by calling the relation

{$ButtonLink.AddExtraClass('button button--primary')}

This will output a simple anchor tag with the all the fields that had been set on the LinkSetField group.

Custom anchors can be done by accessing the fields through the relation as normal

<% with $ButtonLink %>
  <a 
    href="{$Link} 
    class="button"
    <% if $LinkTarget %> target="{$LinkTarget}"<% end_if %>
    <% if $LinkType = 'File' %> download<% end_if %>
  >
    {$Title}
  </a>
<% end_with %>