Jump to content
awebcreature

Auto-populate field

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! 

Share this post


Link to post
Share on other sites

I've never used this functionality before, but surely you could call $page->createdUser in your template, and the custom fields will be available in the User object? Someone will have to correct me if I'm wrong, though.

What are the fields that you added to the system template?

Share this post


Link to post
Share on other sites

Thanks, Mike! 

Calling $page->user->nameOfField was the first what I though but I don't see where i how to call it. No template file.

That are fields in User page:

post-790-0-09739000-1411112948_thumb.png

Share this post


Link to post
Share on other sites

If you need to duplicate information in the admin from one template to the other you will need to create a simple module that hooks on page save or page add and updates the other template.

Let us know if you need some help getting started.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Calling $page->user->nameOfField was the first what I though but I don't see where i how to call it. No template file.

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

  • Like 1

Share this post


Link to post
Share on other sites

Not sure if you just want to display this information on the frontend? If so, no need for the module, just output as Diogo shows. 

Share this post


Link to post
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! 

Share this post


Link to post
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?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Macaco
      Hi there, 
      I have two templates that should talk to each other. One is for events which should access a list of artists (eventually add a new artist) and their images related to that event; the second is the artist page, which the user should be able to add or remove events that they participated, also mentioning which images are related to each event.
      Some sort of feedback loop between the two.
      I have tried searching around the community and google, but maybe I'm not formulating the question properly. 
      Thank you all for the amazing work with Processwire and the community support. I have made a few websites with PCW yet I'm still quite between beginner and intermediate.
    • By Robin S
      Thanks to @Macrura for the idea behind this module.
      Page Field Info
      Adds information about options in Page Reference fields. Supports InputfieldSelect and inputfields that extend InputfieldSelect:
      InputfieldSelect InputfieldRadios InputfieldSelectMultiple InputfieldCheckboxes InputfieldAsmSelect Requires ProcessWire >= 3.0.61 and AdminThemeUikit.
      Screenshots
      Field config

      Example of changes to inputfield

      Example of info field filled out in Page Edit

      Installation
      Install the Page Field Info module.
      Configuration
      In the Input tab of the settings for a Page Reference field...
      Tick the "Add info tooltips to options" checkbox to enable tooltips for the options in the field. Tooltips are not possible for Select or AsmSelect inputfield types so for those types you would want to tick the next option. Tick the "Append info about selected options" checkbox to append information about the selected options to the bottom of the inputfield. If the Page Reference field is a "multiple pages" field then the info for each selected option will be prefixed with the option label (so the user will know what option each line of info relates to). In the "Info field" dropdown select a text field that will contain information about the page, to be used in the tooltips and appended info. Of course this field should be in the template(s) of the selectable pages for the Page Reference field. Hook
      In most cases the "Info field" will supply the text for the tooltips and appended info, but for advanced usages you can hookPageFieldInfo::getPageInfo() to return the text. For example:
      $wire->addHookAfter('PageFieldInfo::getPageInfo', function(HookEvent $event) { $page = $event->arguments(0); // The page $inputfield = $event->arguments(1); // InputfieldPage $field = $event->arguments(2); // The Page Reference field $info = $event->return; // Text from the info field, if any // Set some custom text as the $event->return... });  
      https://github.com/Toutouwai/PageFieldInfo
      https://modules.processwire.com/modules/page-field-info/
    • By gregory
      Hi guys. I need to create a field (link field) that allows me to select the internal pages to use as links. Do you have any suggestions? Thank you.
    • By Hubris
      Hi there!
      I'm using some page reference fields to create lists of tags, categories, years, etc.. I'm able to find the pages like so:
      $pages->find("template=project, {$filter}={$page->title}"); Which dynamically does something like: 
      $pages->find("template=project, tags=Experimental"); Only if the value (the page name, like "Experimental") starts with letters. If it starts with numbers, find returns nothing.
      Why is this and how can I fix it?
    • By Robin S
      Another little admin helper module...
      Template Field Widths
      Adds a "Field widths" field to Edit Template that allows you to quickly set the widths of inputfields in the template.

      Why?
      When setting up a new template or trying out different field layouts I find it a bit slow and tedious to have to open each field individually in a modal just to set the width. This module speeds up the process.
      Installation
      Install the Template Field Widths module.
      Config options
      You can set the default presentation of the "Field widths" field to collapsed or open. You can choose Name or Label as the primary identifier shown for the field. The unchosen alternative will become the title attribute shown on hover. You can choose to show the original field width next to the template context field width.  
      https://github.com/Toutouwai/TemplateFieldWidths
      https://modules.processwire.com/modules/template-field-widths/
×
×
  • Create New...