I have a content type 'event' which requires a user to enter a location. Is there a way that I can allow the user to either select from a list of existing locations or to add a new location? Currently, it only allows users to enter a new location. If a new event is occurring at a previously entered location, how can he/she find that location and have it auto complete?

Thanks for any help.

Comments

joelstein’s picture

I would also like to know this.

hutch’s picture

You could create a node-type called say 'venue' which uses location cck to define a venue. Then add a Node reference cck field to your event which can be configured as a dropdown (might do autocomplete field too, I haven't got an example to hand)
You will need to create any venues first.
Try it out on a development site first ;-)

joelstein’s picture

I thought of that solution, also, but it provides a poor, confusing user experience.

I have an idea how this could work. On the node form, search for previous nodes authored by this user, and grab those locations. Then, add a new select field above the existing location fields which lists the previous locations, as well as a default option "New location". Send the location options to the page via drupal_add_js() as a setting. When the user selects a previous location, autofill the fields with these values.

With a simple hook_form_alter, I was able to get this working on my site. Wouldn't take too much work to make a patch, but it gets more complicated when thinking about all the different ways that somebody could have Location configured.

I'll paste it here for anyone who wants it. In my configuration, I was using the Location CCK field, and since the "name" attribute was required, I could easily just show the name value.

/**
 * Implementation of hook_form_FORM_ID_alter().
 * nodetype node form.
 */
function custom_form_nodetype_node_form_alter(&$form, $form_state) {
  $form['#after_build'][] = 'custom_nodetype_node_after_build';
}

/**
 * nodetype node form after build callback.
 */
function custom_nodetype_node_after_build($element) {
  global $user;

  // Add javascript file.
  drupal_add_js(drupal_get_path('module', 'custom') .'/nodetype-node-form.js');

  // Find existing locations for this user.
  $locations = array('' => 'New location');
  $settings = array();
  $result = db_query("SELECT location.*
    FROM node node
    INNER JOIN {content_type_nodetype} content_type_nodetype ON content_type_nodetype.vid = node.vid
    INNER JOIN {location} location ON location.lid = content_type_nodetype.field_nodetype_location_lid
    WHERE node.uid = %d
    AND node.type = 'nodetype'
    ORDER BY location.lid",
    $user->uid);
  while ($row = db_fetch_object($result)) {
    $description = join(', ', array_filter(array(
      $row->name,
      $row->street,
      $row->additional,
      $row->city,
      $row->province,
      $row->postal_code,
      $row->country,
    )));
    if (!array_search($description, $locations)) {
      $locations[$row->lid] = $description;
      $provinces = location_get_provinces($row->country);
      $row->province = $provinces[$row->province];
      $settings[$row->lid] = $row;
    }
  }

  // Add these to the location form.
  $element['field_nodetype_location'][0]['existing'] = array(
    '#type' => 'select',
    '#title' => t('Location'),
    '#options' => $locations,
    '#id' => 'edit-location-selector',
  );

  // Add existing locations' data to the page.
  if (!empty($settings)) {
    drupal_add_js(array('location' => $settings), 'setting');
  }

  return $element;
}

Here's the javascript:

Drupal.behaviors.customNodetypeNodeForm = function(context) {

  // Autofill previous address.
  $('#edit-location-selector').change(function() {
    var val = $(':selected', this).val();
    if (val != '') {
      var location = Drupal.settings.location[val];
      $('#edit-field-nodetype-location-0-name').val(location.name);
      $('#edit-field-nodetype-location-0-street').val(location.street);
      $('#edit-field-nodetype-location-0-additional').val(location.additional);
      $('#edit-field-nodetype-location-0-city').val(location.city);
      $('#edit-field-nodetype-location-0-province').val(location.province);
      $('#edit-field-nodetype-location-0-postal-code').val(location.postal_code);
      $('#edit-field-nodetype-location-0-country').val(location.country);
    }
  }).change();

};
rwt’s picture

Is there any module for that? How would I port your code to Drupal 7? Thanks

legolasbo’s picture

Issue summary: View changes
Status: Active » Closed (outdated)

Closing old D6 issues as D6 is end of life

mattbk’s picture

@legolasbo, what's the protocol for requesting this for D7? Should the version on this issue be changed, or should a new issue be opened with the new version?

legolasbo’s picture

@mattbk,

That depends on the type of issue and it's relevance to currently supported branches. If we are for instance talking about a bug which is still present in a newer version, then the issue should be re-openend and assigned to the last (currently supported) version where the issue occurs. Feature requests are usually re-assigned to the current development branch.

As for this issue, it is filed as a support request, but feels more like a feature request (for location autocompletion), which would make re-opening, changing the type to feature request and setting it to 8.x seem a reasonable choice. However, it has had very little activity in it's lifetime and only contains some ancient copy-pasted code which doesn't make the comments in question really valuable. I would therefore choose to open a new issue (feature request) for the d7/d8 branch, write a thorough issue summary and reference this issue.

I hope the above helps you out. If you have any more questions, feel free to ask.