Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the SERP preview #1092

Merged
merged 7 commits into from Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion calendar-bundle/src/Resources/contao/dca/tl_calendar_events.php
Expand Up @@ -259,7 +259,18 @@
'label' => &$GLOBALS['TL_LANG']['MSC']['serpPreview'],
'exclude' => true,
'inputType' => 'serpPreview',
'eval' => array('serpPreview'=>array('title'=>array('pageTitle', 'title'), 'description'=>array('description', 'teaser'))),
'eval' => array
(
'serpPreview' => array
(
'url' => static function (Contao\CalendarEventsModel $model)
{
return Contao\Events::generateEventUrl($model, true);
},
'title' => array('pageTitle', 'title'),
'description' => array('description', 'teaser')
)
),
'sql' => null
),
'location' => array
Expand Down
12 changes: 11 additions & 1 deletion core-bundle/src/Resources/contao/dca/tl_page.php
Expand Up @@ -269,7 +269,17 @@
'label' => &$GLOBALS['TL_LANG']['MSC']['serpPreview'],
'exclude' => true,
'inputType' => 'serpPreview',
'eval' => array('serpPreview'=>array('title'=>array('pageTitle', 'title'))),
'eval' => array
(
'serpPreview' => array
(
'url' => static function (Contao\PageModel $model)
{
return $model->getAbsoluteUrl();
},
'title' => array('pageTitle', 'title'),
)
),
'sql' => null
),
'redirect' => array
Expand Down
78 changes: 43 additions & 35 deletions core-bundle/src/Resources/contao/widgets/SerpPreview.php
Expand Up @@ -37,15 +37,21 @@ public function generate()
$id = $model->id;
$title = StringUtil::substr($this->getTitle($model), 64);
$description = StringUtil::substr($this->getDescription($model), 160);

// Get the URL with a %s placeholder for the alias or ID
$url = $this->getUrl($model);
list($baseUrl) = explode($model->alias ?: $model->id, $url);
$urlSuffix = System::getContainer()->getParameter('contao.url_suffix');
list($baseUrl) = explode('%s', $url);
$url = sprintf($url, $model->alias ?: $model->id);

// Get the input field suffix (edit multiple mode)
$suffix = substr($this->objDca->inputName, \strlen($this->objDca->field));
$titleField = $this->getTitleField() . $suffix;
$titleFallbackField = $this->getTitleFallbackField() . $suffix;
$aliasField = $this->getAliasField() . $suffix;
$descriptionField = $this->getDescriptionField() . $suffix;
$descriptionFallbackField = $this->getDescriptionFallbackField() . $suffix;

$titleField = $this->getTitleField($suffix);
$titleFallbackField = $this->getTitleFallbackField($suffix);
$aliasField = $this->getAliasField($suffix);
$descriptionField = $this->getDescriptionField($suffix);
$descriptionFallbackField = $this->getDescriptionFallbackField($suffix);
$urlSuffix = System::getContainer()->getParameter('contao.url_suffix');

return <<<EOT
<div class="serp-preview">
Expand Down Expand Up @@ -100,87 +106,89 @@ private function getDescription(Model $model)
return $model->{$this->serpPreview['description']};
}

/**
* @todo Use the router to generate the URL in a future version (see #831)
*/
private function getUrl(Model $model)
{
if (isset($this->serpPreview['url']))
if (!isset($this->serpPreview['url']))
{
return $this->serpPreview['url'];
throw new \Exception('Please provide the SERP widget URL as string or callable');
}

// FIXME: use the router to generate the URL (see #831)
switch (true)
if (\is_callable($this->serpPreview['url']))
{
case $model instanceof PageModel:
return $model->getAbsoluteUrl();
$placeholder = bin2hex(random_bytes(10));

case $model instanceof NewsModel:
return News::generateNewsUrl($model, false, true);
// Pass a detached clone with the alias set to the placeholder
$tempModel = clone $model;
$tempModel->origAlias = $tempModel->alias;
$tempModel->alias = $placeholder;
$tempModel->preventSaving(false);

case $model instanceof CalendarEventsModel:
return Events::generateEventUrl($model, true);

default:
throw new \RuntimeException(sprintf('Unsupported model class "%s"', \get_class($model)));
return str_replace($placeholder, '%s', $this->serpPreview['url']($tempModel));
aschempp marked this conversation as resolved.
Show resolved Hide resolved
}

return $this->serpPreview['url'];
}

private function getTitleField()
private function getTitleField($suffix)
{
if (!isset($this->serpPreview['title']))
{
return 'ctrl_title';
return 'ctrl_title' . $suffix;
}

if (\is_array($this->serpPreview['title']))
{
return 'ctrl_' . $this->serpPreview['title'][0];
return 'ctrl_' . $this->serpPreview['title'][0] . $suffix;
}

return 'ctrl_' . $this->serpPreview['title'];
return 'ctrl_' . $this->serpPreview['title'] . $suffix;
}

private function getTitleFallbackField()
private function getTitleFallbackField($suffix)
{
if (!isset($this->serpPreview['title']) || !\is_array($this->serpPreview['title']))
{
return '';
}

return 'ctrl_' . $this->serpPreview['title'][1];
return 'ctrl_' . $this->serpPreview['title'][1] . $suffix;
}

private function getAliasField()
private function getAliasField($suffix)
{
if (!isset($this->serpPreview['alias']))
{
return 'ctrl_alias';
return 'ctrl_alias' . $suffix;
}

return 'ctrl_' . $this->serpPreview['alias'];
return 'ctrl_' . $this->serpPreview['alias'] . $suffix;
}

private function getDescriptionField()
private function getDescriptionField($suffix)
{
if (!isset($this->serpPreview['description']))
{
return 'ctrl_description';
return 'ctrl_description' . $suffix;
}

if (\is_array($this->serpPreview['description']))
{
return 'ctrl_' . $this->serpPreview['description'][0];
return 'ctrl_' . $this->serpPreview['description'][0] . $suffix;
}

return 'ctrl_' . $this->serpPreview['description'];
return 'ctrl_' . $this->serpPreview['description'] . $suffix;
}

private function getDescriptionFallbackField()
private function getDescriptionFallbackField($suffix)
{
if (!isset($this->serpPreview['description']) || !\is_array($this->serpPreview['description']))
{
return '';
}

return 'ctrl_' . $this->serpPreview['description'][1];
return 'ctrl_' . $this->serpPreview['description'][1] . $suffix;
}
}
13 changes: 12 additions & 1 deletion news-bundle/src/Resources/contao/dca/tl_news.php
Expand Up @@ -239,7 +239,18 @@
'label' => &$GLOBALS['TL_LANG']['MSC']['serpPreview'],
'exclude' => true,
'inputType' => 'serpPreview',
'eval' => array('serpPreview'=>array('title'=>array('pageTitle', 'headline'), 'description'=>array('description', 'teaser'))),
'eval' => array
(
'serpPreview' => array
(
'url' => static function (Contao\NewsModel $model)
{
return Contao\News::generateNewsUrl($model, false, true);
},
'title' => array('pageTitle', 'headline'),
'description' => array('description', 'teaser')
)
),
'sql' => null
),
'subheadline' => array
Expand Down