I'm writing a custom element / field and I'm trying to find the best way to add a single element that all fields of this type can interact with the parent form. Options considered so far...

1) Direct interaction with the form using the element

From the first investigation of the FAPI, there does not seem to be any way that the element can interact with the parent form. The process callback gets a clone of the $form and so on...

Since fields can be nested, I can not simply append the helper function to the first element :(

2) Direct interaction with the form using the field

Is this possible?

3) JScript.

Setting the html and helper widget this way. It is a google map so this would make the form UI jump around a bit as it is added and rendered...

4) hook_form_alter

Not the fastest solution, but this is looking like the best candidate

Any help appreciated.

Background
--------------------------------------------------------------------------
I needed a simple Map marker field, and was surprised that there were nothing
that matched my requirements.

I simply need to add a google map with a listener to update the lat/lng/zoom parts.

The basic field is represented like:

listener radio group - 0 [lat - 23.7 ] [lng - 67.8][zoom - 8 \/ ] [icon - taxi ] [tool-tip: %token] [info window: %token]

A typical content type would be:

/-------------------------------------\
|           Google map                  |
\-------------------------------------/

Title:
[          ]
Location:
0 [   23.7 ] [    67.8][ 8 \/ ] [taxi ] [       ] [          ]

Highlights:
0 [   23.7 ] [    67.8][ 8 \/ ] [taxi ] [       ] [          ]
0 [   23.7 ] [    67.8][ 8 \/ ] [taxi ] [       ] [          ]
0 [   23.7 ] [    67.8][ 8 \/ ] [taxi ] [       ] [          ]
0 [   23.7 ] [    67.8][ 8 \/ ] [taxi ] [       ] [          ]

Photos: (I will also be using imagefield extended to tag imagefield images with geodata.)

/-------\   Title: [ ]
|Thumb|    Copyright: [ ]
\-------/   Taken by: [ ]
                
Location: 0 [   23.7 ] [  67.8][ 8 \/ ] [camera-left] [   ] [   ]
Description: [                   ]
[remove]

/-------\   Title: [ ]
|Thumb|    Copyright: [ ]
\-------/   Taken by: [ ]
                
Location: 0 [   23.7 ] [  67.8][ 8 \/ ] [camera-left] [   ] [   ]
Description: [                   ]
[remove]


[save] [preview]

Any help would be great

Comments

alan d.’s picture

I ended up using the form alter approach as this was fairly easy and trivial. Adds a prefix to the first found element in the top level of the form.

<?php
function smap_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
    $fields = content_fields(NULL, $form['#node']->type);
    $smap_point_fields = array();
    foreach ($fields as $key => $field) {
      if ($field['type'] == 'smap_point') {
        $smap_point_fields []= $key;
      }
    }
    if (!empty($smap_point_fields)) {
      foreach (element_children($form) as $child) {
        if (in_array($child, $smap_point_fields)) {
          if (!isset($form[$child]['#prefix'])) {
            $form[$child]['#prefix'] = '';
          }
          $form[$child]['#prefix'] = 'My Map' . $form[$child]['#prefix'];
          break;
        }
      }
    }
  }
}
?>

Alan Davison