Skip to content

Commit

Permalink
Merge pull request #284 from sherlockode/feature/improve-data-retrieval
Browse files Browse the repository at this point in the history
Improve data retrieval
  • Loading branch information
juchi authored Apr 25, 2023
2 parents b5f6289 + ee912bc commit 6111d47
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 13 deletions.
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<argument type="service" id="doctrine.orm.entity_manager"/>
<argument type="service" id="sherlockode_advanced_content.url_builder_manager"/>
<argument type="service" id="sherlockode_advanced_content.user_provider"/>
<argument type="service" id="sherlockode_advanced_content.scope_handler"/>
<argument>form_div_layout.html.twig</argument>
<tag name="twig.extension"/>
</service>
Expand Down
22 changes: 9 additions & 13 deletions Scope/LocaleScopeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ public function getDataFromScope(ScopeInterface $scope): array
}

/**
* @param array|ScopableInterface[] $entities
*
* @return ScopableInterface|null
* @return ScopeInterface|null
*/
public function filterEntityForCurrentScope(array $entities): ?ScopableInterface
public function getCurrentScope(): ?ScopeInterface
{
if (!$this->configurationManager->isScopesEnabled()) {
return null;
}

if (method_exists($this->requestStack, 'getMainRequest')) {
// SF >= 5.3
$mainRequest = $this->requestStack->getMainRequest();
Expand All @@ -88,14 +90,8 @@ public function filterEntityForCurrentScope(array $entities): ?ScopableInterface
return null;
}

foreach ($entities as $entity) {
foreach ($entity->getScopes() as $scope) {
if ($scope->getLocale() === $mainRequest->getLocale()) {
return $entity;
}
}
}

return null;
return $this->em->getRepository($this->configurationManager->getEntityClass('scope'))->findOneBy([
'locale' => $mainRequest->getLocale(),
]);
}
}
27 changes: 27 additions & 0 deletions Scope/ScopeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,31 @@ public function getEntityForCurrentScope(string $entityCode, array $criteria): ?
$this->em->getRepository($this->configurationManager->getEntityClass($entityCode))->findBy($criteria)
);
}

/**
* @param array|ScopableInterface[] $entities
*
* @return ScopableInterface|null
*/
public function filterEntityForCurrentScope(array $entities): ?ScopableInterface
{
if (!$this->configurationManager->isScopesEnabled()) {
return count($entities) > 0 ? reset($entities) : null;
}

$currentScope = $this->getCurrentScope();
if ($currentScope === null) {
return null;
}

foreach ($entities as $entity) {
foreach ($entity->getScopes() as $scope) {
if ($scope->getId() === $currentScope->getId()) {
return $entity;
}
}
}

return null;
}
}
5 changes: 5 additions & 0 deletions Scope/ScopeHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ public function getEntityForCurrentScope(string $entityCode, array $criteria): ?
* @return ScopableInterface|null
*/
public function filterEntityForCurrentScope(array $entities): ?ScopableInterface;

/**
* @return ScopeInterface|null
*/
public function getCurrentScope(): ?ScopeInterface;
}
21 changes: 21 additions & 0 deletions Twig/Extension/ContentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Doctrine\ORM\EntityManager;
use Sherlockode\AdvancedContentBundle\Manager\ElementManager;
use Sherlockode\AdvancedContentBundle\Manager\UrlBuilderManager;
use Sherlockode\AdvancedContentBundle\Model\ContentInterface;
use Sherlockode\AdvancedContentBundle\Model\VersionInterface;
use Sherlockode\AdvancedContentBundle\Scope\ScopeHandlerInterface;
use Sherlockode\AdvancedContentBundle\User\UserProviderInterface;
use Symfony\Component\Form\FormView;
use Twig\Environment;
Expand Down Expand Up @@ -39,6 +41,11 @@ class ContentExtension extends AbstractExtension
*/
private $userProvider;

/**
* @var ScopeHandlerInterface
*/
private $scopeHandler;

/**
* @var string
*/
Expand All @@ -50,6 +57,7 @@ class ContentExtension extends AbstractExtension
* @param EntityManager $em
* @param UrlBuilderManager $urlBuilderManager
* @param UserProviderInterface $userProvider
* @param ScopeHandlerInterface $scopeHandler
* @param string $baseFormTheme
*/
public function __construct(
Expand All @@ -58,13 +66,15 @@ public function __construct(
EntityManager $em,
UrlBuilderManager $urlBuilderManager,
UserProviderInterface $userProvider,
ScopeHandlerInterface $scopeHandler,
$baseFormTheme
) {
$this->elementManager = $elementManager;
$this->twig = $twig;
$this->em = $em;
$this->urlBuilderManager = $urlBuilderManager;
$this->userProvider = $userProvider;
$this->scopeHandler = $scopeHandler;
$this->baseFormTheme = $baseFormTheme;
}

Expand All @@ -88,6 +98,7 @@ public function getFunctions()
new TwigFunction('acb_get_element_attributes', [$this, 'getElementAttributes']),
new TwigFunction('acb_get_json_form', [$this, 'getJsonForm']),
new TwigFunction('acb_get_version_user_name', [$this, 'getVersionUserName']),
new TwigFunction('acb_get_content_by_slug', [$this, 'getContentBySlug']),
];
}

Expand Down Expand Up @@ -378,4 +389,14 @@ public function getVersionUserName(VersionInterface $version): string
{
return $this->userProvider->getUserName($version->getUserId());
}

/**
* @param string $slug
*
* @return ContentInterface|null
*/
public function getContentBySlug(string $slug): ?ContentInterface
{
return $this->scopeHandler->getEntityForCurrentScope('content', ['slug' => $slug]);
}
}

0 comments on commit 6111d47

Please sign in to comment.