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 Pixrael
      Hi guys!
      What do you think about something like this? Because I use this option regularly in the Windows File Explorer breadcrumb and it's very useful. The drop-down menu can display only published/visible child pages. I've tried to find how to do it as a module, but I'm not a coder with enough skills for that ... or I don't know if I can use hooks to do that ...

    • By AAD Web Team
      I've put in a support request (as a followup on an existing one) for this but it's nearly 10pm Ryan's time, so I thought I'd ask on here just in case someone can help...
      Yesterday we purchased ProDrafts, but I couldn't get the key to validate. Every time I hit submit on the module settings screen the key would be removed from the field I entered it in, and then it would complain about the missing value. I put in a support request and Ryan responded last night. It turned out it was because we're behind a proxy/firewall. He gave me a fix to get around that issue - some code to go into admin.php. I put that code in and saved admin.php, but now I can't even get into our admin anymore!
      (Just to clarify, I took the code back out and saved admin.php but that made no difference. It's definitely not the new code causing the error.)
      The errors relate to a lack of permission on the ProDrafts module (see attached screenshot). We do have a key, but evidently the code Ryan gave me to add the key in via admin.php isn't taking effect before the error is thrown...?
      I tried removing the ProDrafts directory from the modules directory, but that hasn't helped.
      Is there a hook I can put in somewhere to tell it not to load ProDrafts at all, or not check the permissions? Or is there some way to manually uninstall it from the file system?

    • By SwimToWin
      In my world, Super Users / Editors should only be able to work with Pages and administer users.
      Everything else is the domain of the web master. The purpose is to prevent technically inexperienced editors from destroying core elements of a site, such as fields and templates.
      That leaves me with the question, how to deny Super Users access to Setup, Modules and Roles / Permissions?
      PS: May I also suggest that it shall be possible to set Permission for each of the main menu items - including their sub-menu items.

    • By SwimToWin
      How can I translate the admin buttons that are shown when mousing over a page in the admin interface ("Edit", "View", "New", "Move", "Unpub", "Hide", "Lock", "Trash")?

      The site uses the built-in Language Support module. I am here: /processwire/page/.
      I looked for files to translate in the Languages setup section (/processwire/setup/languages/edit/?id=1234), so far unsuccessfully because I was not able to identify a file to translate.
    • By fbg13
      Lets discuss the new admin theme, there has been very little talk about it, a few questions about its status and some simple designs by a few users.
      Have you played with it? Do you like it? Do you dislike it? Of course I'm not talking about its design, but the way its structured, how it works and the features it offers so far.
      I'm working on a purple color scheme for it and it turned out quite well (imo), but one thing I dislike is the use of iframes. I haven't used iframes before so it might be just me, but I'm struggling with them when it comes to javascript.
      When iframes are used all scripts are loaded in every frame, which can lead to unexpected results.
      Javascript is an easy way to add, remove, move etc. elements in the admin, but the iframes complicate things.
      What are your thoughts? Is it just me?
      Here's a screenshot of the purple theme: