diff --git a/Plugin/AddSeoNameToCategoryCollectionPlugin.php b/Plugin/AddSeoNameToCategoryCollectionPlugin.php new file mode 100755 index 0000000..8cd2e2e --- /dev/null +++ b/Plugin/AddSeoNameToCategoryCollectionPlugin.php @@ -0,0 +1,53 @@ +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'); + } + } + } + } +} \ No newline at end of file diff --git a/Plugin/AddSeoNameToProductCollectionPlugin.php b/Plugin/AddSeoNameToProductCollectionPlugin.php new file mode 100755 index 0000000..e729de0 --- /dev/null +++ b/Plugin/AddSeoNameToProductCollectionPlugin.php @@ -0,0 +1,54 @@ +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; + } +} \ No newline at end of file diff --git a/Plugin/ModifyCategoryDescriptionPlugin.php b/Plugin/ModifyCategoryDescriptionPlugin.php new file mode 100755 index 0000000..ef584a2 --- /dev/null +++ b/Plugin/ModifyCategoryDescriptionPlugin.php @@ -0,0 +1,114 @@ +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; + } +} \ No newline at end of file diff --git a/Plugin/ModifyCategoryNamePlugin.php b/Plugin/ModifyCategoryNamePlugin.php new file mode 100755 index 0000000..62eec9a --- /dev/null +++ b/Plugin/ModifyCategoryNamePlugin.php @@ -0,0 +1,96 @@ +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; + } +} \ No newline at end of file diff --git a/Plugin/ModifyCategoryParamsPlugin.php b/Plugin/ModifyCategoryParamsPlugin.php new file mode 100755 index 0000000..c402869 --- /dev/null +++ b/Plugin/ModifyCategoryParamsPlugin.php @@ -0,0 +1,127 @@ + 'modifyCategoryTitle', + 'meta_description' => 'modifyCategoryMetaDescription', + 'meta_keywords' => 'modifyCategoryMetaKeywords' + ]; + + /** + * 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 \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 + */ + 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; + } + + $methodName = $this->rendererMethods[$fieldName]; + $convertedResult = ''; + + + if ($this->dynamicRenderer->$methodName($category, $convertedResult)) { + return $convertedResult; + } + + 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 (empty($this->rendererMethods[$fieldName])) { + return true; + } + + return false; + } +} \ No newline at end of file diff --git a/Plugin/ModifyProductDescriptionsPlugin.php b/Plugin/ModifyProductDescriptionsPlugin.php new file mode 100755 index 0000000..b37a764 --- /dev/null +++ b/Plugin/ModifyProductDescriptionsPlugin.php @@ -0,0 +1,129 @@ +helperData = $helperData; + $this->shortDescriptionConverter = $shortDescriptionConverter; + $this->descriptionConverter = $descriptionConverter; + } + + /** + * @param \Magento\CatalogGraphQl\Model\Resolver\Product\ProductComplexTextAttribute $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\Product $product */ + $product = $value['model']; + $fieldName = $field->getName(); + + if ($this->out($product, $fieldName)) { + return $result; + } + + if (empty($result['html'])) { + return $result; + } + + if (!$this->isCurrentEntity($product, $info)) { + return $result; + } + + if ($fieldName === 'short_description') { + $result['html'] = $this->shortDescriptionConverter->convert($product, $result['html'], true); + $product->setShortDescription($result['html']); + } + + if ($fieldName === 'description') { + $result['html'] = $this->descriptionConverter->convert($product, $result['html'], true); + $product->setDescription($result['html']); + } + + return $result; + } + + /** + * @param \Magento\Catalog\Model\Product $product + * @param ResolveInfo $info + * @return bool + */ + protected function isCurrentEntity($product, $info) + { + $variables = $info->variableValues; + + return !empty($variables['_filter_0']['url_key']['eq']) + && $variables['_filter_0']['url_key']['eq'] === $product->getUrlKey(); + } + + /** + * Check if go out + * + * @param \Magento\Catalog\Model\Product $product + * @param string $fieldName + * @return boolean + */ + protected function out($product, $fieldName) + { + if (!is_object($product)) { + return true; + } + + if (!in_array($fieldName, ['description', 'short_description'])) { + return true; + } + + return false; + } +} \ No newline at end of file diff --git a/Plugin/ModifyProductNamePlugin.php b/Plugin/ModifyProductNamePlugin.php new file mode 100755 index 0000000..e8aba7e --- /dev/null +++ b/Plugin/ModifyProductNamePlugin.php @@ -0,0 +1,95 @@ +helperData = $helperData; + } + + /** + * @param \MageWorx\SeoAllGraphQl\Model\Resolver\Product\SeoRenderedElement $subject + * @param string|null $result + * @param Field $field + * @param $context + * @param ResolveInfo $info + * @param array|null $value + * @param array|null $args + * + * @return string + */ + public function afterResolve( + $subject, + $result, + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + if (!$result) { + return $result; + } + + /** @var \Magento\Catalog\Model\Product $product */ + $product = $value['model']; + $fieldName = $field->getName(); + + if ($this->out($product, $fieldName)) { + return $result; + } + + $productSeoName = $product->getData('product_seo_name'); + $product->setData('name', $productSeoName); + + return $productSeoName; + } + + /** + * Check if go out + * + * @param \Magento\Catalog\Model\Product $product + * @param string $fieldName + * @return boolean + */ + protected function out($product, $fieldName) + { + if (!is_object($product)) { + return true; + } + + if ($fieldName !== 'name') { + return true; + } + + if (!$this->helperData->isUseProductSeoName()) { + return true; + } + + if (empty($product->getData('product_seo_name'))) { + return true; + } + + return false; + } +} \ No newline at end of file diff --git a/Plugin/ModifyProductParamsPlugin.php b/Plugin/ModifyProductParamsPlugin.php new file mode 100755 index 0000000..920f6a9 --- /dev/null +++ b/Plugin/ModifyProductParamsPlugin.php @@ -0,0 +1,138 @@ +helperData = $helperData; + $this->metaTitleConverter = $metaTitleConverter; + $this->metaDescriptionConverter = $metaDescriptionConverter; + $this->metaKeywordsConverter = $metaKeywordsConverter; + } + + /** + * @param \MageWorx\SeoAllGraphQl\Model\Resolver\Product\SeoRenderedElement $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\Product $product */ + $product = $value['model']; + $fieldName = $field->getName(); + + if ($this->out($product, $fieldName)) { + return $result; + } + + if (!$this->isCurrentEntity($product, $info)) { + return $result; + } + + if ($fieldName === 'meta_title') { + $result = $this->metaTitleConverter->convert($product, $result, true); + $product->setMetaTitle($result); + } + + if ($fieldName === 'meta_description') { + $result = $this->metaDescriptionConverter->convert($product, $result, true); + $product->setMetaDescription($result); + } + + if ($fieldName === 'meta_keyword') { + $result = $this->metaKeywordsConverter->convert($product, $result, true); + $product->setMetaKeyword($result); + } + + return $result; + } + + /** + * @param \Magento\Catalog\Model\Product $product + * @param ResolveInfo $info + * @return bool + */ + protected function isCurrentEntity($product, $info) + { + $variables = $info->variableValues; + + return !empty($variables['_filter_0']['url_key']['eq']) + && $variables['_filter_0']['url_key']['eq'] === $product->getUrlKey(); + } + + /** + * Check if go out + * + * @param \Magento\Catalog\Model\Product $product + * @param string $fieldName + * @return boolean + */ + protected function out($product, $fieldName) + { + if (!is_object($product)) { + return true; + } + + if (!in_array($fieldName, ['meta_title', 'meta_description', 'meta_keyword'])) { + return true; + } + + return false; + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100755 index 0000000..9704448 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# CmsGraphQl + +**CmsGraphQl** provides type information for the GraphQl module +to generate CMS fields for cms information endpoints. diff --git a/composer.json b/composer.json new file mode 100755 index 0000000..9332e29 --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "mageworx/module-seoxtemplates-graph-ql", + "description": "N/A", + "type": "magento2-module", + "require": { + "mageworx/module-seoxtemplates": ">= 2.8.8", + "magento/module-cms-graph-ql": ">= 100.3.1 <104", + "magento/module-catalog-graph-ql": ">= 100.3.1 <104" + }, + "license": [ + "OSL-3.0", + "AFL-3.0" + ], + "autoload": { + "files": [ + "registration.php" + ], + "psr-4": { + "MageWorx\\SeoXTemplatesGraphQl\\": "" + } + }, + "version": "1.0.0" +} diff --git a/etc/di.xml b/etc/di.xml new file mode 100755 index 0000000..2db78cc --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/etc/module.xml b/etc/module.xml new file mode 100755 index 0000000..1ef946b --- /dev/null +++ b/etc/module.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/registration.php b/registration.php new file mode 100755 index 0000000..2f66234 --- /dev/null +++ b/registration.php @@ -0,0 +1,10 @@ +