Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[6.x] Add hooks for localization switching #12333

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

Open
tao wants to merge 1 commit into statamic:master
base: master
Choose a base branch
Loading
from tao:feature/multisite-hooks

Conversation

tao
Copy link
Contributor

@tao tao commented Sep 6, 2025

As discussed in #12328, and potentially related to the collaboration plugin issue #90 it would be helpful to have hooks available when the sites are selected or created in the multisite selector.

This pull requests adds 4 new hooks:

  • localization.selecting
  • localization.selected
  • localization.creating
  • localization.created

The createLocalization and editLocalization functions both use the same editLocalizationRequest axios request, but I have ensured that only the correct events are fired in either scenario.

The localization.selecting and localization.creating hooks provides the newly selected locale in the payload:

 Statamic.$hooks.on('localization.selecting', (resolve, reject, payload) => {
 let localization = payload.localization
 //
 resolve()
 })
// payload.localization
{
 active: false
 ​​​exists: true
 ​​​handle: "it"
 ​​​livePreviewUrl: "http://localhost:8000/cp/collections/speeches/entries/9f154338-8ff7-4021-a88a-5cc901cbe024/preview"
 name: "Italian"
 origin: false
 published: false​
 root: false
 ​​​status: "draft"​​
 url: "http://localhost:8000/cp/collections/speeches/entries/9f154338-8ff7-4021-a88a-5cc901cbe024"
}

While the localization.selected and localization.created hooks provide the localization and the response from the editLocalization request, which includes data about the current entry:

 Statamic.$hooks.on('localization.selected', (resolve, reject, payload) => {
 let localization = payload.localization
 let entry = payload.entry
 //
 resolve()
 })
// payload.entry
{
 "title": "1994 International UFO Congress",
 "description": "..."
 "content": "",
 "location": "Laughlin, NV",
 "slug": "international-ufo-congress",
 "date": "1994-03-08T00:00:00.000Z",
 "updated_at": 1757157846,
 "published_at": "2008-06-17",
 "published": false,
 "id": "cf03f1eb-c4ef-42bb-80d4-b0a811c8ee62"
}

At the moment, only the entry values are returned in the payload, but if some other values are necessary then they can be included from the response object... I don't specifically need the values for my use-case by perhaps in the future (or for the collab plugin bug) it would be useful to have the entry ID available when creating a new broadcast channel.

These events allow me to detect when a language changes and update the text direction of the Control Panel

 Statamic.$hooks.on('localization.selected', (resolve, reject, payload) => {
 let locale = payload.localization.handle;
 let direction = ['fa', 'he'].includes(locale) ? 'rtl' : 'ltr';
 
 // update the publish fields
 document.querySelector('.publish-fields').setAttribute('dir', direction);
 
 // update the full screen markdown editor
 document.querySelector('.portal-targets')?.setAttribute('dir', direction)
 resolve()
 })
Screen Shot 2025年09月06日 at 14 19 14 Screen Shot 2025年09月06日 at 14 28 47

It would be nice if we could use the localization.created or localization.creating hooks to update values automatically, like allowing for the automatic translation of some fields with an API and resolve() back the new entry data, but I think that might be better to handle on the Laravel side instead of the frontend?

I did examine the frontend tests folder and I couldn't see any tests for specific hooks, so I wasn't sure how to go about including those.

@tao tao changed the title (削除) Feature/multisite hooks (削除ここまで) (追記) Add hooks for localization switching (追記ここまで) Sep 6, 2025
@tao tao changed the title (削除) Add hooks for localization switching (削除ここまで) (追記) [6.x] Add hooks for localization switching (追記ここまで) Sep 6, 2025
@tao tao force-pushed the feature/multisite-hooks branch from 2a63d61 to 6f9e508 Compare October 6, 2025 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

AltStyle によって変換されたページ (->オリジナル) /