Skip to content

Commit

Permalink
init.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkostevich committed Sep 8, 2020
0 parents commit 64d25ad
Show file tree
Hide file tree
Showing 13 changed files with 911 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Plugin/AddSeoNameToCategoryCollectionPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Copyright © MageWorx. All rights reserved.
* See LICENSE.txt for license details.
*/

namespace MageWorx\SeoXTemplatesGraphQl\Plugin;

use GraphQL\Language\AST\FieldNode;
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
use Magento\CatalogGraphQl\Model\AttributesJoiner;

class AddSeoNameToCategoryCollectionPlugin
{
/**
* @var \MageWorx\SeoXTemplates\Helper\Data
*/
protected $helperData;

/**
* AddSeoNameToCategoryCollectionPlugin constructor.
*
* @param \MageWorx\SeoXTemplates\Helper\Data $helperData
*/
public function __construct(
\MageWorx\SeoXTemplates\Helper\Data $helperData
) {
$this->helperData = $helperData;
}

/**
* Adds "category_seo_name" attribute to category collection if needed
*
* @param AttributesJoiner $subject
* @param $result
* @param FieldNode $fieldNode
* @param AbstractCollection $collection
*/
public function afterJoin(
AttributesJoiner $subject,
$result,
FieldNode $fieldNode,
AbstractCollection $collection
): void {
if ($collection instanceof \Magento\Catalog\Model\ResourceModel\Category\Collection) {
if ($this->helperData->isUseCategorySeoName()) {
if ($collection->isAttributeAdded('name')) {
$collection->addAttributeToSelect('category_seo_name');
}
}
}
}
}
54 changes: 54 additions & 0 deletions Plugin/AddSeoNameToProductCollectionPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright © MageWorx. All rights reserved.
* See LICENSE.txt for license details.
*/

namespace MageWorx\SeoXTemplatesGraphQl\Plugin;

use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Framework\Api\SearchCriteriaInterface;

class AddSeoNameToProductCollectionPlugin
{
/**
* @var \MageWorx\SeoXTemplates\Helper\Data
*/
protected $helperData;

/**
* AddSeoNameToProductCollectionPlugin constructor.
*
* @param \MageWorx\SeoXTemplates\Helper\Data $helperData
*/
public function __construct(
\MageWorx\SeoXTemplates\Helper\Data $helperData
) {
$this->helperData = $helperData;
}

/**
* @param CollectionProcessorInterface $subject
* @param Collection $result
* @param Collection $collection
* @param SearchCriteriaInterface $searchCriteria
* @param array $attributeNames
* @return Collection
*/
public function afterProcess(
CollectionProcessorInterface $subject,
Collection $result,
Collection $collection,
SearchCriteriaInterface $searchCriteria,
array $attributeNames
) {
if ($this->helperData->isUseProductSeoName()) {
if ($result->isAttributeAdded('name')) {
$result->addAttributeToSelect('product_seo_name');
}
}

return $result;
}
}
114 changes: 114 additions & 0 deletions Plugin/ModifyCategoryDescriptionPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
/**
* Copyright © MageWorx. All rights reserved.
* See LICENSE.txt for license details.
*/

namespace MageWorx\SeoXTemplatesGraphQl\Plugin;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use MageWorx\SeoXTemplates\Model\DynamicRenderer\Category as Renderer;

class ModifyCategoryDescriptionPlugin
{
/**
* Filter object
*
* @var \MageWorx\SeoXTemplates\Model\DynamicRenderer\Category
*/
protected $dynamicRenderer;

/**
* @var \MageWorx\SeoXTemplates\Helper\Data
*/
protected $helperData;

/**
* ModifyCategoryDescriptionPlugin constructor.
*
* @param \MageWorx\SeoXTemplates\Helper\Data $helperData
* @param Renderer $renderer
*/
public function __construct(
\MageWorx\SeoXTemplates\Helper\Data $helperData,
Renderer $renderer
) {
$this->helperData = $helperData;
$this->dynamicRenderer = $renderer;
}

/**
* @param \Magento\CatalogGraphQl\Model\Resolver\Category\CategoryHtmlAttribute $subject
* @param string|null $result
* @param Field $field
* @param $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
*/
public function afterResolve(
$subject,
$result,
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (!$result) {
return $result;
}

/** @var \Magento\Catalog\Model\Category $category */
$category = $value['model'];
$fieldName = $field->getName();

if ($this->out($category, $fieldName)) {
return $result;
}

if (!$this->isCurrentEntity($category, $info)) {
return $result;
}

if ($this->dynamicRenderer->modifyCategoryDescription($category)) {
return $category->getData('description');
}

return $result;
}

/**
* @param \Magento\Catalog\Model\Category $category
* @param ResolveInfo $info
* @return bool
*/
protected function isCurrentEntity($category, $info)
{
$variables = $info->variableValues;

return !empty($variables['_filter_0']['category_url_path']['eq'])
&& $variables['_filter_0']['category_url_path']['eq'] === $category->getUrlPath();
}

/**
* Check if go out
*
* @param \Magento\Catalog\Model\Category $category
* @param string $fieldName
* @return boolean
*/
protected function out($category, $fieldName)
{
if (!is_object($category)) {
return true;
}

if ($fieldName !== 'description') {
return true;
}

return false;
}
}
96 changes: 96 additions & 0 deletions Plugin/ModifyCategoryNamePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* Copyright © MageWorx. All rights reserved.
* See LICENSE.txt for license details.
*/

namespace MageWorx\SeoXTemplatesGraphQl\Plugin;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use MageWorx\SeoXTemplates\Model\DynamicRenderer\Category as Renderer;

class ModifyCategoryNamePlugin
{
/**
* @var \MageWorx\SeoXTemplates\Helper\Data
*/
protected $helperData;

/**
* ModifyCategoryNamePlugin constructor.
*
* @param \MageWorx\SeoXTemplates\Helper\Data $helperData
*/
public function __construct(
\MageWorx\SeoXTemplates\Helper\Data $helperData
) {
$this->helperData = $helperData;
}

/**
* @param \MageWorx\SeoAllGraphQl\Model\Resolver\Category\SeoRenderedElement $subject
* @param string|null $result
* @param Field $field
* @param $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
*
* @return strung
*/
public function afterResolve(
$subject,
$result,
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (!$result) {
return $result;
}

/** @var \Magento\Catalog\Model\Category $category */
$category = $value['model'];
$fieldName = $field->getName();

if ($this->out($category, $fieldName)) {
return $result;
}

$categorySeoName = $category->getData('category_seo_name');
$category->setData('name', $categorySeoName);

return $categorySeoName;
}

/**
* Check if go out
*
* @param \Magento\Catalog\Model\Category $category
* @param string $fieldName
* @return boolean
*/
protected function out($category, $fieldName)
{
if (!is_object($category)) {
return true;
}

if ($fieldName !== 'name') {
return true;
}

if (!$this->helperData->isUseCategorySeoName()) {
return true;
}

if (empty($category->getData('category_seo_name'))) {
return true;
}

return false;
}
}
Loading

0 comments on commit 64d25ad

Please sign in to comment.