Skip to content

Commit

Permalink
Merge pull request #79 from elkmod/feature/landing-pages
Browse files Browse the repository at this point in the history
Feature: Added support for CMS landing pages
  • Loading branch information
elkmod authored Jul 2, 2021
2 parents 8841b31 + 1b194aa commit 3a73c84
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 62 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
CHANGELOG for Shopware PWA
===================

### 0.3.1

> CMS landing pages are now supported
**Added**
* Constant `LANDING_PAGE_ROUTE` in `SwagShopwarePwa\Pwa\Controller\PageController`
* Class `SwagShopwarePwa\Pwa\PageLoader\LandingPageLoader`
* Class `SwagShopwarePwa\Pwa\PageResult\Landing\LandingPageResult`
* Class `SwagShopwarePwa\Pwa\PageResult\Landing\LandingPageResultHydrator`
* PHPUnit test groups
* `pwa-page-product`
* `pwa-page-category`
* `pwa-page-landing`
* `pwa-page-routing`

### 0.3.0

> PHP level has been increased to PHP 7.4
Expand Down
8 changes: 6 additions & 2 deletions src/Pwa/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class PageController extends AbstractController

const NAVIGATION_PAGE_ROUTE = 'frontend.navigation.page';

const LANDING_PAGE_ROUTE = 'frontend.landing.page';

/**
* @var PageLoaderContextBuilderInterface
*/
Expand Down Expand Up @@ -79,7 +81,7 @@ public function __construct(PageLoaderContextBuilderInterface $pageLoaderContext
* property="resourceType",
* description="Type of page that was fetched. Indicates whether it is a product page or a category page",
* type="string",
* enum={"frontend.detail.page", "frontend.navigation.page"}
* enum={"frontend.detail.page", "frontend.navigation.page", "frontend.landing.page"}
* ),
* @OA\Property(
* property="resourceIdentifier",
Expand Down Expand Up @@ -136,6 +138,9 @@ public function __construct(PageLoaderContextBuilderInterface $pageLoaderContext
* description="The category associated with the loaded page.",
* ref="#/components/schemas/category_flat"
* )
* ),
* @OA\Schema(
* description="A landing page result contains no specific fields."
* )
* }
* )
Expand Down Expand Up @@ -190,7 +195,6 @@ private function getPageLoader(PageLoaderContext $pageLoaderContext): ?PageLoade
*/
private function getPageResult(PageLoaderInterface $pageLoader, PageLoaderContext $pageLoaderContext): AbstractPageResult
{

/** @var AbstractPageResult $pageResult */
$pageResult = $pageLoader->load($pageLoaderContext);

Expand Down
3 changes: 2 additions & 1 deletion src/Pwa/PageLoader/Context/PathResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class PathResolver implements PathResolverInterface
{
private const MATCH_MAP = [
PageController::NAVIGATION_PAGE_ROUTE => '/^\/?navigation\/([a-f0-9]{32})$/',
PageController::PRODUCT_PAGE_ROUTE => '/^\/?detail\/([a-f0-9]{32})$/'
PageController::PRODUCT_PAGE_ROUTE => '/^\/?detail\/([a-f0-9]{32})$/',
PageController::LANDING_PAGE_ROUTE => '/^\/?landingPage\/([a-f0-9]{32})$/'
];

private const ROOT_ROUTE_NAME = PageController::NAVIGATION_PAGE_ROUTE;
Expand Down
61 changes: 61 additions & 0 deletions src/Pwa/PageLoader/LandingPageLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types=1);

namespace SwagShopwarePwa\Pwa\PageLoader;

use Shopware\Core\Content\LandingPage\SalesChannel\AbstractLandingPageRoute;
use Shopware\Core\Content\LandingPage\SalesChannel\LandingPageRoute;
use SwagShopwarePwa\Pwa\PageLoader\Context\PageLoaderContext;
use SwagShopwarePwa\Pwa\PageResult\Landing\LandingPageResult;
use SwagShopwarePwa\Pwa\PageResult\Landing\LandingPageResultHydrator;

/**
* This class loads a static landing page. Landing pages behave the same way as CMS pages, but do not have a breadcrumb.
*
* @package SwagShopwarePwa\Pwa\PageLoader
*/
class LandingPageLoader implements PageLoaderInterface
{
private const RESOURCE_TYPE = 'frontend.landing.page';

/**
* @var LandingPageRoute
*/
private $landingPageRoute;

/**
* @var LandingPageResultHydrator
*/
private $resultHydrator;

public function __construct(AbstractLandingPageRoute $landingPageRoute, LandingPageResultHydrator $resultHydrator)
{
$this->landingPageRoute = $landingPageRoute;
$this->resultHydrator = $resultHydrator;
}

public function getResourceType(): string
{
return self::RESOURCE_TYPE;
}

/**
* @param PageLoaderContext $pageLoaderContext
*
* @return LandingPageResult
*/
public function load(PageLoaderContext $pageLoaderContext): LandingPageResult
{
$landingPageResult = $this->landingPageRoute->load(
$pageLoaderContext->getResourceIdentifier(),
$pageLoaderContext->getRequest(),
$pageLoaderContext->getContext()
);

$pageResult = $this->resultHydrator->hydrate(
$pageLoaderContext,
$landingPageResult->getLandingPage()->getCmsPage() ?? null
);

return $pageResult;
}
}
9 changes: 0 additions & 9 deletions src/Pwa/PageLoader/ProductPageLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@

use Shopware\Core\Content\Product\Exception\ProductNumberNotFoundException;
use Shopware\Core\Content\Product\SalesChannel\Detail\AbstractProductDetailRoute;
use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductDefinition;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\RequestCriteriaBuilder;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use SwagShopwarePwa\Pwa\PageLoader\Context\PageLoaderContext;
use SwagShopwarePwa\Pwa\PageResult\Product\ProductPageResult;
use SwagShopwarePwa\Pwa\PageResult\Product\ProductPageResultHydrator;
Expand Down Expand Up @@ -80,11 +76,6 @@ public function load(PageLoaderContext $pageLoaderContext): ProductPageResult
$pageLoaderContext->getContext()->getContext()
);

$criteria->addFilter(
new ProductAvailableFilter($pageLoaderContext->getContext()->getSalesChannel()->getId()),
new EqualsFilter('active', 1)
);

$result = $this->productRoute->load(
$pageLoaderContext->getResourceIdentifier(),
$pageLoaderContext->getRequest(),
Expand Down
11 changes: 11 additions & 0 deletions src/Pwa/PageResult/Landing/LandingPageResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace SwagShopwarePwa\Pwa\PageResult\Landing;

use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Content\Cms\CmsPageEntity;
use SwagShopwarePwa\Pwa\PageResult\AbstractPageResult;

class LandingPageResult extends AbstractPageResult
{
}
22 changes: 22 additions & 0 deletions src/Pwa/PageResult/Landing/LandingPageResultHydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

namespace SwagShopwarePwa\Pwa\PageResult\Landing;

use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Content\Cms\CmsPageEntity;
use SwagShopwarePwa\Pwa\PageLoader\Context\PageLoaderContext;
use SwagShopwarePwa\Pwa\PageResult\AbstractPageResultHydrator;

class LandingPageResultHydrator extends AbstractPageResultHydrator
{
public function hydrate(PageLoaderContext $pageLoaderContext, ?CmsPageEntity $cmsPageEntity): LandingPageResult
{
$pageResult = new LandingPageResult();

$pageResult->setCmsPage($cmsPageEntity);
$pageResult->setResourceType($pageLoaderContext->getResourceType());
$pageResult->setResourceIdentifier($pageLoaderContext->getResourceIdentifier());

return $pageResult;
}
}
9 changes: 9 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
<tag name="pwa.page_loader"/>
</service>

<service id="SwagShopwarePwa\Pwa\PageLoader\LandingPageLoader">
<argument type="service" id="Shopware\Core\Content\LandingPage\SalesChannel\LandingPageRoute" />
<argument type="service" id="SwagShopwarePwa\Pwa\PageResult\Landing\LandingPageResultHydrator" />
<tag name="pwa.page_loader"/>
</service>


<!-- Page hydrators -->

Expand All @@ -69,6 +75,9 @@
<service id="SwagShopwarePwa\Pwa\PageResult\Navigation\NavigationPageResultHydrator"
parent="SwagShopwarePwa\Pwa\PageResult\AbstractPageResultHydrator"/>

<service id="SwagShopwarePwa\Pwa\PageResult\Landing\LandingPageResultHydrator"
parent="SwagShopwarePwa\Pwa\PageResult\AbstractPageResultHydrator"/>


<!-- Repositories -->
<service id="SwagShopwarePwa\Pwa\Entity\SalesChannelRoute\SalesChannelRouteRepository">
Expand Down
Loading

0 comments on commit 3a73c84

Please sign in to comment.