2020-11-13 01:50:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace SilverStripe\CMS\GraphQL;
|
|
|
|
|
|
|
|
use GraphQL\Type\Definition\ResolveInfo;
|
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
|
|
|
use SilverStripe\Core\Config\Configurable;
|
|
|
|
use SilverStripe\Core\Injector\Injectable;
|
|
|
|
use SilverStripe\GraphQL\Schema\DataObject\Plugin\QueryFilter\QueryFilter;
|
|
|
|
use SilverStripe\GraphQL\Schema\Exception\SchemaBuilderException;
|
|
|
|
use SilverStripe\GraphQL\Schema\Field\ModelQuery;
|
|
|
|
use SilverStripe\GraphQL\Schema\Interfaces\ModelQueryPlugin;
|
|
|
|
use SilverStripe\GraphQL\Schema\Schema;
|
2021-09-06 23:34:52 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2020-11-13 01:50:44 +01:00
|
|
|
use SilverStripe\ORM\DataList;
|
|
|
|
|
|
|
|
if (!interface_exists(ModelQueryPlugin::class)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
class LinkablePlugin implements ModelQueryPlugin
|
|
|
|
{
|
|
|
|
use Configurable;
|
|
|
|
use Injectable;
|
|
|
|
|
|
|
|
const IDENTIFIER = 'getByLink';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
* @config
|
|
|
|
*/
|
2021-09-06 23:34:52 +02:00
|
|
|
private static $single_field_name = 'link';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
* @config
|
|
|
|
*/
|
|
|
|
private static $list_field_name = 'links';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $resolver = [__CLASS__, 'applyLinkFilter'];
|
2020-11-13 01:50:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getIdentifier(): string
|
|
|
|
{
|
|
|
|
return self::IDENTIFIER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ModelQuery $query
|
|
|
|
* @param Schema $schema
|
|
|
|
* @param array $config
|
|
|
|
*/
|
|
|
|
public function apply(ModelQuery $query, Schema $schema, array $config = []): void
|
|
|
|
{
|
|
|
|
$class = $query->getModel()->getSourceClass();
|
|
|
|
// Only site trees have the get_by_link capability
|
|
|
|
if ($class !== SiteTree::class && !is_subclass_of($class, SiteTree::class)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-06 23:34:52 +02:00
|
|
|
$singleFieldName = $this->config()->get('single_field_name');
|
|
|
|
$listFieldName = $this->config()->get('list_field_name');
|
|
|
|
$fieldName = $query->isList() ? $listFieldName : $singleFieldName;
|
|
|
|
$type = $query->isList() ? '[String]' : 'String';
|
|
|
|
$query->addArg($fieldName, $type);
|
|
|
|
$query->addResolverAfterware(
|
|
|
|
$config['resolver'] ?? static::config()->get('resolver')
|
2020-11-13 01:50:44 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $context
|
2021-09-06 23:34:52 +02:00
|
|
|
* @return callable
|
2020-11-13 01:50:44 +01:00
|
|
|
*/
|
2021-09-06 23:34:52 +02:00
|
|
|
public static function applyLinkFilter($obj, array $args, array $context, ResolveInfo $info)
|
|
|
|
{
|
|
|
|
$singleFieldName = static::config()->get('single_field_name');
|
|
|
|
$listFieldName = static::config()->get('list_field_name');
|
|
|
|
$filterLink = $args['filter'][$singleFieldName] ?? ($args['filter'][$listFieldName] ?? null);
|
|
|
|
$argLink = $args[$singleFieldName] ?? ($args[$listFieldName] ?? null);
|
|
|
|
$linkData = $filterLink ?: $argLink;
|
|
|
|
if (!$linkData) {
|
|
|
|
return $obj;
|
2020-11-13 01:50:44 +01:00
|
|
|
}
|
2021-09-06 23:34:52 +02:00
|
|
|
// Normalise to an array for both cases. The readOne operation will get
|
|
|
|
// ->first() run on it by the firstResult plugin.
|
|
|
|
$links = is_array($linkData) ? $linkData : [$linkData];
|
|
|
|
|
|
|
|
$result = ArrayList::create();
|
2020-11-13 01:50:44 +01:00
|
|
|
|
2021-09-06 23:34:52 +02:00
|
|
|
foreach ($links as $link) {
|
|
|
|
$page = SiteTree::get_by_link($link);
|
|
|
|
if ($page) {
|
|
|
|
$result->push($page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
2020-11-13 01:50:44 +01:00
|
|
|
}
|
|
|
|
}
|