Node reference that filters another node reference

Events happening in the community are now at Drupal community events on www.drupal.org.
Alaa Rihan2's picture

Hello!
Let's explain what I'm trying to get:

IN SHORT:
A node1 that has 2 CCK node references to node2 and node3 should be able to select any node2, and then, just be able to select some node3, just the ones that have a relation with node2 (so node3 already has a cck node reference set to node2).

DETAILED:
I have a site which contains several kind of nodes. I have species, subspecies and videos.

For each video, I have 2 node references:
1- one to select the species which is shown on the video
2- the subspecies of that species.

The species selection works just great, with autocompletion and so on.
The problem is that the subspecies displays all the subspecies available, but I need to "filter" that list.

Let me clarify the relation between species and subspecies. Each subspecies has a node reference to the species, so one species could have a list of its subspecies. Of course, a subspecies itself can't exists (must be related to it's species).

The CCK node reference that I added to my video node allows me to select the species just fine, but for the subspecies, the autocomplete field should be filtered and just display the subspecies that are connected to that species. A button to "Get subspecies" once the species has been filled it's an option.

I could even change the autocomplete of the video node, when selecting the subspecies, for a list, because most species have 3, 5 or 20 subspecies, but not hundreds, so a list could be useful too.

Now, an example:

Nodes: sp1, sp2, sp3, ssp1, ssp2, ssp3, ssp4, ssp5, ssp6, ssp7, video1, video2. (sp = species, ssp = subspecies, video...well, video :) )
ssp1 has a node reference pointing to the sp1
ssp2 has a node reference pointing to the sp1
ssp3 has a node reference pointing to the sp3
ssp4 has a node reference pointing to the sp3
ssp5 has a node reference pointing to the sp3
ssp6 has a node reference pointing to the sp3

So, when adding/editing video1, I select the sp1 using my autocomplete field, and now the system should let me try between ssp1 and ssp2, but not any of the other ssp!

If I select the species sp2, NO subspecies should be presented (in a list, in the autocomplete CCK node reference...) to be selected.

If I choose sp3, I will have available ssp3, ssp4, ssp5 and ssp6.

So, how may I filter that list, or that autocomplete field once I selected the species??

Any help will be appreciated! I "know" what's the hook_form_alter, but just the basics yet...

Thanks in advance!

Comments

want to access the external database in view...

mag2000's picture

I have the database external name : Mydbase
Following table are there.

Tables Fields
Company maincode, subcode, company name
Product product code, product name
Capacity maincode, subcode, product code, capacity, since.

one to many relation of company and capacity
one to many relation of product and capacity.

want to display in view

maincode, subcode,product code, company name,product name, capacity,since.

want to expose the product code filter as well

I have setup the setting.php db_url of external database.

Then i create the form in page for select the company. Once the user select the company i store the company code in session variable for reference. This variable i will use for fetch information in view. I will enter its value in default argument php code and view will provide me the capacity information of selected company. This page auto redirect the user to view page of capacity. by using drupal_got0() function. the code of the page is attached below.. In code i have one problem.I can not store the company name in variable,only able to fetch the information maincode and subcode. How the fetch the display value of drop down instead of value tag value...

I do not know how to use view for external database without the content type define in drupal, and do not have any cck field as well. If i can i want to expose the product code as well in the view.so user can select and view one product of select company.

+++++++++++++++++ code start +++++++++++++++++++++

<?php
if(isset($_SESSION['ses_maincode'])){
echo
"Company Main code Selected =  " . $_SESSION['ses_maincode'] . "<br/>";
echo
"Company Sub code Selected =  " . $_SESSION['ses_subcode'] . "<br/>";
echo
"<br />";
}
$output .= drupal_get_form('formexample_testform');

echo
$output;

function
formexample_testform() {
$myvalue=array();
$count = 0;
db_set_active('mydb');
$result = db_query('SELECT maincode,subcode,cname FROM {company}');
while (
$row = db_fetch_array($result)) {
$arrayvalue=$row[maincode]  . '+' . $row[subcode];
$myvalue[$arrayvalue]=$row['cname'];
$count = $count +1;
}
db_set_active('default');
$form['select_company'] = array(
'#title' => t('Select Company'),
'#type' => 'select',
'#description' => t('Pick the company '),
'#options' => $myvalue,
'#multiple' => FALSE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit')
);
return
$form;
}

function
formexample_testform_submit($form_id, $form_state) {
$name=$form_state['values']['select_company'];
drupal_set_message(t(' You have select the company %name',
array(
'%name' => $name)));

$mystring = $form_state['values']['select_company'];
$pos = strrpos($mystring, "+");
$endpoint = strlen($mystring) - $pos;
$_SESSION['ses_maincode']=substr($mystring,0,$pos);
$_SESSION['ses_subcode']=substr($mystring,$pos+1,$endpoint);


$urlstring='node/7';
/** . $_SESSION['ses_maincode']; */
drupal_goto($urlstring);

}
?>

+++++ code end here

Content Construction Kit (CCK)

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week