Skip to content

Commit

Permalink
Support template overrides and custom meta templates in SEO Preview f…
Browse files Browse the repository at this point in the history
…or ... everything
  • Loading branch information
mmikkel committed Apr 6, 2024
1 parent be0f337 commit 5c25e81
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 22 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

### Added
- Added the ability to create field profiles specific to a particular section, entry type, category group or Commerce product type ([#86](https://github.com/vaersaagod/seomate/pull/86))
- Added the ability to be specific in the `previewEnabled` setting about which sections, entry types, category groups and/or Commerce product types should be SEO-previewable
- Added the ability to be specific in the `previewEnabled` setting about which sections, entry types, category groups and/or Commerce product types should be SEO-previewable
- Added support for custom meta templates and template overrides in SEO Preview to categories, nested entries and Commerce products, in addition to regular ol' section entries

### Changed
- Removed support for "SEO Preview" for elements using legacy live preview
Expand Down
89 changes: 68 additions & 21 deletions src/controllers/PreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@
use Craft;

use craft\base\Element;
use craft\base\ElementInterface;
use craft\commerce\elements\Product;
use craft\elements\Category;
use craft\elements\Entry;
use craft\web\Controller;
use craft\web\Response;
use craft\web\View;

use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;

use vaersaagod\seomate\SEOMate;

use yii\base\Exception;
use yii\base\InvalidConfigException;
use yii\web\Response as YiiResponse;
use yii\web\Response;
use yii\web\ServerErrorHttpException;

/**
Expand All @@ -40,11 +46,12 @@ class PreviewController extends Controller
protected int|bool|array $allowAnonymous = ['index'];

/**
* @return Response
* @throws Exception
* @throws InvalidConfigException
* @throws ServerErrorHttpException
*/
public function actionIndex(): Response|YiiResponse
public function actionIndex(): Response
{
$elementId = Craft::$app->getRequest()->getParam('elementId');
$siteId = Craft::$app->getRequest()->getParam('siteId');
Expand Down Expand Up @@ -78,26 +85,13 @@ public function actionIndex(): Response|YiiResponse
$view->setTemplateMode(View::TEMPLATE_MODE_SITE);
$context = $view->getTwig()->getGlobals();

$meta = null;

if ($element instanceof Entry) {
// If this is an entry, get the metadata from the rendered entry template
// This ensures that custom meta templates and template overrides will be rendered
try {
if ($template = $element->getSection()?->getSiteSettings()[$element->siteId]['template'] ?? null) {
$variables = array_merge($context, [
'entry' => $element,
'seomatePreviewElement' => $element,
]);
$html = $view->renderTemplate($template, $variables);
$meta = $this->_getMetaFromHtml($html);
}
} catch (\Throwable $e) {
\Craft::error($e, __METHOD__);
}
try {
$meta = $this->_getMetaFromElementPageTemplate($element, $context);
} catch (\Throwable $e) {
Craft::error("An error occurred when attempting to render meta data for element page template: " . $e->getMessage(), __METHOD__);
}

if (!$meta) {
if (empty($meta)) {
// Fall back to getting the metadata directly from the meta service
$context = array_merge($context, [
'seomate' => [
Expand All @@ -119,6 +113,59 @@ public function actionIndex(): Response|YiiResponse
]);
}

/**
* @param ElementInterface $element
* @param array $context
* @return array|null
* @throws Exception
* @throws InvalidConfigException
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
private function _getMetaFromElementPageTemplate(ElementInterface $element, array $context = []): ?array
{

if (!$element instanceof Element) {
return null;
}

$refHandle = null;
if (method_exists($element, 'refHandle')) {
$refHandle = $element->refHandle();
}

if (empty($refHandle)) {
return null;
}

$pageTemplate = null;

if ($element instanceof Entry) {
if (!empty($element->sectionId)) {
$pageTemplate = $element->getSection()?->getSiteSettings()[$element->siteId]['template'] ?? null;
} else if (!empty($element->fieldId)) { // Nested entry
$pageTemplate = $element->getField()->siteSettings[$element->getSite()->uid]['template'] ?? null;
}
} else if ($element instanceof Category) {
$pageTemplate = $element->getGroup()->getSiteSettings()[$element->siteId]['template'] ?? null;
} else if ($element instanceof Product) {
$pageTemplate = $element->getType()->getSiteSettings()[$element->siteId]['template'] ?? null;
}

if (empty($pageTemplate) || !is_string($pageTemplate)) {
return null;
}

$variables = array_merge($context, [
$refHandle => $element,
'seomatePreviewElement' => $element,
]);
$html = Craft::$app->getView()->renderTemplate($pageTemplate, $variables);

return $this->_getMetaFromHtml($html);
}

/**
* @param string|null $html
* @return array|null
Expand Down

0 comments on commit 5c25e81

Please sign in to comment.