-
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 14, 2023
1 parent
55208f8
commit 4df1552
Showing
25 changed files
with
487 additions
and
411 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,102 @@ | ||
<?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 | ||
* @param string[] $types | ||
*/ | ||
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 | ||
* @param string[] $types | ||
*/ | ||
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 | ||
{ | ||
$typesList = []; | ||
$typeDefinitions = $this->genarateAllowedTypes(); | ||
foreach ($typeDefinitions as $key => $class) { | ||
/** @var Link $type */ | ||
$type = Injector::inst()->get($class); | ||
$typesList[$key] = [ | ||
'key' => $key, | ||
'title' => $type->i18n_singular_name(), | ||
'handlerName' => $type->LinkTypeHandlerName(), | ||
]; | ||
} | ||
|
||
return json_encode($typesList); | ||
} | ||
|
||
/** | ||
* Generate allowed types with key => value pair | ||
* Example: ['cms' => SiteTreeLink::class] | ||
* @param string[] $types | ||
*/ | ||
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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.