-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1305 from bolt/feature/filter-unpublished-content…
…-api API shows published and viewless: false content only
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 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 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,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bolt\Api\Extensions; | ||
|
||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
use Bolt\Configuration\Config; | ||
use Bolt\Entity\Content; | ||
use Bolt\Enum\Statuses; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
final class ContentExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface | ||
{ | ||
/** @var Config */ | ||
private $config; | ||
|
||
public function __construct(Config $config) | ||
{ | ||
$this->config = $config; | ||
} | ||
|
||
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?string $operationName = null): void | ||
{ | ||
if ($resourceClass !== Content::class) { | ||
return; | ||
} | ||
|
||
$this->requirePublishedContent($queryBuilder); | ||
} | ||
|
||
public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, ?string $operationName = null, array $context = []): void | ||
{ | ||
if ($resourceClass !== Content::class) { | ||
return; | ||
} | ||
|
||
$this->requirePublishedContent($queryBuilder); | ||
$this->excludeViewlessContent($queryBuilder); | ||
} | ||
|
||
private function requirePublishedContent(QueryBuilder $queryBuilder): void | ||
{ | ||
$rootAlias = $queryBuilder->getRootAliases()[0]; | ||
$queryBuilder->andWhere(sprintf('%s.status = :status', $rootAlias)); | ||
$queryBuilder->setParameter('status', Statuses::PUBLISHED); | ||
} | ||
|
||
private function excludeViewlessContent(QueryBuilder $queryBuilder): void | ||
{ | ||
$rootAlias = $queryBuilder->getRootAliases()[0]; | ||
|
||
$viewlessContentTypes = $this->config->get('contenttypes')->filter(function ($ct) { | ||
return $ct->get('viewless', false); | ||
})->map(function ($ct) { | ||
return $ct->get('slug'); | ||
})->toArray(); | ||
|
||
$queryBuilder->andWhere(sprintf('%s.contentType NOT IN (:cts)', $rootAlias)); | ||
$queryBuilder->setParameter('cts', $viewlessContentTypes); | ||
} | ||
} |