$item) {
if (is_array($item)) {
$type = $item['#type'];
if ($type == 'fieldset') { // loop through all the subitems.
foreach ($form[$key] as $subkey => $subitem) {
popups_reference_alter_item($form[$key], $subkey, $subitem, $fields);
}
}
else {
popups_reference_alter_item($form, $key, $item, $fields);
}
}
}
}
}
}
function popups_reference_alter_item(&$form, $key, $item, $fields) {
$field_name = strstr($key, 'field_');
if (isset($fields[$field_name]) && $fields[$field_name]['type'] == 'nodereference') {
$type = $form['type']['#value'];
$field = content_fields($field_name, $form['type']['#value']);
if ($field['widget']['type'] == 'nodereference_select') {
$id = 'popups-reference-' . _popups_reference_counter();
$links = _popups_reference_links($field, $type, $id);
// Need wrapped with id for ahah replacement.
$form[$key]['#prefix'] = '
';
$form[$key]['#suffix'] = '
Add New: ' . implode(', ', $links) .'
';
}
else if ($field['widget']['type'] == 'nodereference_autocomplete') {
$links = _popups_reference_links($field, $type);
$form[$key]['#suffix'] = 'Add New: ' . implode(', ', $links) .'
';
}
}
}
/**
* Generates 'Add new...' link
* for each allowed content type
*/
function _popups_reference_links($field, $src_type, $id=null) {
if ($id) { // Creating link to select box.
$pclass = $id;
// Target just the select box for replacing.
popups_add_popups(
array('.'.$pclass=>array(
'targetSelectors' => array('#'.$id), // Only reload the select box element.
'behaviors' => array("Drupal.popups.collapsibleBehavior"),
))
);
}
else { // Creating link to autofill box.
$pclass = 'popups-reference-autofill';
popups_add_popups(
array('.'.$pclass=>array(
'noReload' => TRUE, // Don't refresh the original page.
'behaviors' => array("Drupal.popups.collapsibleBehavior"),
))
);
}
$links = array();
$all_types = node_get_types();
foreach ($field['referenceable_types'] as $type => $value) {
if (!empty($value) && user_access("create $type content")) {
$path = "node/add/". strtr($type, '_', '-');
$links[] = l("Add ". $all_types[$type]->name, $path, array('class' => $pclass), "destination=node/add/" . strtr($src_type, '_', '-'));
}
}
return $links;
}
function _popups_reference_counter() {
static $count = 0;
return $count++;
}