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

Redirect edit pages to the previous page on save #16140

Open
wants to merge 6 commits into
base: 5.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/craftcms-vue/admintable/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@
isEmpty: false,
isLoading: true,
searchClearTitle: Craft.escapeHtml(Craft.t('app', 'Clear')),
searchTerm: '',
searchTerm: new URL(window.location.href).searchParams.get('q') || '',
selectAll: null,
sortable: null,
tableBodySelector: '.vuetable-body',
Expand Down Expand Up @@ -717,6 +717,10 @@
}
this.reload();
}

const url = new URL(window.location.href);
url.searchParams.set('q', this.searchTerm);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brandonkelly I don't think using q here will cause any issues, but if you think it's likely to run into another query param, we can use something else.

window.history.replaceState({}, '', url);
}, 500),

resetSearch: function () {
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/ElementsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public function actionEdit(?ElementInterface $element, ?int $elementId = null):
$type = $element::lowerDisplayName();
$enabledForSite = $element->getEnabledForSite();
$hasRoute = $element->getRoute() !== null;
$redirectUrl = ElementHelper::postEditUrl($element);
$redirectUrl = UrlHelper::cpReferralUrl() ?? $element->getPostEditUrl() ?? Craft::$app->getConfig()->getGeneral()->getPostCpLoginRedirect();

// Site statuses
if ($canEditMultipleSites) {
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/EntryTypesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use craft\fieldlayoutelements\entries\EntryTitleField;
use craft\helpers\Cp;
use craft\helpers\Html;
use craft\helpers\UrlHelper;
use craft\models\EntryType;
use craft\models\Section;
use craft\web\Controller;
Expand Down Expand Up @@ -92,7 +93,7 @@ public function actionEdit(?int $entryTypeId = null, ?EntryType $entryType = nul
->addCrumb(Craft::t('app', 'Settings'), 'settings')
->addCrumb(Craft::t('app', 'Entry Types'), 'settings/entry-types')
->action('entry-types/save')
->redirectUrl('settings/entry-types')
->redirectUrl(UrlHelper::cpReferralUrl() ?? 'settings/entry-types')
->addAltAction(Craft::t('app', 'Save and continue editing'), [
'redirect' => 'settings/entry-types/{id}',
'shortcut' => true,
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/FieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function actionEditField(?int $fieldId = null, ?FieldInterface $field = n
->addCrumb(Craft::t('app', 'Settings'), 'settings')
->addCrumb(Craft::t('app', 'Fields'), 'settings/fields')
->action('fields/save-field')
->redirectUrl('settings/fields')
->redirectUrl(UrlHelper::cpReferralUrl() ?? 'settings/fields')
->addAltAction(Craft::t('app', 'Save and continue editing'), [
'redirect' => 'settings/fields/edit/{id}',
'shortcut' => true,
Expand Down
23 changes: 23 additions & 0 deletions src/helpers/UrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,29 @@ public static function cpHost(): string
return static::hostInfo(static::baseCpUrl());
}

/**
* Returns a CP referral URL.
*
* @return string|null
* @since 5.6.0
*/
public static function cpReferralUrl(): ?string
{
$referrer = Craft::$app->getRequest()->getReferrer();

// Make sure it didn't refer itself
if ($referrer === Craft::$app->getRequest()->getFullUri()) {
return null;
}

// Make sure the CP referred it
if (!str_contains($referrer, self::baseCpUrl())) {
return null;
}

return $referrer;
}

/**
* Parses a URL for the host info.
*
Expand Down
2 changes: 1 addition & 1 deletion src/web/assets/admintable/dist/js/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/admintable/dist/js/app.js.map

Large diffs are not rendered by default.

Loading