Question

I have a site which shows paintings by artists. On each painting page there is a section called 'related paintings'. This is a related field which provides the admin user the ability to tick other related paintings.

I would like to tick paintings by the same artist by default, so admin doesn't have to do it. I initially did this and it works, but obviously only ticks one painting:

return array(
    0 => array('nid' => '278')
);

Then I tried this, which does work in a template, i.e. it produces an array in the right format:

$artist_nid = $node->field_artist[0]["nid"];
$artist_paintings = node_load($artist_nid)->field_painting_nodes[0]              ["items"];
$a = array();
foreach ($artist_paintings as $painting)  {
   $a[] = array('nid' => $painting["nid"]);
}

But this doesn't work when called, I get the error:

warning: Invalid argument supplied for foreach() in /home/will/sites/modernprints/www/sites/all/modules/cck/content.module(2220) : eval()'d code on line 4.    

I'm guessing that $node doesn't exist or something in that context? What variables are available?

Was it helpful?

Solution

I've just realised that what I'm trying to do is completely flawed - when adding a new painting the node doesn't exist yet, so you can't get the artist and the artists paintings.

It might work if the user saved it first, but it's not very convenient.

The only way to do this properly would be with javascript, i.e. fire off an event once the user has chosen an artist to select the related paintings checkboxes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top