Skip to content

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

Closed
tao wants to merge 1 commit into
statamic:masterfrom
tao:feature/multisite-hooks
Closed

[6.x] Add hooks for localization switching#12333
tao wants to merge 1 commit into
statamic:masterfrom
tao:feature/multisite-hooks

Conversation

@tao

@tao tao commented Sep 6, 2025

Copy link
Copy Markdown
Contributor

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
@jasonvarga

Copy link
Copy Markdown
Member

Hey @tao can you check out #13024

Would that PR make this one unnecessary?

@jasonvarga

Copy link
Copy Markdown
Member

Closing this under the assumption that #13024 is enough to solve your issue. We can reopen if need be though.

@jasonvarga jasonvarga closed this Nov 12, 2025
@tao

tao commented Nov 26, 2025

Copy link
Copy Markdown
Contributor Author

It's amazing! I'm so happy that this has been implemented finally. #13024 works great except for the text direction of the full-screen markdown window... but I'll see if I can make a pull-request for that using the same solution.

@tao
tao deleted the feature/multisite-hooks branch November 26, 2025 07:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants