[development] Proper way to programmatically modifying a CCK node's nodereference field in 4.7

Rob Barreca rob at electronicinsight.com
Thu Sep 28 00:07:21 UTC 2006


Thanks to some help from dopry on IRC I've got a little handle on the 
CCK API. To programmatically add references to a CCK nodereference 
field, I'm doing something like below although it probably needs a 
little smoothing.

  $field = content_fields('field_related_blog_posts', 'content_initiative');
  if (is_array($project_nids)) {
    foreach($project_nids as $nid => $title) {
      $node = node_load($nid);
      $additions = content_field('load', $node, $field, 
$node->field_related_blog_posts, NULL, NULL);
     
      $already_exists = FALSE;
      foreach($additions['field_related_blog_posts'] as $delta => $array) {
        if ($array['nid'] == $blog_nid) {
          $already_exists = TRUE;
          break;
        }
      }
      if (!$already_exists) {
        $additions['field_related_blog_posts'][] = array('nid' => 
$blog_nid);
      }
      content_field('update', $node, $field, 
$additions['field_related_blog_posts'], NULL, NULL);
    }
  }

Of course the keys are:
  $additions = content_field('load', $node, $field, 
$node->field_related_blog_posts, NULL, NULL);
  content_field('update', $node, $field, 
$additions['field_related_blog_posts'], NULL, NULL);

Rob Roy Barreca
Founder and COO
Electronic Insight Corporation
http://www.electronicinsight.com
rob at electronicinsight.com



Rob Barreca wrote:
> Can anyone with some CCK knowledge point me in the right direction 
> with this? Just trying to manually add node's to a CCK nodereference 
> field.
>
> Thanks!
>
> Rob Roy Barreca
> Founder and COO
> Electronic Insight Corporation
> http://www.electronicinsight.com
> rob at electronicinsight.com
>
>
>
> Rob Barreca wrote:
>> I want to programmatically add related blog postings references to a 
>> CCK node's nodereference field called field_related_blog_posts. Would 
>> something like this be the proper way to do this in 4.7? I know the 
>> fapi pull in 5.0 would help with this.
>>
>> $node = node_load(5);
>> $node->field_related_blog_posts[] = array('nid' => $blog_nid, 'view' 
>> => '<a>..</a>');
>> node_save($node);
>>
>
>


More information about the development mailing list