Skip to content

Commit

Permalink
NEW Allowed link types
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabina Talipova committed Dec 7, 2023
1 parent 5d41b85 commit 737a662
Show file tree
Hide file tree
Showing 28 changed files with 425 additions and 351 deletions.
8 changes: 0 additions & 8 deletions _config/graphql.yml

This file was deleted.

20 changes: 0 additions & 20 deletions _config/types.yml

This file was deleted.

3 changes: 0 additions & 3 deletions _graphql/queries.yml

This file was deleted.

5 changes: 0 additions & 5 deletions _graphql/types.yml

This file was deleted.

2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions client/src/boot/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* global document */
/* eslint-disable */
import registerComponents from './registerComponents';
import registerQueries from './registerQueries';

document.addEventListener('DOMContentLoaded', () => {
registerComponents();
registerQueries();
});
8 changes: 0 additions & 8 deletions client/src/boot/registerQueries.js

This file was deleted.

1 change: 0 additions & 1 deletion client/src/components/LinkField/LinkField.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ const mapDispatchToProps = (dispatch) => ({
});

export default compose(
injectGraphql('readLinkTypes'),
fieldHolder,
connect(null, mapDispatchToProps)
)(LinkField);
1 change: 1 addition & 0 deletions client/src/entwine/LinkField.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jQuery.entwine('ss', ($) => {
value,
onChange: this.handleChange.bind(this),
isMulti: this.data('is-multi') ?? false,
types: this.data('types') ?? [],
};
},

Expand Down
9 changes: 5 additions & 4 deletions src/Controllers/LinkFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use SilverStripe\Forms\DefaultFormFactory;
use SilverStripe\Forms\Form;
use SilverStripe\LinkField\Models\Link;
use SilverStripe\LinkField\Type\Registry;
use SilverStripe\Security\SecurityToken;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction;
Expand All @@ -19,6 +18,8 @@
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\HiddenField;
use SilverStripe\LinkField\Form\LinkField;
use SilverStripe\LinkField\Services\LinkTypeService;
use SilverStripe\ORM\DataList;

class LinkFieldController extends LeftAndMain
Expand Down Expand Up @@ -74,7 +75,7 @@ public function linkForm(): Form
}
} else {
$typeKey = $this->typeKeyFromRequest();
$link = Registry::create()->byKey($typeKey);
$link = LinkTypeService::byKey($typeKey);
if (!$link) {
$this->jsonError(404, _t('LinkField.INVALID_TYPEKEY', 'Invalid typeKey'));
}
Expand Down Expand Up @@ -174,7 +175,7 @@ public function save(array $data, Form $form): HTTPResponse
// Creating a new Link
$operation = 'create';
$typeKey = $this->typeKeyFromRequest();
$className = Registry::create()->list()[$typeKey] ?? '';
$className = LinkTypeService::byKey($typeKey) ?? '';
if (!$className) {
$this->jsonError(404, _t('LinkField.INVALID_TYPEKEY', 'Invalid typeKey'));
}
Expand Down Expand Up @@ -239,7 +240,7 @@ private function createLinkForm(Link $link, string $operation): Form
$form = $formFactory->getForm($this, $name, ['Record' => $link]);

// Set where the form is submitted to
$typeKey = Registry::create()->keyByClassName($link->ClassName);
$typeKey = LinkTypeService::keyByClassName($link->ClassName);
$form->setFormAction($this->Link("linkForm/$id?typeKey=$typeKey"));

// Add save action button
Expand Down
3 changes: 3 additions & 0 deletions src/Form/LinkField.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataObjectInterface;
use SilverStripe\LinkField\Models\Link;
use SilverStripe\LinkField\Traits\AllowedLinkClassesTrait;

/**
* Allows CMS users to edit a Link object.
*/
class LinkField extends FormField
{
use AllowedLinkClassesTrait;

protected $schemaComponent = 'LinkField';

protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_CUSTOM;
Expand Down
3 changes: 3 additions & 0 deletions src/Form/MultiLinkField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use LogicException;
use SilverStripe\Forms\FormField;
use SilverStripe\LinkField\Traits\AllowedLinkClassesTrait;
use SilverStripe\ORM\DataObjectInterface;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\Relation;
Expand All @@ -16,6 +17,8 @@
*/
class MultiLinkField extends FormField
{
use AllowedLinkClassesTrait;

protected $schemaComponent = 'LinkField';

protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_CUSTOM;
Expand Down
30 changes: 0 additions & 30 deletions src/GraphQL/LinkTypeResolver.php

This file was deleted.

2 changes: 2 additions & 0 deletions src/Models/EmailLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class EmailLink extends Link
{
private static string $table_name = 'LinkField_EmailLink';

private static $short_code = 'email';

private static array $db = [
'Email' => 'Varchar(255)',
];
Expand Down
2 changes: 2 additions & 0 deletions src/Models/ExternalLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class ExternalLink extends Link
{
private static string $table_name = 'LinkField_ExternalLink';

private static $short_code = 'external';

private static array $db = [
'ExternalUrl' => 'Varchar',
];
Expand Down
2 changes: 2 additions & 0 deletions src/Models/FileLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class FileLink extends Link
{
private static string $table_name = 'LinkField_FileLink';

private static $short_code = 'file';

private static array $has_one = [
'File' => File::class,
];
Expand Down
9 changes: 6 additions & 3 deletions src/Models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\LinkField\Type\Registry;
use SilverStripe\LinkField\Form\LinkField;
use SilverStripe\LinkField\Services\LinkTypeService;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBHTMLText;

Expand All @@ -26,6 +27,8 @@ class Link extends DataObject
{
private static $table_name = 'LinkField_Link';

private static $short_code = 'link';

private static array $db = [
'Title' => 'Varchar',
'OpenInNew' => 'Boolean',
Expand Down Expand Up @@ -188,7 +191,7 @@ function setData($data): Link
);
}

$type = Registry::singleton()->byKey($typeKey);
$type = LinkTypeService::byKey($typeKey);

if (!$type) {
throw new InvalidArgumentException(
Expand Down Expand Up @@ -225,7 +228,7 @@ function setData($data): Link

public function jsonSerialize(): mixed
{
$typeKey = Registry::singleton()->keyByClassName(static::class);
$typeKey = LinkTypeService::keyByClassName(static::class);

if (!$typeKey) {
return [];
Expand Down
2 changes: 2 additions & 0 deletions src/Models/PhoneLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class PhoneLink extends Link
{
private static string $table_name = 'LinkField_PhoneLink';

private static $short_code = 'phone';

private static array $db = [
'Phone' => 'Varchar(255)',
];
Expand Down
2 changes: 2 additions & 0 deletions src/Models/SiteTreeLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class SiteTreeLink extends Link
{
private static string $table_name = 'LinkField_SiteTreeLink';

private static $short_code = 'cms';

private static array $db = [
'Anchor' => 'Varchar',
'QueryString' => 'Varchar',
Expand Down
89 changes: 89 additions & 0 deletions src/Services/LinkTypeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace SilverStripe\LinkField\Services;

use LogicException;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\LinkField\Models\Link;

class LinkTypeService
{

use Injectable;

public static function generateAllLinkTypes(): array
{
$typeDefinitions = ClassInfo::subclassesFor(Link::class);

$result = array();
foreach ($typeDefinitions as $class) {
if (is_subclass_of($class, Link::class)) {
$type = Injector::inst()->get($class)->config()->get('short_code');
$result[$type] = $class;
}
}

return $result;
}

public static function byKey(string $key): ?Link
{
/** @var array $types */
$typeDefinitions = static::generateAllLinkTypes();
$definition = $typeDefinitions[$key] ?? null;

if (!$definition) {
return null;
}

return static::definitionToType($definition);
}

public static function keyByClassName(string $classname): ?string
{
$typeDefinitions = static::generateAllLinkTypes();

foreach ($typeDefinitions as $key => $class) {
if ($class === $classname) {
return $key;
}
}

return null;
}

public static function definitionToType(string $className): Link
{
if (!$className && !($className instanceof Link)) {
throw new LogicException(
_t(
'LinkField.NO_CLASSNAME',
'"{class}": All types should reference a valid classname',
['class' => static::class],
sprintf('%s: All types should reference a valid classname', static::class),
),
);
}

/** @var Link $type */
$type = Injector::inst()->get($className);

if (!$type instanceof Link) {
throw new LogicException(
_t(
'LinkField.INVALID_TYPENAME',
'"{class}": {typename} is not a valid link type',
[
'class' => static::class,
'typename' => $className,
],
sprintf('%s: %s is not a valid link type', static::class, $className),
),
);
}

return $type;
}
}
Loading

0 comments on commit 737a662

Please sign in to comment.