-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mageworx/develop
added canonical_url and mw_hreflangs fields
- Loading branch information
Showing
9 changed files
with
492 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/** | ||
* Copyright © MageWorx. All rights reserved. | ||
* See LICENSE.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MageWorx\SeoBaseGraphQl\Model\Resolver\Category; | ||
|
||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\Exception\LocalizedException; | ||
|
||
class CanonicalUrl implements ResolverInterface | ||
{ | ||
/** | ||
* @var \MageWorx\SeoBase\Model\CanonicalFactory | ||
*/ | ||
protected $canonicalFactory; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $fullActionName = 'catalog_category_view'; | ||
|
||
/** | ||
* CanonicalUrl constructor. | ||
* | ||
* @param \MageWorx\SeoBase\Model\CanonicalFactory $canonicalFactory | ||
*/ | ||
public function __construct( | ||
\MageWorx\SeoBase\Model\CanonicalFactory $canonicalFactory | ||
) { | ||
$this->canonicalFactory = $canonicalFactory; | ||
} | ||
|
||
/** | ||
* @param Field $field | ||
* @param ContextInterface $context | ||
* @param ResolveInfo $info | ||
* @param array|null $value | ||
* @param array|null $args | ||
* @return Value|mixed | ||
* @throws LocalizedException | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
if (!isset($value['model'])) { | ||
throw new LocalizedException(__('"model" value should be specified')); | ||
} | ||
|
||
$category = $value['model']; | ||
$arguments = ['fullActionName' => $this->fullActionName]; | ||
|
||
/** @var \MageWorx\SeoBase\Model\CanonicalInterface $canonicalModel */ | ||
$canonicalModel = $this->canonicalFactory->create($this->fullActionName, $arguments); | ||
$canonicalModel->setEntity($category); | ||
|
||
$canonicalUrl = $canonicalModel->getCanonicalUrl(); | ||
|
||
if ($canonicalUrl) { | ||
return $canonicalUrl; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
/** | ||
* Copyright © MageWorx. All rights reserved. | ||
* See LICENSE.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MageWorx\SeoBaseGraphQl\Model\Resolver\Category; | ||
|
||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use MageWorx\SeoBase\Model\HreflangsFactory; | ||
|
||
class Hreflangs implements ResolverInterface | ||
{ | ||
/** | ||
* @var HreflangsFactory | ||
*/ | ||
protected $hreflangsFactory; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $fullActionName = 'catalog_category_view'; | ||
|
||
/** | ||
* Hreflangs constructor. | ||
* | ||
* @param HreflangsFactory $hreflangsFactory | ||
*/ | ||
public function __construct(HreflangsFactory $hreflangsFactory) | ||
{ | ||
$this->hreflangsFactory = $hreflangsFactory; | ||
} | ||
|
||
/** | ||
* @param Field $field | ||
* @param ContextInterface $context | ||
* @param ResolveInfo $info | ||
* @param array|null $value | ||
* @param array|null $args | ||
* @return Value|mixed | ||
* @throws LocalizedException | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
if (!isset($value['model'])) { | ||
throw new LocalizedException(__('"model" value should be specified')); | ||
} | ||
|
||
return [ | ||
'items' => $this->getHreflangsData($value['model']), | ||
]; | ||
} | ||
|
||
/** | ||
* @param \Magento\Catalog\Model\Category $category | ||
* @return array|null | ||
*/ | ||
protected function getHreflangsData(\Magento\Catalog\Model\Category $category): ?array | ||
{ | ||
$arguments = ['fullActionName' => $this->fullActionName]; | ||
$hreflangModel = $this->hreflangsFactory->create($this->fullActionName, $arguments); | ||
|
||
if (!$hreflangModel) { | ||
return null; | ||
} | ||
|
||
$hreflangModel->setEntity($category); | ||
|
||
$hreflangUrls = $hreflangModel->getHreflangUrls(); | ||
|
||
if (!$hreflangUrls) { | ||
return null; | ||
} | ||
|
||
$data = []; | ||
|
||
foreach ($hreflangUrls as $code => $hreflangUrl) { | ||
$data[$code] = [ | ||
'url' => $hreflangUrl, | ||
'code' => $code | ||
]; | ||
} | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
/** | ||
* Copyright © MageWorx. All rights reserved. | ||
* See LICENSE.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MageWorx\SeoBaseGraphQl\Model\Resolver\CmsPage; | ||
|
||
use Magento\Cms\Api\Data\PageInterface; | ||
use Magento\Cms\Api\PageRepositoryInterface; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\Exception\LocalizedException; | ||
|
||
class CanonicalUrl implements ResolverInterface | ||
{ | ||
/** | ||
* @var \MageWorx\SeoBase\Model\CanonicalFactory | ||
*/ | ||
protected $canonicalFactory; | ||
|
||
/** | ||
* @var PageRepositoryInterface | ||
*/ | ||
protected $pageRepository; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $fullActionName = 'cms_page_view'; | ||
|
||
/** | ||
* CanonicalUrl constructor. | ||
* | ||
* @param \MageWorx\SeoBase\Model\CanonicalFactory $canonicalFactory | ||
* @param PageRepositoryInterface $pageRepository | ||
*/ | ||
public function __construct( | ||
\MageWorx\SeoBase\Model\CanonicalFactory $canonicalFactory, | ||
PageRepositoryInterface $pageRepository | ||
) { | ||
$this->canonicalFactory = $canonicalFactory; | ||
$this->pageRepository = $pageRepository; | ||
} | ||
|
||
/** | ||
* @param Field $field | ||
* @param ContextInterface $context | ||
* @param ResolveInfo $info | ||
* @param array|null $value | ||
* @param array|null $args | ||
* @return Value|mixed | ||
* @throws LocalizedException | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
if (!isset($value[PageInterface::PAGE_ID])) { | ||
throw new LocalizedException(__('"%1" value should be specified', PageInterface::PAGE_ID)); | ||
} | ||
|
||
/** @var \Magento\Cms\Model\Page $cmsPage */ | ||
$cmsPage = $this->pageRepository->getById($value[PageInterface::PAGE_ID]); | ||
$arguments = ['fullActionName' => $this->fullActionName]; | ||
|
||
/** @var \MageWorx\SeoBase\Model\CanonicalInterface $canonicalModel */ | ||
$canonicalModel = $this->canonicalFactory->create($this->fullActionName, $arguments); | ||
$canonicalModel->setEntity($cmsPage); | ||
|
||
$canonicalUrl = $canonicalModel->getCanonicalUrl(); | ||
|
||
if ($canonicalUrl) { | ||
return $canonicalUrl; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
/** | ||
* Copyright © MageWorx. All rights reserved. | ||
* See LICENSE.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MageWorx\SeoBaseGraphQl\Model\Resolver\CmsPage; | ||
|
||
use Magento\Cms\Api\Data\PageInterface; | ||
use Magento\Cms\Api\PageRepositoryInterface; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use MageWorx\SeoBase\Model\HreflangsFactory; | ||
use MageWorx\SeoBase\Helper\Data as HelperData; | ||
|
||
class Hreflangs implements ResolverInterface | ||
{ | ||
/** | ||
* @var HreflangsFactory | ||
*/ | ||
protected $hreflangsFactory; | ||
|
||
/** | ||
* @var PageRepositoryInterface | ||
*/ | ||
protected $pageRepository; | ||
|
||
/** | ||
* @var HelperData | ||
*/ | ||
protected $helperData; | ||
|
||
/** | ||
* Hreflangs constructor. | ||
* | ||
* @param HreflangsFactory $hreflangsFactory | ||
* @param PageRepositoryInterface $pageRepository | ||
* @param HelperData $helperData | ||
*/ | ||
public function __construct( | ||
HreflangsFactory $hreflangsFactory, | ||
PageRepositoryInterface $pageRepository, | ||
HelperData $helperData | ||
) { | ||
$this->hreflangsFactory = $hreflangsFactory; | ||
$this->pageRepository = $pageRepository; | ||
$this->helperData = $helperData; | ||
} | ||
|
||
/** | ||
* @param Field $field | ||
* @param ContextInterface $context | ||
* @param ResolveInfo $info | ||
* @param array|null $value | ||
* @param array|null $args | ||
* @return Value|mixed | ||
* @throws LocalizedException | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
if (!isset($value[PageInterface::PAGE_ID])) { | ||
throw new LocalizedException(__('"%1" value should be specified', PageInterface::PAGE_ID)); | ||
} | ||
|
||
/** @var \Magento\Cms\Model\Page $cmsPage */ | ||
$cmsPage = $this->pageRepository->getById($value[PageInterface::PAGE_ID]); | ||
|
||
return [ | ||
'items' => $this->getHreflangsData($cmsPage), | ||
]; | ||
} | ||
|
||
/** | ||
* @param \Magento\Cms\Model\Page $cmsPage | ||
* @return array|null | ||
*/ | ||
protected function getHreflangsData(\Magento\Cms\Model\Page $cmsPage): ?array | ||
{ | ||
$fullActionName = $this->isHomePage($cmsPage) ? 'cms_index_index' : 'cms_page_view'; | ||
$arguments = ['fullActionName' => $fullActionName]; | ||
$hreflangModel = $this->hreflangsFactory->create($fullActionName, $arguments); | ||
|
||
if (!$hreflangModel) { | ||
return null; | ||
} | ||
|
||
$hreflangModel->setEntity($cmsPage); | ||
|
||
$hreflangUrls = $hreflangModel->getHreflangUrls(); | ||
|
||
if (!$hreflangUrls) { | ||
return null; | ||
} | ||
|
||
$data = []; | ||
|
||
foreach ($hreflangUrls as $code => $hreflangUrl) { | ||
$data[$code] = [ | ||
'url' => $hreflangUrl, | ||
'code' => $code | ||
]; | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* @param \Magento\Cms\Model\Page $page | ||
* @return bool | ||
*/ | ||
protected function isHomePage(\Magento\Cms\Model\Page $page): bool | ||
{ | ||
$homePageId = null; | ||
$homeIdentifier = $this->helperData->getHomeIdentifier(); | ||
|
||
if (strpos($homeIdentifier, '|') !== false) { | ||
list($homeIdentifier, $homePageId) = explode('|', $homeIdentifier); | ||
} | ||
|
||
return $homeIdentifier == $page->getIdentifier(); | ||
} | ||
} |
Oops, something went wrong.