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 alias field and missing primary key #1169

Merged
merged 2 commits into from Jan 10, 2020
Merged

Conversation

@aschempp
Copy link
Contributor

aschempp commented Jan 10, 2020

This fixes two SERP issues:

  1. Because of the cloned model, the primary key is missing. So if the alias is empty, we try to generate an URL for an empty ID, which results in exception Parameter "alias" for route "contao_frontend" must match ".+" ("" given) to generate a corresponding URL

  2. Das temporäre Alias muss natürlich auf dem Feld alias und nicht auf dem Wert des Feldes gesetzt werden 😆

@aschempp aschempp added the defect label Jan 10, 2020
@aschempp aschempp added this to the 4.9 milestone Jan 10, 2020
@aschempp aschempp requested a review from leofeyer Jan 10, 2020
@aschempp aschempp self-assigned this Jan 10, 2020
$placeholder = bin2hex(random_bytes(10));

// Pass a detached clone with the alias set to the placeholder
$tempModel = clone $model;
$tempModel->origAlias = $tempModel->$alias;
$tempModel->$alias = $placeholder;
$tempModel->{$tempModel::getPk()} = $model->{$model::getPk()};

This comment has been minimized.

Copy link
@leofeyer

leofeyer Jan 10, 2020

Member

This line should not be necessary according to the initial issue description.

If the model has no ID and no alias this call will throw an exception.

This comment has been minimized.

Copy link
@aschempp

aschempp Jan 10, 2020

Author Contributor

It is necessary. Because if the alias is empty, PageModel::getAbsoluteUrl() will use the ID, and if that is empty as well, we'll get the exception.

This comment has been minimized.

Copy link
@leofeyer

leofeyer Jan 10, 2020

Member

I see. Then we should use cloneOriginal() instead.

		$aliasField = $this->aliasField ?: 'alias';
		$placeholder = bin2hex(random_bytes(10));

		// Pass a detached clone with the alias set to the placeholder
		$tempModel = $model->cloneOriginal();
		$tempModel->origAlias = $tempModel->$aliasField;
		$tempModel->$aliasField = $placeholder;
		$tempModel->preventSaving(false);
$placeholder = bin2hex(random_bytes(10));

// Pass a detached clone with the alias set to the placeholder
$tempModel = clone $model;
$tempModel->origAlias = $tempModel->$alias;
$tempModel->$alias = $placeholder;
$tempModel->{$tempModel::getPk()} = $model->{$model::getPk()};

This comment has been minimized.

Copy link
@leofeyer

leofeyer Jan 10, 2020

Member

I see. Then we should use cloneOriginal() instead.

		$aliasField = $this->aliasField ?: 'alias';
		$placeholder = bin2hex(random_bytes(10));

		// Pass a detached clone with the alias set to the placeholder
		$tempModel = $model->cloneOriginal();
		$tempModel->origAlias = $tempModel->$aliasField;
		$tempModel->$aliasField = $placeholder;
		$tempModel->preventSaving(false);
@leofeyer leofeyer mentioned this pull request Jan 10, 2020
@leofeyer leofeyer merged commit 4263016 into master Jan 10, 2020
9 checks passed
9 checks passed
Coverage
Details
Coding Style
Details
PHP 7.2
Details
PHP 7.3
Details
PHP 7.4
Details
Prefer Lowest
Details
Bundles
Details
Windows
Details
codecov/project 89.66% (-0.07%) compared to 0bb6227
Details
@leofeyer leofeyer deleted the bugfix/serp-alias branch Jan 10, 2020
@leofeyer

This comment has been minimized.

Copy link
Member

leofeyer commented Jan 10, 2020

Thank you @aschempp.

@leofeyer leofeyer changed the title Exception and incorrect SERP alias Fix the SERP preview alias field and missing primary key Jan 10, 2020
Tastaturberuf pushed a commit to Tastaturberuf/contao that referenced this pull request Jan 13, 2020
…1169)

Description
-----------

This fixes two SERP issues:

1. Because of the cloned model, the primary key is missing. So if the alias is empty, we try to generate an URL for an empty ID, which results in exception `Parameter "alias" for route "contao_frontend" must match ".+" ("" given) to generate a corresponding URL`

2. Das temporäre Alias muss natürlich auf dem Feld `alias` und nicht auf dem Wert des Feldes gesetzt werden 😆

Commits
-------

fdafcfb Fixed SERP issues
fece7aa Use cloneOriginal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.