-
Notifications
You must be signed in to change notification settings - Fork 5
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 #5 from lopes-vincent/master
Add canonical override in seo form
- Loading branch information
Showing
11 changed files
with
249 additions
and
6 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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
# 1.2.0 | ||
|
||
- Add canonical override in seo form | ||
|
||
# 1.1.1 | ||
|
||
- Fix exception when Thelia was not configured | ||
|
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
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
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
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
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
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,103 @@ | ||
<?php | ||
|
||
namespace CanonicalUrl\EventListener; | ||
|
||
use CanonicalUrl\CanonicalUrl; | ||
use Thelia\Core\HttpFoundation\Request; | ||
use Symfony\Component\EventDispatcher\EventDispatcher; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Thelia\Action\BaseAction; | ||
use Thelia\Core\Event\TheliaEvents; | ||
use Thelia\Core\Event\TheliaFormEvent; | ||
use Thelia\Core\Event\UpdateSeoEvent; | ||
use Thelia\Model\MetaDataQuery; | ||
|
||
class SeoFormListener extends BaseAction implements EventSubscriberInterface | ||
{ | ||
/** @var \Thelia\Core\HttpFoundation\Request */ | ||
protected $request; | ||
|
||
/** | ||
* @param Request $request | ||
*/ | ||
public function __construct(Request $request) | ||
{ | ||
$this->request = $request; | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return array( | ||
TheliaEvents::FORM_AFTER_BUILD.'.thelia_seo' => array('addCanonicalField', 128), | ||
TheliaEvents::CATEGORY_UPDATE_SEO => array('saveCategorySeoFields', 128), | ||
TheliaEvents::BRAND_UPDATE_SEO => array('saveBrandSeoFields', 128), | ||
TheliaEvents::CONTENT_UPDATE_SEO => array('saveContentSeoFields', 128), | ||
TheliaEvents::FOLDER_UPDATE_SEO => array('saveFolderSeoFields', 128), | ||
TheliaEvents::PRODUCT_UPDATE_SEO => array('saveProductSeoFields', 128) | ||
); | ||
} | ||
|
||
public function saveCategorySeoFields(UpdateSeoEvent $event, $eventName, EventDispatcher $dispatcher) | ||
{ | ||
$this->saveSeoFields($event, $eventName, $dispatcher, 'category'); | ||
} | ||
|
||
public function saveBrandSeoFields(UpdateSeoEvent $event, $eventName, EventDispatcher $dispatcher) | ||
{ | ||
$this->saveSeoFields($event, $eventName, $dispatcher, 'brand'); | ||
} | ||
|
||
public function saveContentSeoFields(UpdateSeoEvent $event, $eventName, EventDispatcher $dispatcher) | ||
{ | ||
$this->saveSeoFields($event, $eventName, $dispatcher, 'content'); | ||
} | ||
|
||
public function saveFolderSeoFields(UpdateSeoEvent $event, $eventName, EventDispatcher $dispatcher) | ||
{ | ||
$this->saveSeoFields($event, $eventName, $dispatcher, 'folder'); | ||
} | ||
|
||
public function saveProductSeoFields(UpdateSeoEvent $event, $eventName, EventDispatcher $dispatcher) | ||
{ | ||
$this->saveSeoFields($event, $eventName, $dispatcher, 'product'); | ||
} | ||
|
||
protected function saveSeoFields(UpdateSeoEvent $event, $eventName, EventDispatcher $dispatcher, $elementKey) | ||
{ | ||
$form = $this->request->request->get('thelia_seo'); | ||
|
||
|
||
if (null === $form || !array_key_exists('id', $form) || !array_key_exists('canonical', $form)) { | ||
return; | ||
} | ||
|
||
$canonicalValues = []; | ||
|
||
$canonicalMetaData = MetaDataQuery::create() | ||
->filterByMetaKey(CanonicalUrl::SEO_CANONICAL_META_KEY) | ||
->filterByElementKey($elementKey) | ||
->filterByElementId($form['id']) | ||
->findOneOrCreate(); | ||
|
||
if (!$canonicalMetaData->isNew()) { | ||
$canonicalValues = json_decode($canonicalMetaData->getValue(), true); | ||
} | ||
|
||
$locale = $form['locale']; | ||
$canonicalValues[$locale] = $form['canonical']; | ||
|
||
$canonicalMetaData | ||
->setIsSerialized(0) | ||
->setValue(json_encode($canonicalValues)) | ||
->save(); | ||
} | ||
|
||
public function addCanonicalField(TheliaFormEvent $event) | ||
{ | ||
$event->getForm()->getFormBuilder() | ||
->add( | ||
'canonical', | ||
'text' | ||
); | ||
} | ||
} |
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,40 @@ | ||
<?php | ||
|
||
namespace CanonicalUrl\Hook; | ||
|
||
use CanonicalUrl\CanonicalUrl; | ||
use Thelia\Core\Event\Hook\HookRenderEvent; | ||
use Thelia\Core\Hook\BaseHook; | ||
use Thelia\Model\MetaDataQuery; | ||
|
||
class SeoUpdateFormHook extends BaseHook | ||
{ | ||
public function addInputs(HookRenderEvent $event) | ||
{ | ||
$id = $event->getArgument('id'); | ||
$type = $event->getArgument('type'); | ||
|
||
$canonical = null; | ||
$canonicalMetaData = MetaDataQuery::create() | ||
->filterByMetaKey(CanonicalUrl::SEO_CANONICAL_META_KEY) | ||
->filterByElementKey($type) | ||
->filterByElementId($id) | ||
->findOneOrCreate(); | ||
|
||
$canonicalMetaDataValues = json_decode($canonicalMetaData->getValue(), true); | ||
|
||
$lang = $this->getSession()->getAdminEditionLang(); | ||
|
||
if (isset($canonicalMetaDataValues[$lang->getLocale()])) { | ||
$canonical = $canonicalMetaDataValues[$lang->getLocale()]; | ||
} | ||
|
||
$event->add($this->render( | ||
'hook-seo-update-form.html', | ||
[ | ||
'form' => $event->getArgument('form'), | ||
'canonical' => $canonical | ||
] | ||
)); | ||
} | ||
} |
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,5 @@ | ||
<?php | ||
|
||
return array( | ||
'Surcharge de l\'url canonique' => 'Canonical url override', | ||
); |
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
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,6 @@ | ||
{form_field form=$form field='canonical' } | ||
<div class="form-group"> | ||
<label for="{$name}">{intl l="Surcharge de l'url canonique" d="canonicalurl.bo.default"}</label> | ||
<input type="text" id="{$name}" name="{$name}" value="{$canonical}" class="form-control"> | ||
</div> | ||
{/form_field} |