Jump to content

Prefill Fields


WiSch
 Share

Recommended Posts

Hi.

I'm new here and started today to take a look at processwire.

I created some fields (repeater, float, datetime...) to insert jobs I've done for my customers. Later I want to get a report of them.

I can fill up all needed fields by hand, but is there a way to

1. fill my field "total time" with an calculated value based on my fields "start datetime" and "end datetime"?

2. fill my field "hourly rate" based on the value of my fieldvalue of "user" with wich I want the value of my field "hourly rate" out of the depending user data?

Maybe it should be better to set up an form in the frontend to insert all this values and prefill the wanted fields with a little ajax?

Any suggestions?

Thanks for your help.

  • Like 1
Link to comment
Share on other sites

Maybe it should be better to set up an form in the frontend to insert all this values and prefill the wanted fields with a little ajax?

That sounds like one possible way to do it. But maybe not ideal unless you are already having them fill these things out on the front-end.

You could always create a custom Fieldtype/Inputfield designed for this specific purpose. But if you are okay with the information being automatically populated when the page is saved, you could pretty easily create a module to do it for you. Such a module hooks before Pages::save() and looks for pages having the expected fields, then automatically populates the required value, before the page is saved. Let me know if I can describe further or give an example?

Link to comment
Share on other sites

Hi Ryan.

While I'm thinking about the result I want to get at the end I think the form in the frontend should be the best way.

I thought so, because later I want more than one user to fill in these job-infos. But each user shouldn't see the records of the others.

It'll be very helpfull for me, if you can point out an example how I implement a form that will do this job: Ask for the infos, e.g. calculate the total time with the given start- and endtime and write all together to the database. Examples for every substep will help me out also.

By the way, is there a modul for using forms in the frontend? I didn't find one here?

Thanks in advance for your support!

Link to comment
Share on other sites

  • 1 month later...

I'd greatly appreciate any help in creating such a module. I need a field that gets populated with data parsed from another field. I actually have working code for the frontend, but I think I need it for the backend.

I have this simplified code testcase in a successfully loaded module, but it doesn't seem to do anything. I have 2 fields: report_text (type textarea), and report_specialtext (type TextareaSpecial) -- it's meant to grab the contents of report_text, do a simple search and replace, and put the results in report_specialtext:

<?php

class FieldtypeTextareaSpecial extends FieldtypeTextarea {


public static function getModuleInfo() {
return array(
'title' => 'TextareaSpecial',
'version' => 100,
'summary' => 'Field that stores some text',
);
}



public function getSpecialtext($text) {
$out=str_replace("search for this", "replace with this", $text);
return $out;
}

public function init() {
		 $this->pages->addHookBefore('save', $this, 'calcMethod');
 }

public function calcMethod($event) {

		 $text=$page->report_text;

		 $specialtext = FieldtypeTextareaSpecial::getSpecialtext($text);

	 $page->report_specialtext = $specialtext;

		 $this->message("Hello World! Special text {$specialtext}. Normal text: {$page->report_text}");

 }
}

?>

Any help greatly appreciated!

Link to comment
Share on other sites

The $event just provides you with a way to pass data back and forth between the hooked object and method:

$event->arguments is an array of arguments sent to the hooked method. So $event->arguments[0] would be the first argument, $event->arguments[1] the second argument, etc.

$event->arguments('name') is another option where you can retrieve the argument by name, i.e. $event->arguments('page');

$event->setArgument($n, $value) or $event->setArgument($name, $value) lets you change the value of an argument. This is useful only for before hooks (though set to execute before the hooked method).

$event->arguments = $value; where value is an array lets you set all the arguments to the function at once.

$event->return is the value that will be returned from the function you are hooking. This is only useful for 'after' hooks (i.e. hooks that you set to execute after the hooked function, rather than before). You can both retrieve this value and set it, if you want to.

$event->object is the instance of the object that the hooked function exists in. So if you hooked Page::render, then $event->object would be an instance of Page.

See here for full documentation on hooks.

-----------

I'm not sure I've got enough understanding of what you are trying to do with your module to say too much, but it looks to me like your hook there might not actually be necessary. I think you've be better off overriding the savePageField method of FieldtypeTextarea.

public function ___savePageField(Page $page, Field $field) {
 $value = $page->get($field->name); 
 $value = $this->getSpecialText($value); 
 $page->set($field->name, $value); 
 return parent::___savePageField($page, $field); 
}
  • Like 1
Link to comment
Share on other sites

  • 2 years later...

Sorry if this is answered elsewhere, but I found only this old topic, so bringind it up)

Is there a way to populate value of a field based on the value of the other field or fields instantly without page save? I see two ways: ajax and js. Well, I am pretty sure there is, as I've seen Adrians's modules working. What I am looking for is some instructions, examples or (hopefully) a module with only options to chose)

My need is to calculate the end of a subscription based on a start date and the duration of a chosen option.

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