Thursday, March 24, 2011

How do I alter the #weight of the content generated by the upload.module?

On my Drupal powered website, I want to list available downloads at the top of a node page (within a narrow float:right <div>), not at the bottom where they normally appear.

Within my theme, I've overridden the theme_upload_attachments() function to generate a <div> of width 40%, but this is showing up at the bottom of the page.

Within the upload.module file is code that controls where the attachments are listed on the page:

// function upload_nodeapi(), line #284 of upload.module
$node->content['files'] = array(
    '#value' => theme('upload_attachments', $node->files),
    '#weight' => 50,
);

If I manually hack this #weight to -1, my custom list of attachments shows where I want, floating at the righthand side of the top of the content area.

However, I don't want to manually hack the core file upload.module, as my changes will be lost next time I apply an upgrade (say, for a security patch).

How/Where do I modify the #weight of content['files'] within my theme code?
Or, am I going about this the wrong way?

From stackoverflow
  • You'll need a module to do this, not just a theme. A module can implement hook_nodeapi(), which will give it a chance to change the contents of that $node->content array before it's rendered. If your module is named 'upload_tweaker' for example, you'd use the following function:

    function upload_tweaker_nodeapi(&$node, $op) {
      if ($op == 'view') {
        $node->content['files']['#weight'] = -1;
      }
    }
    

    Each module gets a crack at changing the node during this 'nodeapi' event; if you want to change the stuff that's added by one module, you need to make sure that your module loads after it. This can be done by naming it something like 'zzz', or by changing its "weight" field in the system table of your site's database. Modules can be weighted just like form elements.

    api.drupal.org has more information.

    Bevan : Thanks for the pointer - I've now implemented this little module and it is working exactly as I need. Cheers.

0 comments:

Post a Comment