silverstripe / silverstripe-subsites

Subsites module for Silverstripe CMS
http://addons.silverstripe.org/add-ons/silverstripe/subsites
BSD 3-Clause "New" or "Revised" License
65 stars 106 forks source link

In line links on SubsiteVirtualPage are broken #16

Open sminnee opened 12 years ago

sminnee commented 12 years ago

Copied from http://open.silverstripe.org/ticket/6659

When creating a subsite Virtual Page, any links which are inline in the content are broken because they are set to be reletive to the current Subsite rather than the site they are actually on.

To fix it I had to override the core link parser with my own which returns an absolute link taking into account the Subsite the source page is on. It's a little hacky, as I had to set a constant for the main site domain as this is not stored in the db. In Page.php > Page:

public static function link_shortcode_handler($arguments, $content = null, $parser = null) {

    if(!isset($arguments['id']) || !is_numeric($arguments['id'])) return;

    if (
           !($page = DataObject::get_by_id('SiteTree', $arguments['id']))         // Get the current page by ID.
        && !($page = Versioned::get_latest_version('SiteTree', $arguments['id'])) // Attempt link to old version.
        && !($page = DataObject::get_one('ErrorPage', '"ErrorCode" = \'404\''))   // Link to 404 page directly.
    ) {
         return; // There were no suitable matches at all.
    }

    if($content) {
        return sprintf('<a href="%s">%s</a>', $page->MultisiteLink(), $parser->parse($content));
    } else {
        return $page->MultisiteLink();
    }
}

public function MultisiteLink()
{
    $Link = parent::Link();

    if($this->SubsiteID && $Subsite = DataObject::get_one('SubsiteDomain', "SubsiteID = $this->SubsiteID"))
    {
        return $Sibsite->Domain . $Link;
    }
    else //We are on the main site, so get the domain (set as a constant in _config) and append the relative link
    {
        return  constant('main_site_domain') . $Link;
    }
}
chillu commented 11 years ago

Links in the preview now have the current SubsiteID appended automatically, but that doesn't fix this specific issue - so still relevant.

torleif commented 9 years ago

We're ran into this issue. It's a high priority for us given we link across subsites often.

While this current fix above works, It's not ideal as all links will become absolute.

Perhaps a solution to is to extend the WYSIWYG popup for links, and allow the user to optionally select if they want to link to another subsite in the content?

torleif commented 9 years ago

The following solution allows CMS authors to select links across subsites in a friendly manner:

https://gist.github.com/torleif/1c69c8806ec206bd5b60

Selecting 'another subsite' in the linking options subsitelinker Selecting the page subsitelink2 selecting your subsite subsitelink3

Only links that are specified to be a subsite links are made absolute. Instead of relying on the subsites dataobject (which doesn't have the main site and dev domains), you create a list of domains in your _config.php file:

subsitelinker4

If you want to to create a push for this fix I can.

robbieaverill commented 7 years ago

@torleif are you still seeing this as a problem? If so, are you still willing to submit a pull request to fix it?

torleif commented 7 years ago

@robbieaverill yes we still have this problem. I should be able to create a PR

robbieaverill commented 6 years ago

Hey @torleif, still keen? =D

robbieaverill commented 5 years ago

Reproduced on SilverStripe 4.4.x with Subsites 2.3.x.

For me, the link in the original page to another page on that subsite is empty when you view it through a subsites virtual page on another subsite.