Skip to content

Commit

Permalink
Fix folder_contents being broken with VHM and Multilingual
Browse files Browse the repository at this point in the history
When Multilingual is enabled and VHM is enabled too, serving any folderish's `folder_contents` under a language folder as a top level domain name is currently broken (if `en.site.com/archives` is VHMed to `PloneSite/en/archives` then `@@qsOptions` URL is `/archives/@@qsOptions` instead of being `/@@qsOptions` as it should be.

The fundamental problem is that the function `get_top_site_from_url(...)` is failing to retrieve the actual top site because it can't find a site in the traversal structure, so it picks the last folderish as the site, which is obvs broken because `/@@qsOptions` only works on site roots, not on folderish content.

This code makes it so that any site found in the structure is returned as the top site from the URL, but if no top site is found in that traversal, the actual site from `getSite()` is returned instead, which is the correct behavior based on how the function is named, and actually fixes the `/@@qsOptions` conundrum.
  • Loading branch information
Rudd-O authored Sep 7, 2022
1 parent 49075e9 commit 23708ce
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/plone/base/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,17 +262,28 @@ def get_top_site_from_url(context, request):
- Virtual hosting roots to Subsite, URL path: /, Returns: Subsite
"""
site = getSite()
# This variable collects all sites found during the traversal that
# takes place below, starting with the site root.
subsites = [site]
try:
url_path = urlparse(context.absolute_url()).path.split("/")
for idx in range(len(url_path)):
_path = "/".join(url_path[: idx + 1]) or "/"
site_path = "/".join(request.physicalPathFromURL(_path)) or "/"
_site = context.restrictedTraverse(site_path)
if ISite.providedBy(_site):
subsites.append(_site)
break
if _site:
site = _site
except (ValueError, AttributeError):
# Pick the subsite to return.
# If no subsite was found, return the top site.
# If at some point a subsite was found, return that
# (in effect, the shallowest subsite inside the site).
# With VHM, sometimes the topmost site is not actually
# in the client URL, so in that case we fall back to
# the actual top site, even if it is not within the
# client URL.
site = subsites[-1]
except (ValueError, AttributeError) as exc:
# On error, just return getSite.
# Refs: https://github.com/plone/plone.app.content/issues/103
# Also, TestRequest doesn't have physicalPathFromURL
Expand Down

0 comments on commit 23708ce

Please sign in to comment.