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 7689bc4 commit 37769f9
Show file tree
Hide file tree
Showing 23 changed files with 427 additions and 358 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.

12 changes: 9 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 Down Expand Up @@ -188,7 +189,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 +226,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 Expand Up @@ -320,4 +321,9 @@ public function getDefaultTitle(): string
}
return $default;
}

public function getShortCode(): string
{
return strtolower(str_replace([' ', 'Link'], '', $this->LinkTypeTile()));
}
}
101 changes: 101 additions & 0 deletions src/Services/LinkTypeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace SilverStripe\LinkField\Services;

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

class LinkTypeService
{
use Injectable;

/**
* Generate all link types that are subclasses of Link::class
*/
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)->getShortCode();
$result[$type] = $class;
}
}

return $result;
}

/**
* Return a Link instance by key
* @throws InvalidArgumentException
*/
public static function byKey(string $key): ?Link
{
$typeDefinitions = static::generateAllLinkTypes();
$definition = $typeDefinitions[$key] ?? null;

if (!$definition) {
return null;
}

return static::definitionToType($definition);
}

/**
* Return a key for link type by classname
* @throws InvalidArgumentException
*/
public static function keyByClassName(string $classname): ?string
{
$typeDefinitions = static::generateAllLinkTypes();

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

return null;
}

/**
* @throws InvalidArgumentException
*/
public static function definitionToType(string $className): Link
{
if (!$className && !($className instanceof Link)) {
throw new InvalidArgumentException(
_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 InvalidArgumentException(
_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 37769f9

Please sign in to comment.