views:

31

answers:

1

Hi.

I'm working on a drupal module, and one thing that it needs to do is modify the display of cck file fields automatically (using template files is not an option). For example, this array:

$node->field_images[0]['view'];

That is what I would like to get into. The 'view' part will output an image based on the display settings for the content type. I would like to attach a link to each image before the node is displayed to the user.

One thing I've tried:

function mymodule_nodeapi(&$node, $op, $teaser, $page) {
      switch ($op) {
          case 'view':
            $node->content['field_images']['view'] = array(
              '#value' => "hello",
              '#weight' => 10
            );
          break;
      }
 }

This inserts the text one time after the last image. I tried using a foreach to loop through all of them and produced no results at all.

I've also tried this:

function mymodule_nodeapi(&$node, $op, $teaser, $page) {
      switch ($op) {
          case 'view':
              // Attach link           
              foreach($node->field_images as $media) {
                  $media['view'] .= generate_link($node->nid, $media);
              }           
          break;
      }
    }

It seems like it should work, but it doesn't. I've searched everywhere for solutions without any luck.

If someone could help me out I would really appreciate it.

Thanks a lot.

A: 

Edit

To modify the CCK values before the page is loaded outside the templating system, use the alter op in hook_nodeapi. view is called before the node is rendered, alter is called after.


As you noticed, the CCK fields are not populated in time for the places you're trying to put code. Instead, you need to use a preprocessing function—template_preprocess_node()—which will let you modify values just before the rendered output is displayed.

function mymodule_preprocess_node(&$variables) {
  // $variables['field_name'] contains the CCK array for field_name
}

This won't change the rendered output of $content in your node template (unless you really want to get into resetting $variables['content'], but it will allow you to use $field_name in your template with your preprocessed values.

Alternatively, you could create your own custom formatter for the field, which will affect the rendered output of $content. Check out googletorp's blog post on Creating a field formatter for CCK.

Mark Trapp
I'm not sure this will work since preprocessing a node only makes the variables available to template files. I don't have the option to use template files. This module needs to automatically generate and attach the links to each filefield once its enabled. Isn't hook_nodeapi the only way to do that?
eatsleepdev
Ah, you are correct. I've updated my answer.
Mark Trapp