Skip to content

Commit

Permalink
get parent-children from own query
Browse files Browse the repository at this point in the history
  • Loading branch information
DerCalli committed Nov 27, 2019
1 parent 82dde46 commit 9fe03ea
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.idea

.DS_Store

vendor
src/Generated
composer.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

namespace FondOfSpryker\Yves\CatalogCategoryWidget;

use FondOfSpryker\Yves\CatalogCategoryWidget\Dependency\Client\CatalogPageWidgetToCategoryStoreStorageClientBridge;
use FondOfSpryker\Yves\CatalogCategoryWidget\Dependency\Client\CatalogCategoryWidgetToCatalogCategoryClientBridge;
use FondOfSpryker\Yves\CatalogCategoryWidget\Dependency\Client\CatalogCategoryWidgetToCategoryStoreStorageClientBridge;
use Spryker\Shared\Kernel\Store;
use Spryker\Yves\Kernel\AbstractBundleDependencyProvider;
use Spryker\Yves\Kernel\Container;

class CatalogCategoryWidgetDependencyProvider extends AbstractBundleDependencyProvider
{
public const CLIENT_CATEGORY_STORE_STORAGE = 'CLIENT_CATEGORY_STORE_STORAGE';
public const CLIENT_CATALOG_CATEGORY = 'CLIENT_CATALOG_CATEGORY';
public const STORE = 'STORE';

/**
Expand All @@ -35,7 +37,9 @@ public function provideDependencies(Container $container)
protected function addCategoryStoreStorageClient(Container $container): Container
{
$container[static::CLIENT_CATEGORY_STORE_STORAGE] = function (Container $container) {
return new CatalogPageWidgetToCategoryStoreStorageClientBridge($container->getLocator()->categoryStoreStorage()->client());
return new CatalogCategoryWidgetToCategoryStoreStorageClientBridge(
$container->getLocator()->categoryStoreStorage()->client()
);
};

return $container;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

namespace FondOfSpryker\Yves\CatalogCategoryWidget;

use FondOfSpryker\Yves\CatalogCategoryWidget\Dependency\Client\CatalogPageWidgetToCategoryStoreStorageClientInterface;
use FondOfSpryker\Yves\CatalogCategoryWidget\Dependency\Client\CatalogCategoryWidgetToCatalogCategoryClientInterface;
use FondOfSpryker\Yves\CatalogCategoryWidget\Dependency\Client\CatalogCategoryWidgetToCategoryStoreStorageClientInterface;
use Spryker\Shared\Kernel\Store;
use Spryker\Yves\Kernel\AbstractFactory;

class CatalogCategoryWidgetFactory extends AbstractFactory
{
/**
* @return CatalogCategoryWidgetToCategoryStoreStorageClientInterface;
* @throws
*
* @return CatalogPageWidgetToCategoryStoreStorageClientInterface;
*/
public function getCategoryStoreStorageClient(): CatalogPageWidgetToCategoryStoreStorageClientInterface
public function getCategoryStoreStorageClient(): CatalogCategoryWidgetToCategoryStoreStorageClientInterface
{
return $this->getProvidedDependency(CatalogCategoryWidgetDependencyProvider::CLIENT_CATEGORY_STORE_STORAGE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Generated\Shared\Transfer\CategoryNodeStorageTransfer;
use Spryker\Client\CategoryStorage\CategoryStorageClientInterface;

class CatalogPageWidgetToCategoryStoreStorageClientBridge implements CatalogPageWidgetToCategoryStoreStorageClientInterface
class CatalogCategoryWidgetToCategoryStoreStorageClientBridge implements CatalogCategoryWidgetToCategoryStoreStorageClientInterface
{
/**
* @var \Spryker\Client\CategoryStorage\CategoryStorageClientInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Generated\Shared\Transfer\CategoryNodeStorageTransfer;

interface CatalogPageWidgetToCategoryStoreStorageClientInterface
interface CatalogCategoryWidgetToCategoryStoreStorageClientInterface
{
/**
* @param int $idCategoryStorageNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace FondOfSpryker\Yves\CatalogCategoryWidget\Widget;

use FondOfSpryker\Yves\CatalogCategoryWidget\Dependency\Plugin\CategoryWidget\CategoryBlockWidgetPluginInterface;
use Generated\Shared\Transfer\CategoryNodeStorageTransfer;
use Spryker\Yves\Kernel\Widget\AbstractWidgetPlugin;

/**
* @method \FondOfSpryker\Yves\CatalogCategoryWidget\CatalogCategoryWidgetFactory getFactory()
* @method \FondOfSpryker\Client\CatalogCategoryWidget\CatalogCategoryWidgetClientInterface getClient()
*/
class CategoryBlockWidgetPlugin extends AbstractWidgetPlugin implements CategoryBlockWidgetPluginInterface
{
Expand All @@ -25,6 +27,10 @@ public function initialize(int $idCategory, string $locale, bool $render = true)
->getCategoryStoreStorageClient()
->getCategoryNodeById($idCategory, $locale);

if (!$categoryNode) {
return;
}

$storeTransfer = $this->getFactory()->getStore();
$storeName = explode("_", $storeTransfer->getStoreName())[0];
$this->addParameter('storename', strtolower($storeName));
Expand All @@ -38,7 +44,7 @@ public function initialize(int $idCategory, string $locale, bool $render = true)
return;
}

if (count($categoryNode->getChildren()) > 0) {
if ($categoryNode->getChildren()->count() > 0) {
$this->addParameter('categories', $categoryNode->getChildren());
$this->addParameter('parentCategory', $categoryNode);
$this->addParameter('idCategory', $idCategory);
Expand All @@ -47,9 +53,16 @@ public function initialize(int $idCategory, string $locale, bool $render = true)
return;
}

if (count($categoryNode->getChildren()) === 0) {
$this->addParameter('categories', $categoryNode->getParents()[0]->getChildren());
$this->addParameter('parentCategory', $categoryNode->getParents()[0]);
if ($categoryNode->getChildren()->count() === 0 && $categoryNode->getParents()->count() === 1) {
$parent = $this->getFullParent($categoryNode, $locale);
$children = $parent->getChildren();

foreach($parent->getChildren() as $child) {
$a = $child;
}

$this->addParameter('categories', $parent->getChildren());
$this->addParameter('parentCategory', $parent);
$this->addParameter('idCategory', $idCategory);
$this->addParameter('render', $render);

Expand All @@ -58,11 +71,28 @@ public function initialize(int $idCategory, string $locale, bool $render = true)
}

/**
* Specification:
* - Returns the name of the widget as it's used in templates.
* @param CategoryNodeStorageTransfer $categoryNodeStorageTransfer
* @param string $locale
*
* @api
* @return array
*
* @throws
*/
protected function getFullParent(CategoryNodeStorageTransfer $categoryNodeStorageTransfer, string $locale): CategoryNodeStorageTransfer
{
if ($categoryNodeStorageTransfer->getParents()->count() !== 1) {
return $categoryNodeStorageTransfer;
}

/** @var CategoryNodeStorageTransfer $parent */
$parent = $categoryNodeStorageTransfer->getParents()[0];

return $this->getFactory()
->getCategoryStoreStorageClient()
->getCategoryNodeById($parent->getNodeId(), $locale);
}

/**
* @return string
*/
public static function getName()
Expand All @@ -71,11 +101,6 @@ public static function getName()
}

/**
* Specification:
* - Returns the the template file path to render the widget.
*
* @api
*
* @return string
*/
public static function getTemplate()
Expand Down

0 comments on commit 9fe03ea

Please sign in to comment.