Skip to content

Commit

Permalink
Created method aliases without get prefix, updated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Zauberfisch committed Apr 20, 2021
1 parent 94045ca commit e60fba1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# SilverStripe (inline) LinkField module

Allows adding one or multiple links to any object and saves into a single DB field.
Editing happens inline in the form field, no GridField or popup is used.
Allows adding one or multiple links to any object and saves into a single DB field.
Editing happens inline in the form field, no GridField or popup is used.

## Screenshots

Empty LinkList:
Empty LinkList:
![](https://paste.zauberfisch.com/i/600e0dc1c4323/linkfield-empty.png)

LinkList with 6 links (all possible types):
LinkList with 6 links (all possible types):
![](https://paste.zauberfisch.com/i/600e0dc1c4323/linkfield-all.png)


Expand Down Expand Up @@ -101,8 +101,10 @@ foreach($list as $button) {

```html
<% 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) -->
<%-- 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 %>
```
Expand Down
12 changes: 12 additions & 0 deletions src/Link/AbstractLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,21 @@ public function getCMSFields(): FieldList {

abstract public function getLink(): string;

public function Link(): string {
return $this->getLink();
}

public function getAbsoluteLink(): string {
return $this->getLink();
}

public function AbsoluteLink(): string {
return $this->getAbsoluteLink();
}

abstract public function getLinkType(): string;

public function LinkType(): string {
return $this->getLinkType();
}
}
8 changes: 8 additions & 0 deletions src/Link/FileLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace zauberfisch\LinkField\Link;

use SilverStripe\Assets\File;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Forms\FieldList;
use zauberfisch\SerializedDataObject\Form\UploadField;

Expand Down Expand Up @@ -35,6 +36,13 @@ public function getFile(): ?File {
return $this->getFileID() ? File::get()->byID($this->getFileID()) : null;
}

/**
* @return \SilverStripe\ORM\DataObject|null|File
*/
public function File(): ?File {
return $this->getFile();
}

public function getLink(): string {
$file = $this->getFile();
return $file && $file->exists() ? $file->getURL() : '';
Expand Down
7 changes: 7 additions & 0 deletions src/Link/InternalLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public function getPage(): ?SiteTree {
return $this->getPageID() ? SiteTree::get()->byID($this->getPageID()) : null;
}

/**
* @return \SilverStripe\ORM\DataObject|null|SiteTree
*/
public function Page(): ?SiteTree {
return $this->getPage();
}

public function getLink(): string {
$page = $this->getPage();
return $page && $page->exists() ? $page->Link() : '';
Expand Down
14 changes: 9 additions & 5 deletions src/Link/PhoneLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PhoneLink extends AbstractLink {
'NumberWithoutPrefix',
];

public function getCMSFields() : FieldList {
public function getCMSFields(): FieldList {
$fields = parent::getCMSFields();
$countryMap = [];
$fields->insertBefore('NewTab',
Expand All @@ -38,15 +38,19 @@ public function getCMSFields() : FieldList {
return $fields;
}

public function getPhoneNumber() :string {
public function getPhoneNumber(): string {
return $this->CountryPrefix . $this->NumberWithoutPrefix;
}

public function getLink() :string {
return sprintf('tel:%s', $this->getPhoneNumber());
public function PhoneNumber(): string {
return $this->getPhoneNumber();
}

public function getLinkType() :string {
public function getLink(): string {
return sprintf('tel:%s', $this->getPhoneNumber());
}

public function getLinkType(): string {
return 'phone';
}
}

0 comments on commit e60fba1

Please sign in to comment.