Jump to content

Auto-populate field


awebcreature
 Share

Recommended Posts

Hello there! 

In my new project i need to populate field with current user information (field), which are inserted on user registration on "Users" in admin panel of PW.

I add more fields in system template "User" which contain that information. 

The question is: how to auto-populate field with that information (field) of current user which create new page. That is in admin panel in PW, in template without file. 

Any advice are welcome! 

Thanks in advance! 

Link to comment
Share on other sites

Hi awebcreature. Here is something very simple that I've just put together but I haven't tested it and you'll have to apply it to your scenario. There may also be other ways of achieving this but just as an example:

<?php

class DuplicateField extends WireData implements Module {

    public static function getModuleInfo() {

        return array(
            'title' => 'Duplicates a field value',
            'version' => 101,
            'summary' => 'Duplicates a field value from user template to other template',
            'singular' => true,
            'autoload' => true,
            );
    }

    public function init() {
        // we hook into the page save function
        $this->pages->addHookAfter('save', $this, 'duplicateField');
    }

    public function duplicateField($event) {

        $page = $event->arguments[0];

        // when a page of type user is saved and our field's value has changed
        if ($page->template == 'user' && $page->isChanged("myfield")){

          // get the page that this field will get dupicated
          $other_page = $wire->pages("template=other_template, name=$page->name");

          // if we don't find this page return
          if (!$other_page->id) { return; }

          // update this other page
          $other_page->of(false); // set output formatting to off to update values
          $other_page->myfield = $page->myfield;
          $other_page->save();

          $this->message("$other_page->url also changed");
        }


}
  • Like 2
Link to comment
Share on other sites

Thats not how you call the user. Try this: $user->nameOfField

The question is where (template, field) to insert that " $user->nameOfField" to show content of that field.

Generally:

 Current user has entered value in the field "Administration", in current case "Administration 1"

 When that current user creates new page (different template), in this page there is another field that should accept the value of the field "Administration" of this current user.

Thank you for helping me! 

Link to comment
Share on other sites

The question is where (template, field) to insert that " $user->nameOfField" to show content of that field.

Generally:

 Current user has entered value in the field "Administration", in current case "Administration 1"

 When that current user creates new page (different template), in this page there is another field that should accept the value of the field "Administration" of this current user.

Thank you for helping me! 

Just to confirm - So this is about showing the same value in the admin and not the front-end of your website?

Link to comment
Share on other sites

The code goes in the template file. I'm still not sure I understand what you want to do, but I get the feeling that there is some confusion going on. What I'm interpreting from your question, you want to copy one of the fields that is in a User page, or even in all Users pages (You mean the default users pages in the system, right?)  to a field in another page. You could do that, and as onjegolders showed to you, it's not difficult. But you would create an unnecessary duplication of data. Unnecessary because you can use the original data when needed. To get that data, you only need t know how to access the user page and get the required field. To get the current user (the user that is viewing the website), do as i said before $user->nameOfField. To another specific user use find() or get() with the $users object http://cheatsheet.processwire.com/#users. If you want to create pages that are directly connected to a user, create a page field to refer to that user http://wiki.processwire.com/index.php/Page_Field. Like this you will have access to all the actual info of that user, instead of a copy of the content of one their fields.

  • Like 1
Link to comment
Share on other sites

Yes, in admin, not in front-end  custom template.

Then you'll want to add all the necessary fields to the other template then create a new module in your /site/modules folder, install it in the admin and using what I wrote as a starting point, (editing the field and template names to match your case), you may want to add another function in the module that handles cases where a user gets deleted?

It's not as complicated as it sounds but you'll want to try all this on a local install, while you get it working right.

We can help you as you go.

  • Like 2
Link to comment
Share on other sites

Ok, it's more clear now. But consider what I said before. I would avoid duplication as much as possible by using the page fieldtype. You can keep those fields in the user template, or move them to new page template, while keeping the User page connected to the new page.

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