Skip to content

Commit

Permalink
MNT Reorder methods to match our coding conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Feb 15, 2024
1 parent d56eb64 commit 5c1ee0e
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 170 deletions.
14 changes: 7 additions & 7 deletions src/Models/EmailLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public function getCMSFields(): FieldList
return parent::getCMSFields();
}

public function getCMSCompositeValidator(): CompositeValidator
{
$validator = parent::getCMSCompositeValidator();
$validator->addValidator(RequiredFields::create(['Email']));
return $validator;
}

public function getDescription(): string
{
return $this->Email ?: '';
Expand All @@ -54,11 +61,4 @@ public function getMenuTitle(): string
{
return _t(__CLASS__ . '.LINKLABEL', 'Link to email address');
}

public function getCMSCompositeValidator(): CompositeValidator
{
$validator = parent::getCMSCompositeValidator();
$validator->addValidator(RequiredFields::create(['Email']));
return $validator;
}
}
14 changes: 7 additions & 7 deletions src/Models/ExternalLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public function getCMSFields(): FieldList
return parent::getCMSFields();
}

public function getCMSCompositeValidator(): CompositeValidator
{
$validator = parent::getCMSCompositeValidator();
$validator->addValidator(RequiredFields::create(['ExternalUrl']));
return $validator;
}

public function getDescription(): string
{
return $this->ExternalUrl ?: '';
Expand All @@ -55,11 +62,4 @@ public function getMenuTitle(): string
{
return _t(__CLASS__ . '.LINKLABEL', 'Link to external URL');
}

public function getCMSCompositeValidator(): CompositeValidator
{
$validator = parent::getCMSCompositeValidator();
$validator->addValidator(RequiredFields::create(['ExternalUrl']));
return $validator;
}
}
28 changes: 14 additions & 14 deletions src/Models/FileLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public function getCMSFields(): FieldList
return parent::getCMSFields();
}

public function getCMSCompositeValidator(): CompositeValidator
{
$validator = parent::getCMSCompositeValidator();
$validator->addValidator(RequiredFields::create(['File']));
return $validator;
}

public function getDescription(): string
{
$file = $this->File();
Expand All @@ -52,16 +59,6 @@ public function getURL(): string
return $file->exists() ? (string) $file->getURL() : '';
}

protected function getDefaultTitle(): string
{
$file = $this->File();
if (!$file?->exists()) {
return _t(__CLASS__ . '.MISSING_DEFAULT_TITLE', '(File missing)');
}

return (string) $this->getDescription();
}

/**
* The title that will be displayed in the dropdown
* for selecting the link type to create.
Expand All @@ -71,10 +68,13 @@ public function getMenuTitle(): string
return _t(__CLASS__ . '.LINKLABEL', 'Link to a file');
}

public function getCMSCompositeValidator(): CompositeValidator
protected function getDefaultTitle(): string
{
$validator = parent::getCMSCompositeValidator();
$validator->addValidator(RequiredFields::create(['File']));
return $validator;
$file = $this->File();
if (!$file?->exists()) {
return _t(__CLASS__ . '.MISSING_DEFAULT_TITLE', '(File missing)');
}

return (string) $this->getDescription();
}
}
209 changes: 102 additions & 107 deletions src/Models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace SilverStripe\LinkField\Models;

use ReflectionException;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Forms\FieldList;
use SilverStripe\LinkField\Services\LinkTypeService;
Expand Down Expand Up @@ -67,28 +66,6 @@ class Link extends DataObject
*/
private static $icon = 'font-icon-link';

/**
* Get a short description of the link.
*
* This is displayed in LinkField as an indication of what the link is pointing at.
*/
public function getDescription(): string
{
return '';
}

/**
* Get the react component used to render the modal form.
*/
public function getLinkTypeHandlerName(): string
{
return 'FormBuilderModal';
}

/**
* @return FieldList
* @throws ReflectionException
*/
public function getCMSFields(): FieldList
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
Expand Down Expand Up @@ -119,6 +96,96 @@ public function getCMSFields(): FieldList
return parent::getCMSFields();
}

/**
* Get a short description of the link. This is displayed in LinkField as an indication of what the link is pointing at.
*
* This method should be overridden by any subclasses
*/
public function getDescription(): string
{
return '';
}

/**
* Get the URL this Link links to.
*
* This method should be overridden by any subclasses
*/
public function getURL(): string
{
return '';
}

/**
* Get the react component used to render the modal form.
*/
public function getLinkTypeHandlerName(): string
{
return 'FormBuilderModal';
}

/**
* The title that will be displayed in the dropdown
* for selecting the link type to create.
*
* Subclasses should override this.
* It will use the singular_name by default.
*/
public function getMenuTitle(): string
{
return $this->i18n_singular_name();
}

public function getTitle(): string
{
// If we have link text, we can just bail out without any changes
if ($this->LinkText) {
return $this->LinkText;
}

$defaultLinkTitle = $this->getDefaultTitle();

$this->extend('updateDefaultLinkTitle', $defaultLinkTitle);

return $defaultLinkTitle;
}

/**
* This method process the defined singular_name of Link class
* to get the short code of the Link class name.
*
* Or If the name is not defined (by redefining $singular_name in the subclass),
* this use the class name. The Link prefix is removed from the class name
* and the resulting name is converted to lowercase.
* Example: Link => link, EmailLink => email, FileLink => file, SiteTreeLink => sitetree
*/
public function getShortCode(): string
{
return strtolower(rtrim(ClassInfo::shortName($this), 'Link')) ?? '';
}

/**
* Get a string representing the versioned state of the link.
*/
public function getVersionedState(): string
{
if (!$this->exists()) {
return 'unsaved';
}
if ($this->hasExtension(Versioned::class)) {
if ($this->isPublished()) {
if ($this->isModifiedOnDraft()) {
return 'modified';
}
return 'published';
}
return 'draft';
}
// Unversioned - links are saved in the modal so there is no 'dirty state' and
// when undversioned saved is the same thing as published
return 'unversioned';
}

public function onBeforeWrite(): void
{
// Ensure a Sort value is set and that it's one larger than any other Sort value for
Expand Down Expand Up @@ -171,38 +238,6 @@ public function forTemplate()
return $this->renderWith([static::class, self::class]);
}

/**
* Get the URL this Link links to.
*
* This method should be overridden by any subclasses
*/
public function getURL(): string
{
return '';
}

/**
* Get a string representing the versioned state of the link.
*/
public function getVersionedState(): string
{
if (!$this->exists()) {
return 'unsaved';
}
if ($this->hasExtension(Versioned::class)) {
if ($this->isPublished()) {
if ($this->isModifiedOnDraft()) {
return 'modified';
}
return 'published';
}
return 'draft';
}
// Unversioned - links are saved in the modal so there is no 'dirty state' and
// when undversioned saved is the same thing as published
return 'unversioned';
}

/**
* Get the owner of this link, if there is one.
*
Expand Down Expand Up @@ -269,6 +304,18 @@ public function can($perm, $member = null, $context = [])
};
}

/**
* Get the title that will be displayed if there is no LinkText.
*/
protected function getDefaultTitle(): string
{
$default = $this->getDescription() ?: $this->getURL();
if (!$default) {
$default = _t(static::class . '.MISSING_DEFAULT_TITLE', '(No value provided)');
}
return $default;
}

private function canPerformAction(string $canMethod, $member, $context = [])
{
// Allow extensions to override permission checks
Expand All @@ -290,56 +337,4 @@ private function canPerformAction(string $canMethod, $member, $context = [])
// Default to DataObject's permission checks
return parent::$canMethod($member, $context);
}

public function getTitle(): string
{
// If we have link text, we can just bail out without any changes
if ($this->LinkText) {
return $this->LinkText;
}

$defaultLinkTitle = $this->getDefaultTitle();

$this->extend('updateDefaultLinkTitle', $defaultLinkTitle);

return $defaultLinkTitle;
}

/**
* Get the title that will be displayed if there is no LinkText.
*/
protected function getDefaultTitle(): string
{
$default = $this->getDescription() ?: $this->getURL();
if (!$default) {
$default = _t(static::class . '.MISSING_DEFAULT_TITLE', '(No value provided)');
}
return $default;
}

/**
* This method process the defined singular_name of Link class
* to get the short code of the Link class name.
*
* Or If the name is not defined (by redefining $singular_name in the subclass),
* this use the class name. The Link prefix is removed from the class name
* and the resulting name is converted to lowercase.
* Example: Link => link, EmailLink => email, FileLink => file, SiteTreeLink => sitetree
*/
public function getShortCode(): string
{
return strtolower(rtrim(ClassInfo::shortName($this), 'Link')) ?? '';
}

/**
* The title that will be displayed in the dropdown
* for selecting the link type to create.
*
* Subclasses should override this.
* It will use the singular_name by default.
*/
public function getMenuTitle(): string
{
return $this->i18n_singular_name();
}
}
14 changes: 7 additions & 7 deletions src/Models/PhoneLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public function getCMSFields(): FieldList
return parent::getCMSFields();
}

public function getCMSCompositeValidator(): CompositeValidator
{
$validator = parent::getCMSCompositeValidator();
$validator->addValidator(RequiredFields::create(['Phone']));
return $validator;
}

public function getDescription(): string
{
return $this->Phone ?: '';
Expand All @@ -49,11 +56,4 @@ public function getMenuTitle(): string
{
return _t(__CLASS__ . '.LINKLABEL', 'Phone number');
}

public function getCMSCompositeValidator(): CompositeValidator
{
$validator = parent::getCMSCompositeValidator();
$validator->addValidator(RequiredFields::create(['Phone']));
return $validator;
}
}
Loading

0 comments on commit 5c1ee0e

Please sign in to comment.