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

Incorrect format used for page link when parent page is unpublished #774

Open
stnvh opened this issue Jan 20, 2023 · 2 comments
Open

Incorrect format used for page link when parent page is unpublished #774

stnvh opened this issue Jan 20, 2023 · 2 comments

Comments

@stnvh
Copy link

stnvh commented Jan 20, 2023

When a child page is linked using the [sitetree_link] shortcode and it's parent is unpublished, the link looses all references to the parent page(s) URL structure and country suffix in base URL, instead appending the ?l= parameter to the link.

For a linked page located at example.com/uk/root-page/subpage/, if root-page is unpublished, the link output by the shortcode (calling $page->Link()) becomes example.com/subpage/?l=en_GB.

This issue causes problems for legacy context, for example when implementing redirects for unpublished/archived pages linked in many content areas like blog posts etc.

A temp fix is to re-register the link_shortcode_handler on an extended SiteTree model to walk the parent tree instead, building the link manually from last known version:

/* ... */

public static function link_shortcode_handler($arguments, $content = null, $parser = null)
{
    $link = parent::link_shortcode_handler($arguments, $content, $parser);

    if (stripos($link, 'l=') !== false) {
        $parents = array_reverse(self::get_parent_ids_for_page($arguments['id']));
        $locale = Locale::getCurrentLocale();
        $newLink = $locale ? $locale->getBaseURL() : '/';

        foreach ($parents as $id) {
            $page = DataObject::get_by_id(SiteTree::class, $id) ?: Versioned::get_latest_version(SiteTree::class, $id);

            if (!$page) continue;

            $newLink .= sprintf('%s/', $page->URLSegment);
        }

        return $content ? sprintf('<a href="%s">%s</a>', $newLink, $content) : $newLink;
    }

    return $link;
}

protected static function get_parent_ids_for_page($id)
{
    $current = $id;
    $pages = [$id];

    while ($current) {
        $result = DB::prepared_query('SELECT ParentID FROM SiteTree WHERE ID=?', [$current]);
        $current = $result->value();
        $pages[] = $current;
    }

    return array_filter($pages);
}
@tractorcow
Copy link
Collaborator

Can you even navigate to a page in live mode when its parent is unpublished, even without fluent?

@stnvh
Copy link
Author

stnvh commented Jan 23, 2023

The issue isn't if the page can be navigated to or not, in the above circumstance both would still return a 404. The issue is about maintaining the link formatting.

In one example it would make it easier to implement redirects. If a page is linked to on say 100+ blog posts, a new version of that page is created elsewhere and a redirect middleware / htaccess was being used to handle this, when one of the parents are unpublished for the old page all prior redirects won't have effect any longer; the fluent base url is removed along with the parent tree context.

Two separate pages with the same URL segment under different unpublished parents even render the same (albeit broken) link, it makes it near impossible to manage legacy context, though I understand this may be a wider scope than fluent.

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

No branches or pull requests

2 participants