Jump to content
Andreas Augustin

Edit Article-URL for Preview

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?

Share this post


Link to post
Share on other sites

I just found, that I have to modify the function protected function ___getViewActions in ProcessPageEdit.module.

Is it good to just override this in a module?

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

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 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

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:
 

<?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

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 sambadave
      Hi there
      Short version of question
      Let's say I have a page in the admin that contains a field... Is it possible to output the content from that field on another page in the admin? Almost like a reference.
      Longer version of question (with example)
      A house builder with multiple (60+) developments. They want to be able to create notices/messages that can be added to one or many developments. Handy for things like regional covid lockdowns or temporary office closures due to bad weather.
      My approach for the admin editing options:
      Add each message to each development
      Pros: You edit the message on the development page in context
      Cons: Very time consuming and repetitive if the same message needs to be applied to 60+ developments
        Control all the messages from one admin page and say which development each message should be applied to
      Pros: Easier to add/remove messages to more than one development at a time. Control all messages from one place.
      Cons: Content is not added on development page, which is where typical admin users may expect to find it I went for option 2 due to flexibility, and created a page within the admin for global development notices. This contains a repeater with:
      Field for message to display Checkbox list of all developments. The user can select which ones to apply each message to It's working really well but the only thing is that if the user goes to a specific development in the admin, the relevant messages aren't displayed in context (as they aren't edited on that page and instead on the global development notices page)... which may cause confusion when a new staff member / content admin tries to edit the text but there is no field when they go to the development admin page where they expect to see it...
      Solution???
      I don't require the message(s) to also be editable on the development page, but I wondered if there was a nice way to show it/them somehow. I feel like I am missing something really simple as I'm sure ProcessWire will have a nice way of achieving this, or maybe there are field settings that allow this kind of thing to happen?
      Any ideas on approaches or similar experiences would be much appreciated, even if it is just a much simpler example with the content from one field being shown on another admin page to get the ball rolling.
      Thanks in advance for any advice :)
    • By humanafterall
      Hi,
      I have a URL field that will sometimes have relative/local URLs on a multilingual site, for example /contact/ 

      However the URL field does not seem to pick up when I'm on another language, for example /fr/ so I'm taken to the default language page for /contact/ rather than /fr/contact/
      Is there a way to make the URL fields play well with a multi-language site?
      Thanks!
       
    • By humanafterall
      Hi,
      I would like to set an admin template to 'https only' as recommended in the Processwire security docs.
      However if I do this it forces this setting locally too, resulting in https://localhost requests which result in an error page.
      Is there a simple way round this? Setting https for templates in the config?
      Thanks!
    • By DanielKit
      Hi. I'm currently stuck at the login page in my project. Once I enter my admin username and my password and press login, nothing happens. The page just reloads. However, the URL changes from http://myipaddress/processwire to http://myipaddress/processwire/?login=1. I've checked all of my server settings, and to my knowledge, all seems to be fine there. I don't know where to go from here.. Thanks in advance!
    • By Robin S
      A new module that hasn't had a lot of testing yet. Please do your own testing before deploying on any production website.
      Custom Paths
      Allows any page to have a custom path/URL.
      Note: Custom Paths is incompatible with the core LanguageSupportPageNames module. I have no experience working with LanguageSupportPageNames or multi-language sites in general so I'm not in a position to work out if a fix is possible. If anyone with multi-language experience can contribute a fix it would be much appreciated!
      Screenshot

      Usage
      The module creates a field named custom_path on install. Add the custom_path field to the template of any page you want to set a custom path for. Whatever path is entered into this field determines the path and URL of the page ($page->path and $page->url). Page numbers and URL segments are supported if these are enabled for the template, and previous custom paths are managed by PagePathHistory if that module is installed.
      The custom_path field appears on the Settings tab in Page Edit by default but there is an option in the module configuration to disable this if you want to position the field among the other template fields.
      If the custom_path field is populated for a page it should be a path that is relative to the site root and that starts with a forward slash. The module prevents the same custom path being set for more than one page.
      The custom_path value takes precedence over any ProcessWire path. You can even override the Home page by setting a custom path of "/" for a page.
      It is highly recommended to set access controls on the custom_path field so that only privileged roles can edit it: superuser-only is recommended.
      It is up to the user to set and maintain suitable custom paths for any pages where the module is in use. Make sure your custom paths are compatible with ProcessWire's $config and .htaccess settings, and if you are basing the custom path on the names of parent pages you will probably want to have a strategy for updating custom paths if parent pages are renamed or moved.
      Example hooks to Pages::saveReady
      You might want to use a Pages::saveReady hook to automatically set the custom path for some pages. Below are a couple of examples.
      1. In this example the start of the custom path is fixed but the end of the path will update dynamically according to the name of the page:
      $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'my_template') { $page->custom_path = "/some-custom/path-segments/$page->name/"; } }); 2. The Custom Paths module adds a new Page::realPath method/property that can be used to get the "real" ProcessWire path to a page that might have a custom path set. In this example the custom path for news items is derived from the real ProcessWire path but a parent named "news-items" is removed:
      $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'news_item') { $page->custom_path = str_replace('/news-items/', '/', $page->realPath); } }); Caveats
      The custom paths will be used automatically for links created in CKEditor fields, but if you have the "link abstraction" option enabled for CKEditor fields (Details > Markup/HTML (Content Type) > HTML Options) then you will see notices from MarkupQA warning you that it is unable to resolve the links.
      Installation
      Install the Custom Paths module.
      Uninstallation
      The custom_path field is not automatically deleted when the module is uninstalled. You can delete it manually if the field is no longer needed.
       
      https://github.com/Toutouwai/CustomPaths
      https://modules.processwire.com/modules/custom-paths/
×
×
  • Create New...