Skip to content

Commit

Permalink
Merge pull request #31 from mageprince/develop-1.0.7
Browse files Browse the repository at this point in the history
Solve FAQ url issue
  • Loading branch information
mageprince authored Mar 4, 2020
2 parents 379b846 + c8d78ed commit 4120bdf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Controller/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public function execute()
$metaDescriptionConfig = $this->helper->getConfig('faqtab/seo/meta_description');

$resultPage->getConfig()->getTitle()->set($metaTitleConfig);
$resultPage->getConfig()->setDescription($metaKeywordsConfig);
$resultPage->getConfig()->setKeywords($metaDescriptionConfig);
$resultPage->getConfig()->setDescription($metaDescriptionConfig);
$resultPage->getConfig()->setKeywords($metaKeywordsConfig);

return $resultPage;
}
Expand Down
51 changes: 47 additions & 4 deletions Observer/ConfigChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@

namespace Prince\Faq\Observer;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\UrlRewrite\Model\UrlRewriteFactory;
use Magento\Store\Model\StoreManagerInterface;

class ConfigChange implements ObserverInterface
{
const REQUEST_PATH = 'faq';

/**
* @var RequestInterface
*/
Expand All @@ -28,28 +33,66 @@ class ConfigChange implements ObserverInterface
* @var WriterInterface
*/
private $configWriter;
/**
* @var UrlRewriteFactory
*/
private $urlRewriteFactory;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* ConfigChange constructor.
* @param RequestInterface $request
* @param WriterInterface $configWriter
* @param UrlRewriteFactory $urlRewriteFactory
* @param StoreManagerInterface $storeManager
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
RequestInterface $request,
WriterInterface $configWriter
WriterInterface $configWriter,
UrlRewriteFactory $urlRewriteFactory,
StoreManagerInterface $storeManager,
ScopeConfigInterface $scopeConfig
) {
$this->request = $request;
$this->configWriter = $configWriter;
$this->urlRewriteFactory = $urlRewriteFactory;
$this->storeManager = $storeManager;
$this->scopeConfig = $scopeConfig;
}

public function execute(EventObserver $observer)
{
$faqParams = $this->request->getParam('groups');
$faqUrlVal = $faqParams['seo']['fields']['faq_url']['value'];
if($faqUrlVal) {
$urlKey = str_replace(' ', '-', $faqUrlVal);
$faqUrlVal = $faqParams['seo']['fields'];
if(key_exists('faq_url', $faqUrlVal)) {
$urlKey = str_replace(' ', '-', $faqUrlVal['faq_url']['value']);
$filterUrlKey = preg_replace('/[^A-Za-z0-9\-]/', '', $urlKey);
$this->configWriter->save('faqtab/seo/faq_url', $filterUrlKey);
$storeId = $this->storeManager->getStore()->getId();
$faqUrl = $this->scopeConfig->getValue(
'faqtab/seo/faq_url',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeId
);
$urlRewriteModel = $this->urlRewriteFactory->create();
$rewritecollection = $urlRewriteModel->getCollection()
->addFieldToFilter('request_path', self::REQUEST_PATH)
->addFieldToFilter('store_id', $storeId)
->getFirstItem();
$urlRewriteModel->load($rewritecollection->getId());
$urlRewriteModel->setStoreId($storeId);
$urlRewriteModel->setTargetPath($faqUrl);
$urlRewriteModel->setRequestPath(self::REQUEST_PATH);
$urlRewriteModel->setredirectType(301);
$urlRewriteModel->save();
}
return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
</group>
<group id="seo" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Search Engine Optimization </label>
<field id="faq_url" translate="label" sortOrder="10" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="faq_url" translate="label" sortOrder="10" type="text" showInDefault="1">
<label>FAQ Url</label>
<validate>required-entry</validate>
<validate>required-entry validate-identifier</validate>
</field>
<field id="meta_title" translate="label" sortOrder="20" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Meta Title</label>
Expand Down

0 comments on commit 4120bdf

Please sign in to comment.