-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sabina Talipova
committed
Dec 12, 2023
1 parent
7f65d4f
commit 421a198
Showing
24 changed files
with
367 additions
and
406 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace SilverStripe\LinkField\Form\Traits; | ||
|
||
use SilverStripe\Core\Injector\Injector; | ||
use SilverStripe\LinkField\Models\Link; | ||
use SilverStripe\LinkField\Services\LinkTypeService; | ||
|
||
/** | ||
* Trait to manage which Link type can be added to LinkField form field. | ||
* This trait is used in LinkField and MultiLinkField classes. | ||
*/ | ||
trait AllowedLinkClassesTrait | ||
{ | ||
private $allowed_types = []; | ||
|
||
/** | ||
* Set allowed types for LinkField | ||
*/ | ||
public function setAllowedTypes(array $types): static | ||
{ | ||
if (empty($types) || !$this->validateTypes($types)) { | ||
$this->allowed_types = $this->genarateAllowedTypes(); | ||
} else { | ||
$this->allowed_types = $this->genarateAllowedTypes($types); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get allowed types for LinkField | ||
*/ | ||
public function getAllowedTypes(): array | ||
{ | ||
return $this->allowed_types; | ||
} | ||
|
||
/** | ||
* Validate types that they are subclasses of Link | ||
*/ | ||
private function validateTypes(array $types): bool | ||
{ | ||
$validClasses = []; | ||
foreach ($types as $type) { | ||
if (is_subclass_of($type, Link::class)) { | ||
$validClasses[] = $type; | ||
} | ||
} | ||
|
||
return count($validClasses) > 0; | ||
} | ||
|
||
/** | ||
* The method returns an associational array converted to a JSON string, | ||
* of available link types with additional parameters necessary | ||
* for full-fledged work on the client side. | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function getTypesProps(): string | ||
{ | ||
$types = []; | ||
$typeDefinitions = $this->genarateAllowedTypes(); | ||
|
||
foreach ($typeDefinitions as $key => $class) { | ||
$types[$key] = Injector::inst()->get($class); | ||
} | ||
|
||
$typesList = []; | ||
|
||
array_map(function (Link $type, string $key) use (&$typesList) { | ||
$typesList[$key] = [ | ||
'key' => $key, | ||
'title' => $type->LinkTypeTile(), | ||
'handlerName' => $type->LinkTypeHandlerName(), | ||
]; | ||
}, $types, array_keys($types)); | ||
|
||
return json_encode($typesList); | ||
} | ||
|
||
/** | ||
* Generate allowed types with key => value pair | ||
* Example: ['cms' => SiteTreeLink::class] | ||
*/ | ||
private function genarateAllowedTypes(array $types = []): array | ||
{ | ||
$typeDefinitions = !empty($types) ? $types : $this->getAllowedTypes(); | ||
|
||
if (empty($typeDefinitions)) { | ||
return LinkTypeService::create()->generateAllLinkTypes(); | ||
} | ||
|
||
$result = array(); | ||
foreach ($typeDefinitions as $class) { | ||
if (is_subclass_of($class, Link::class)) { | ||
$type = Injector::inst()->get($class)->getShortCode(); | ||
$result[$type] = $class; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
} |
Oops, something went wrong.