0

I've set up a view that displays users and has exposed filters user first and last names. If there is no user matching the filter values, I'd like to display a link to create a new user and prepopulate the name fields with the values searched in views.

I know there's the prepopulate module and likely other methods to do the actual prepopulating, but I guess this question is more centered on how to grab the filter values so they can be added to the new link url as properly formatted arguments.

EDIT: Got this solved with help from the answer below. Ending up grabbing the filter values with the "exposed filter values" module and populating the field values in a custom module with this code:

function hook_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'user_register_form') {

        //Prepopulate the name fields with url args
        $field_lang = $form['field_name']['#language'];
        if(isset($_GET['first'])) {
            $first = $_GET['first'];
            $form['field_name'][$field_lang][0]['#default_value']['given'] = $first;
        }

        if(isset($_GET['last'])) {
            $last = $_GET['last'];
            $form['field_name'][$field_lang][0]['#default_value']['family']= $last;
        }
    }
}

1 Answer 1

0

After googling this question some, I came up with the following receipe:

  1. Install prepopulate and exposed filter data
  2. Modify exposed_filter_data.tpl.php provided by exposed filter data to produce a dynamic prepopulate URL for creating a new user

Hope this helps!

P.S.: Inspired from this discussion => https://www.drupal.org/node/1630198

5
  • Exposed filter data is exactly what I was looking for. Ended up skipping the prepopulate module and implementing some code into a hook_form_alter I already have implemented. Threw that code in as an edit to my question above. Thanks!
    – Mrweiner
    Mar 29, 2016 at 20:12
  • Cool idea, much leaner! Mar 29, 2016 at 21:41
  • I haven't had ton of experience so far optimizing security-specific concerns, but do you know if there's anything I should implement to the added code to address injection of any sort? I'd imagine the risk would be mitigated since it's just throwing values into a field, which is handled/validated on form submission anyway.
    – Mrweiner
    Mar 29, 2016 at 21:58
  • Not an expert myself, but I don't think this is the point where you address injection, rather on the output. And I agree, the form submission and subsequent database queries are expected to handle injection concerns. Mar 29, 2016 at 22:04
  • Sure! Any time! Mar 29, 2016 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.