Drupal adding "add new" link using PHP code

456 Views Asked by At

I have a cck node type that contains a node reference to node gallery type. Since I don't want to actually create a new gallery for ever CCK node I have, I would like to do the fallowing. in the page that displays the cck node;

if the gallery reference exists for the given node, display a link to that gallery. If no gallery exists, I would like to display a link "add gallery", ideally, this would programatically create a new gallery node and reference. I would also like to automatically populate the title, as well as the author information (without giving the user access to this). I looked all over drupal.org to try and find the info but can't figure it out.

Thanks

1

There are 1 best solutions below

1
On BEST ANSWER
 <?php
    $node_A = node_load($some_nid);
    if(empty($node_A->field_type_gallery_node_ref[]['nid'] )// imp point this field is an array
    {
    //link to add gallery page
    //call a function for creating a node of gallery and return the nide and you can pass  //parameters like nid of A etc..then return the new node from the function
$node_GALLERY =  get_gallery_node_new($node_A->nid);
$node_type_A->field_type_GALLERY_node_ref[]['nid'] = $node_GALLERY;
}
    else
    { 
    //display this gallery
    }
    ?>

to get current node's nid :

<?php
if (arg(0) == 'node' && is_numeric(arg(1))) 

$nodeid = arg(1);

?>

or using any field value of CCK A (that you know) and applying a join with node table

$nid_ref_query ='SELECT nid FROM `content_type_A` cto join node n on cto.nid=n.nid where cto.field_some_field_you_know =' . $something;
$nid_array = db_fetch_array(db_query($nid_ref_query));
$nid = $nid_array['nid'];