Andreas Augustin

Edit Article-URL for Preview

Recommended Posts


I have a very special Processwire Setup. Short: We use Processwire for our "Blog", which is embedded in an SAP Hybris Webshop. PW provedes the Contnet via HTML and Hybris render this HTML into the Webshop (Between Default Header, Navigation and Footer)

The URLs which are created in PW are the URLs for the local HTML e.g.:

If some Editor creates an article, he want to see the preview in the real webshop embedded. This URL would be: http://www.webshop.coom/en/blog/news/some-article

Where and how can I create a Module which overwrites the URL with the correct one in the Admin-Area?

Share this post

Link to post
Share on other sites

The easiest approach would be to add a hook in your module after getViewActions that modifies the event's return value and replaces the URL for every returned array element.

  • Like 1

Share this post

Link to post
Share on other sites

Another approach would be to do a redirect in your template file:

$session->redirect('' . $page->url);


Share this post

Link to post
Share on other sites


On 7.2.2017 at 10:09 PM, Robin S said:

Another approach would be to do a redirect in your template file:

$session->redirect('' . $page->url);



Good Idea - I have only have to find some logic that the redirect doesn't happen if the request comes from our Webshop System/Server

Share this post

Link to post
Share on other sites

Bumping this to see if anyone else found a viable solution.

We need to be able to redirect users to an app served by a different server as the PW instance just outputs JSON data. Seems like there is no hook available to simply alter the view URL. Would be super useful.

Share this post

Link to post
Share on other sites

A addtitonal approach would be combination with AdminCustomFiles module.

i use this to change the href of the view button where i've events that their single template creates an .ics file output and the editor only should visit/see the is my example.

like in the module described a file called ProcessPageEdit.js under /templates/AdminCustomFiles/ folder:

//used in a PW 2.7.x installation!
//change the /events/ links in backend page edit for preview the events page not the .ics file!
$(document).ready(function() {
    $("a#_ProcessPageEditView[href*=events]").addClass('found').attr("href", "/events/");

but the hook methode direct via PHP is the better way on more complex url creating....just mentioned my workaround

Share this post

Link to post
Share on other sites

Figured out a slight workaround today using various hooks, it's probably not ideal but seems to work OK. It adds two new fields to every template, the first gives the option to hide view links from pages using that template, the second adds the ability to edit the URL. The following is in my ready.php file:

// Helper function to rewrite URLs with dynamic properties
// For example {name} will be replaced with the page name property
function rewriteURL($page, $url) {
  $appUrl = wire('pages')->get(1)->link; // Base URL set in home template
  $regex = '/(?:{([^}]+?)})|(.+?(?={[^}]+?}|$))/';
  $out = '';

  preg_match_all($regex, $url, $matches, PREG_SET_ORDER, 0);

  foreach($matches as $match) {
    if ($match[1]) $out .= $page[$match[1]];
    else if ($match[2]) $out .= $match[2];

  return $appUrl.$out; // e.g.

// Rewrite page list view URLs
$wire->addHook('ProcessPageListActions::getActions', function($event) {
  $page = $event->arguments(0);
  $template = $page->template;
  if ($template->hideViewLink) {
    $actions = $event->return;
    $event->return = $actions;
  } elseif ($template->rewriteViewURL) {
    $actions = $event->return;
    if (array_key_exists('view', $actions)) {
      $actions['view'] = array(
        'cn' => $actions['view']['cn'],
        'name' => $actions['view']['name'],
        'url' => rewriteURL($page, $template->rewriteViewURL),
      $event->return = $actions;

// Rewrite page edit view URLs
$wire->addHookBefore('ProcessPageEdit::buildFormView', function($event) {
  $page = $event->object->getPage();
  $template = $page->template;
  if ($template->rewriteViewURL) {
    $updatedURL = rewriteURL($page, $template->rewriteViewURL);
    $event->arguments = [ $updatedURL ];

// Edits the edit page 'View' tab if required
// Removes dropdown items
$wire->addHookAfter('ProcessPageEdit::getTabs', function($event) {
  $template = $event->object->getPage()->template;
  $tabs = $event->return;
  $editTab = array_key_exists('ProcessPageEditView', $tabs);
  if ($editTab) {
    if ($template->hideViewLink) {
      $tabs['ProcessPageEditView'] = false;
    } else {
      $tabs['ProcessPageEditView'] = preg_replace('/<span.*(?=<\/a>)/', '', $tabs['ProcessPageEditView']);
    $event->return = $tabs;

// Adds extra fields to templates
$wire->addHookAfter("ProcessTemplate::buildEditForm", function(HookEvent $event) {
  $template = $event->arguments[0];
  $form = $event->return;

  $f = $this->modules->get('InputfieldText');
  $f->attr('id+name', 'rewriteViewURL');
  $f->value = $template->rewriteViewURL;
  $f->label = $this->_('Rewrite View URL');
  $f->description = $this->_('You can access page properties by wrapping them in curly braces.');
  $f->notes = $this->_('e.g. {name} will be replaced with the page name');
  $form->insertAfter($f, $form->templateLabel);

  $f = $this->modules->get('InputfieldRadios');
  $f->attr('id+name', 'hideViewLink');
  $f->addOption(1, 'Yes');
  $f->addOption(NULL, 'No');
  $f->value = $template->hideViewLink;
  $f->label = $this->_('Hide View Link');
  $f->description = $this->_('If this template should hide view links.');
  $form->insertAfter($f, $form->templateLabel);

  $event->return = $form;

// Save the extra template fields
$wire->addHookBefore("ProcessTemplate::executeSave", function() {
  $template = $this->templates->get($this->input->post->id);
  $template->set('hideViewLink', $this->input->post->hideViewLink);
  $template->set('rewriteViewURL', $this->input->post->rewriteViewURL);


Also need some custom CSS rules to hide the view link in the dropdown Save button.

.pw-button-dropdown .ui-menu-item {
  display: block

.pw-button-dropdown .ui-menu-item a[data-pw-dropdown-value="view"] {
  display: none;

Would still be interested in a cleaner solution though!

  • Like 1

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By jello_arch
      Hello, just installed and the link "login to admin" takes me to a blank page.  the url is processwire/processwire but there is not processwire directory inside the processwire directory. What is this supposed to be pointing to?
    • By joelplambeck
      Hi Guys,
      I'm trying to do my first migration to the customers existing server (IIS 10) . I ran the site as a subdirectory on my website for test purposes (everything works fine).
      Following the tutorial of Joss, I tryed the site on a local xampp server to make sure, it also works on a root directory. So far so good, everything works.
      Now I moved the files (from the xampp) to the customers server. The root/index page is shown but for every subpage i get 404 Errors...
      Hence I followed the troubleshooting guide for not working URLs:
      On the first sight, the .htaccess file is not recognized, therefore I contacted the host support. They said, it is recognized but not all modules are supported in the processwire .htaccess file. I did the "öalskjfdoal" test in the .htaccess file and didn't get a 500 Error.... BUT the rewrite rule from the hosts support, to proof the file is read, DID work... The support claims, they do not provide debugging... so basically the .htaccess file is recognized and working, but not throwing any errors (for whatever reason).
      Working rewrite rule (from support):
      RewriteEngine On RewriteBase / RewriteRule ^test\.asp$ index.html [NC,L] RewriteRule ^test\.html$ konzept.html [NC,L] RewriteRule ^test2\.html$ team.html [NC,L] The support said, a couple modules are not supported in the htaccess file, the supported ones are listed here: (I think mod_rewrite is supported)
      As I do not completely understand what exactly is happening in the htaccess file, I'm stuck. I tried all suggestions I found regarding this topic on the forum, but none of them solved the problem.
    • By rooofl
      I get often logged out from a web service built with PW.
      This web service has a large database and I use ListerPro to let my client browse and interact with it. After a couple of manipulations, or around 2-3 minute after logged in, the login page appears again. In this process, the last data submitted in the admin get often lost.
      About my configuration:
      The website is hosted on one of the first plans of OVH server (cheap ones), can it may be related to the problem? I uninstalled all the extra modules I used (Redirect, AdminCustomFiles). Nothing changed. The problem appeared recently only (since around 4 days) The website is still using basic http There is nothing interesting neither in `site/assets/logs/errors.txt` nor `site/assets/logs/modules.txt` I sometimes get the red error `This request was aborted because it appears to be forged.` together with `The process returned no content.` after a login attempt. I saw on this forum that it can be related to access rights and checked a couple of files permissions, and I *think* everything is fine. Thanks in advance for your help.
    • By Andreas Augustin
      Can I deactivate the renaming feature of images and the editing (croping, rotating etc) in the processwire admin panel?
    • By louisstephens
      So I have a project coming up soon and one of the goals was to use Google's AMP project for the project's mobile site. I have gone through the tutorials and think I have a good grasp on the matter, but there is still one roadblock I do not really know how to tackle. The site, which uses a responsive grid system, will look great on a mobile and desktop which was is fine by me. However, if a user comes from Google to one of our AMP pages (ie and clicks on a url, they will then be loading our responsive mobile pages and not the amp pages. For the sake of consistency, we really want to "force" users to stay on all the amp pages.
      My current thoughts on how to set up this task:
      Allow url segments for all pages using "/amp" Using a simple if statement, load the amp page if it exists <?php if($input->urlSegment1) { // add "&& $input->urlSegment1 == 'amp'" if you've more urlSegments include("partials/amp-page.php"); } else { include("partials/normal.php"); } ?>  
      However, I have hit a roadblock on appending "/amp" to all pages if they came to an amp page via Google, or even if they are on mobile and visit the site. Is this even possible to do, or should we just use the amp pages (if a user comes from google) and allow them to be active on our mobile pages?  We are just trying to give the fastest load times possible, as well as give a consistent look between mobile and desktop versions. As always, I really appreciate the ideas and help.