processwire / processwire-issues

ProcessWire issue reports.
45 stars 2 forks source link

What pages link to this page? link in a repeater returns url to a repeater page #1174

Open rogersarrasin opened 4 years ago

rogersarrasin commented 4 years ago

Short description of the issue

When on a website page in the admin area under Settings -> What pages link to this page? Found pages display links but only when links are in pages. If links are in repeater fields it returns a link to the repeater template. Ie. /user/setup/duplicator/for-field-346/for-page-1752/1561680242-0599-1/ (in href link)

Expected behavior

The expected behaviour would be a link to the parent page. Ie. the page that an anonymous user would see when viewing the page. A link to the repeater template doesn't seem too helpful.

Actual behavior

If a link was create in a repeater field, the rendered link under "What pages link to this page?" is a link to the repeater template. ie. Ie. /user/setup/duplicator/for-field-346/for-page-1752/1561680242-0599-1/ (in href link)

Optional: Screenshots/Links that demonstrate the issue

Screen Shot 2020-05-12 at 5 49 32 PM

Optional: Suggestion for a possible fix

Sorry, can't suggest a fix. But the behaviour of returning a link to the parent page and not the repeater template if a parent page existed would be much more useful.

Steps to reproduce the issue

  1. Create Repeater Matrix with a text field
  2. Add Repeater Matrix to a page template
  3. Edit the page and add an internal text link via the text field in the Repeater Matrix
  4. Go to the page that is being linked to in step #3, admin -> settings and view "What pages link to this page".

Setup/Environment

I've tested on three different sites an have the same results.

adrianbj commented 4 years ago

@rogersarrasin - I know you and I have discussed this and agree that a hook is not the proper solution, but just for the record here, this does "solve" it:

$this->addHookAfter('Page::references', function(HookEvent $event) {
    $referencingPages = $event->return;
    $actualPageArr = new PageArray();

    foreach($referencingPages as $p) {
        if(method_exists($p, 'getForPage')) {
            $actualPageArr->add($p->getForPage());
        }
        else {
            $actualPageArr->add($p);
        }
    }

    $event->return = $actualPageArr;
});
adrianbj commented 4 years ago

Sorry @rogersarrasin - I just realized you are talking about links, not references.

If you change that above hook for Page::links it should handle calls to $page->links() and also work for the "What pages link to this page" section.

You could also hook both at once, like this:

$this->addHookAfter('Page::references, Page::hooks', function(HookEvent $event) {
    $referencingPages = $event->return;
    $actualPageArr = new PageArray();

    foreach($referencingPages as $p) {
        if(method_exists($p, 'getForPage')) {
            $actualPageArr->add($p->getForPage());
        }
        else {
            $actualPageArr->add($p);
        }
    }

    $event->return = $actualPageArr;
});
rogersarrasin commented 4 years ago

@adrianbj Thanks for the updated. Seems adding Page::hooks didn't work, but replacing it with Page::links did.

$this->addHookAfter('Page::references, Page::links', function(HookEvent $event) {
    $referencingPages = $event->return;

    $actualPageArr = new PageArray();

    foreach($referencingPages as $p) {
        if(method_exists($p, 'getForPage')) {
            $actualPageArr->add($p->getForPage());
        } else {
            $actualPageArr->add($p);
        }
    }

    $event->return = $actualPageArr;
});
adrianbj commented 4 years ago

Oh my goodness - I don't know how or why I ended up with hooks instead of links - brain definitely wasn't firing on all cylinders when I typed that :)

Anyway, glad it worked once you fixed my blunder!