Domanda

I am working on a real estate directory using Drupal 7 and Views. I have a content type (called offering) with all the necessary fields on it and I want to be able to create a share form that will auto-populate with data from the content type. So for instance when you visit that property's page and click on the share button, a form pops up with the property name, title, property image and the subject line already filled in and all the user has to do is enter a recipient email and hit send. I was able to do this with custom php/jquery but I am wondering if there are any Drupal modules that can achieve the same thing since my code is a bit buggy and I have 3 forms on 1 page (share form, contact support and contact transaction team).

Here is a test link http://greysteel.webmschf.com/?q=7-eleven-1

Here is some sample code of the node template that the share form shows up on

<form id="shareform" action="" method="post">
  <input type="text" name="shareto" />
  <input type="hidden" name="ttsubject" value="Greysteel Offering: <?php print $title; ?>" />
  <input type="hidden" name="teamemail" value="<?php print render($content['field_team_emails']['#items'][0]['value']); ?>" />
  <input type="submit" />
</form> 

The above works perfectly, but the form is hard coded and cannot be administered through Drupal's back end. Is there any way around this? I've tried using the Webform module but I cannot find a way to automatically insert the property data into the webform.

È stato utile?

Soluzione

You could use Javascript to accomplish this. You could print the value somewhere in the page, hide it with css, and on page load grab it's value and put it into the form field. that would be a quick solution.

Another way (probably your best option) is to write a hook_form_alter function that loads the node, grabs the data from it, and pre-populates the field.

function modulename_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'webform_client_form_###') {
    $node = node_load(#);
    $form['ttsubject']['#default_value'] = field_view_field('node', $node, 'field_name');
  }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top