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.

CommentFileSizeAuthor
#4 view.txt2.05 KBmichaelcrm
#4 field.txt3.43 KBmichaelcrm
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

michaelcrm’s picture

Assigned: michaelcrm » Unassigned
merlinofchaos’s picture

Project: Views (for Drupal 7) » Content Construction Kit (CCK)
Component: Views Data » Views Integration
yched’s picture

Component: Views Integration » nodereference.module
Category: bug » support
Status: Active » Postponed (maintainer needs more info)

Please 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.

michaelcrm’s picture

FileSize
3.43 KB
2.05 KB

Hello 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:

  1. Create field "field_color" in content type of page, and define multiple colours as allowed values.
  2. Create view "get_color_of_page". Under default display,
    • add "Content: color" as the field
    • add "Node: nid" as the argument, set "Action to take if argument is not present" is to "Provide default argument (PHP code)", and enter code:
      return arg(3);
    • create nodereference field "field_page_color" in content type of story, set to use the view "get_color_of_page", leave the argument blank (because it will retrieve its argument from the 4th part of the url)
    • creat a page, select four colors, and the uid is 14, for example
    • go to "node/add/story/14", and you will see at the there is only one color to choose from at the "page color" field. What's more, the title of node #14 has been automatically added besides the only color value, something like: "grey - Title: Page 1"
    yched’s picture

    Status: Postponed (maintainer needs more info) » Active

    Hm, 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...

    michaelcrm’s picture

    Hello 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,

    1. I added to Story content an additional integer field "field_page_id" to store the node id that "arg(3)" retrieved from the url.
    2. I set up the code for "field_page_id" :
      return array(0 => array('value' => arg(3)),);
    3. I deleted the nodereference field "field_page_color", and created the new text-select field "field_page_color"

    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"):

    1. like you said, "grab" the value of "field_page_id" (from current page) that has been automatically filled.
    2. load the node of grabbed nid
    3. retrieve the values of $node->field_color
    4. 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:

      $pass_content = content_load($node->nid);
      $pass_id = $pass_content->field_page_id;
      $node = node_load(array('nid' => $pass_id));
      $color = array();
      $color = $node->field_color;
      return array (0 => $color);
      

      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

    yched’s picture

    The 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)

    $node = node_load(array('nid' => $pass_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...