My task is to generate a node list (to be referenced in a CCK field) from a view's result. That view is argumented with a part of url (by setting up "arg()" as the argument of the view). Therefore, the listed nodes that user may choose from will be sensative to the different urls.
The first thing I tried is "using view in nodereference field". I tried that many times but it always show only one option to select, when the view produce multiple results. That is the bug that I am reporting.
The alternative method that I am working on is to use the view to generate "allowed values" in a SELECT field, rather than the nodereference field type in the first way. But I am young learner of PHP and I couldn't organize the correct PHP code to do that. I've tried following code but it didn't work:
$current = array();
$result = views_embed_view('MyView', 'default');
foreach ($result as $current)
return array(
0 => $current,
);
Anyone has idea about what the correct code should be? Thanks in advance.
Comment | File | Size | Author |
---|---|---|---|
#4 | view.txt | 2.05 KB | michaelcrm |
#4 | field.txt | 3.43 KB | michaelcrm |
Comments
Comment #1
michaelcrm CreditAttribution: michaelcrm commentedComment #2
merlinofchaos CreditAttribution: merlinofchaos commentedComment #3
yched CreditAttribution: yched commentedPlease provide exports of both your View and your nodereference field that tries to use it.
(post them as .txt file attachments).
You'll need to enable the content_copy module to export your field.
Comment #4
michaelcrm CreditAttribution: michaelcrm commentedHello yched, please see the attahced view and field. Please also see the below construction process of this demonstration.
Task: Provide user an opportunity to specify the page color when add a "story". For example, "node/add/story/14" will lead to the story-adding form showing the field "page color" optioned with the colours of the page #14.
Result: Field "page color" on "node/add/story/14" only show single allowed value to choose from, while the page #14 has four different colours. That being said, the view "get_color_of_page" function well in preview.
The following is what I did:
return arg(3);
"grey - Title: Page 1"
Comment #5
yched CreditAttribution: yched commentedHm, using a noderef field for this seems far-fetched.
You could use a text field with 'allowed value PHP' setting :
In there, grab the nid form the url, load the node, get the values of $node->field_color.
But you'll also need to handle the case of editing an existing story. PHP code for allowed values is eval()'d when displaying the node form.
What 'allowed colors' will you set when url is node/x/edit and you don't have the nid of the 'original' page ?
No idea for now...
Comment #6
michaelcrm CreditAttribution: michaelcrm commentedHello yched,
I agree with you that using views in such an application is a way unneccessarily long. The reason why I tried it is that I had no clue on how to write PHP codes to load values from other nodes. Enlighted by your previous comment,
return array(0 => array('value' => arg(3)),);
Then I need to write codes in the "Allowed values" for "field_page_color", and the code should do following things on node creation form "node/add/story/*" (the * is the nid of a "page"):
However, I tried dozens of times to do above jobs, I tried both node_load() and content_load(), but none of them worked. The key issue is I don't know how to grab the value of "field_page_id" from current page. I would further appreciate if you can kindly instruct me the complete code. Please see below one of wrong examples that I tried:
The result of above code is "field_page_color" has no allowed value at all.
The another question that I need your kind instruction is, in case the "field_color" on PAGE node is a nodereference field referring to multiple COLOR nodes, which means the above code will only return the nids of the Color nodes, how to transform these nids into an array of node titles (that we can return as fields' allowed values?)
I have been heading around within above issues for weeks, and I really appreciate your kind help!
Thank you very much!
Sincerely,
Michael
Comment #7
yched CreditAttribution: yched commentedThe key issue is I don't know how to grab the value of "field_page_id" from current page
You can't, on a node *creation* form, because there is no node yet, and hence no value for the field.
I think you'd need your code split in 2 cases, depending on the url : one for node creation form (node/add/foobar/*: grab the id from url), one for node edit form (node/n/edit : grab the id from the value of field_page_id)
seems dangerous, you already have a $node that should not be overwritten. Give your variable another name.
+ the return value for the 'Allowed values PHP' should be something like array('list', 'of', 'values').
Your code doesn't seem to build the correct array.
Other than that, I'm having a hard time following your node/field model...
Comment #8
clashar CreditAttribution: clashar commentedthese issues are related/similar:
#529406: Node Reference using Views returns only 1 result when there should be multiples
#896874: Using argument on Views 6.x-3.x-dev only return only 1 row to CCK Node-reference (also user-reference)