Jump to content

Edit Article-URL for Preview


Andreas Augustin
 Share

Recommended Posts

Hello!

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.: http://processwire.webshop.com/en/news/some-article

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?

Link to comment
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('http://www.webshop.com' . $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

Link to comment
Share on other sites

  • 5 months later...

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.

Link to comment
Share on other sites

A addtitonal approach would be js....in combination with AdminCustomFiles module.

https://modules.processwire.com/modules/admin-custom-files/

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 overviewpage....here 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

Link to comment
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:
 

<?php
// 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. http://www.example.com/some-path
}


// Rewrite page list view URLs
$wire->addHook('ProcessPageListActions::getActions', function($event) {
  $page = $event->arguments(0);
  $template = $page->template;
  if ($template->hideViewLink) {
    $actions = $event->return;
    unset($actions['view']);
    $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
Link to comment
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
 Share

×
×
  • Create New...