Jump to content

Using hook to change page->field in front-end edit


Hurme
 Share

Recommended Posts

Hello,
Any advice on how to hook page, so its fields get populated by something else than the actual saved content?

The below works fine for frontend (it's in ready.php), but has no effect in front-end editing or backend, and that is the problem here.

$this->addHookBefore('Page::render', function($event) {
  $page = $event->object;
  $page->exampleField = '123';
});

The information doesn't need to be saved into PW itself. It's been fetched and then on save, delivered to another database.

Link to comment
Share on other sites

            // for editing
            $this->addHookBefore('ProcessPageEdit::execute', function($e) { /* ... */ });

            // if page template = user
            $this->addHookBefore('ProcessProfile::execute', function($e) { /* ... */ });

 

Link to comment
Share on other sites

  • 3 weeks later...
// example 1: modify page title (runtime) for a specific template in Page Edit Interface
$this->addHookBefore('ProcessPageEdit::execute', function($e) {
	$id = wire('input')->get->id;
	if (!$id) return;
	$page = wire('pages')->get($id);
	if ($page->template != 'specific') return;
	$page->title = "I am a runtime title for pages using template 'specific'";
});

// example 2: modify fields in Profile
$this->addHookBefore('ProcessProfile::execute', function($e) {

	// not for superuser
	if (wire('user')->isSuperuser()) return;

	// the user should check 2 checkboxes in its profile
	// after checking and saving the related checkbox disappears
	// if both are checked a markup field 'info' is shown

	$profileFields = $e->object->profileFields;
	if (wire('user')->checkbox && wire('user')->checkbox_2) $profileFields[] = 'info';
	else {
		if (wire('user')->checkbox != 1) $profileFields[] = 'checkbox';
		if (wire('user')->checkbox_2 != 1) $profileFields[] = 'checkbox_2';
	}

	// update profile fields
	$e->object->profileFields = array_unique($profileFields);
});

 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...