Jump to content

How to hook into "view" links on tree and page edit


steveooo
 Share

Recommended Posts

Ahoy captain!

I need to hook into 2 areas of ProcessWire:

- "view" page link on page tree

- "view" page link on page edit

(Are there more areas with these "view" links for pages or did I get everyone?)

How can I hook into that in order to change the "view" link url?

Arrr! It would be great if someone would help me with that!

– Pirate GlassedEyes :cool:

Link to comment
Share on other sites

A module is needed doing this.
How to build a module: http://wiki.processwire.com/index.php/Module_Creation
Get informed about hooks here: https://processwire.com/api/hooks/

This one should do the job

<?php

/**
 * ProcessWire Module Modify View Url
 *
 * @version 100
 * @copyright 2016 Christoph Thelen
 * @author kixe (Christoph Thelen note@qualyweb.com)
 *
 */

class ProcessModifyViewUrl extends Process implements Module, ConfigurableModule {

	static public function getModuleInfo() {

		return array(
			'title' => 'Modify View Url',
			'summary' => 'Provide alternative url for the action button \'view\' and the view tab under /page/edit/',
			'author' => 'kixe',
			'version' => 100,
			'singular' => true, 
			'autoload' => true, 
			'icon' => 'mail-forward'
		);
	}

	static public function getDefaultConfig() {
		return array(
			'url' => 'http://example.org'
		);
	}

	public function __construct() {
		foreach(self::getDefaultConfig() as $key => $value) {
			$this->$key = $value;
		}
	}

	public function init() {
		$this->addHookBefore('ProcessPageEdit::buildFormView', function($event) {
			$event->arguments(0, $this->url);
		});

		$this->addHookAfter('ProcessPageListActions::getActions', function($event) {
			$actions  = $event->return;
			$actions['view'] = array(
				'cn' => $actions['view']['cn'],
				'name' => $actions['view']['name'],
				'url' => $this->url
			);
			$event->return = $actions;
		});
	}

	public function getModuleConfigInputfields(array $data) {

		$inputfields = new InputfieldWrapper();
		$defaults = self::getDefaultConfig();
		$data = array_merge($defaults, $data);

 		$f = wire('modules')->get("InputfieldText");
 		$f->attr('name+id', 'url');
  		$f->label = __("Alternative Url");
		$f->value = $data['url'];
 		$inputfields->add($f);

 		return $inputfields;
    }
}

  • Like 2
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...