You are here

function webform_protected_downloads_node_view in Webform Protected Downloads 7

Implementation of hook_node_view().

File

./webform_protected_downloads.module, line 393
This file contains hook declarations and functions for the Webform Protected Downloads module.

Code

function webform_protected_downloads_node_view($node, $view_mode, $langcode) {
  if (!webform_protected_downloads_node_is_webform($node)) {
    return;
  }

  // check if the node has protected files
  if (webform_protected_downloads_node_has_protected_files($node->nid)) {

    // make sure that protected files are not displayed
    $disabled = array();
    foreach ($node->wpd['protected_files'] as $file) {
      if (!isset($node->content[$file->field])) {

        // the attribute is only there for files that have been set to display
        // on the node edit form and that have been protected afterwards via
        // this module, once the node form is hit and saved the property is no
        // more there, that's why we need to check that here
        continue;
      }
      foreach ($node->content[$file->field]['#items'] as $key => $item) {
        if ($item['fid'] == $file->fid) {

          // disable access
          $node->content[$file->field][$key]['#access'] = FALSE;

          // count the number of protected files for this field
          $disabled[$file->field] = isset($disabled[$file->field]) ? $disabled[$file->field] + 1 : 1;
        }
      }
    }

    // disable the whole fieldset if all files for a field are protected
    foreach ($disabled as $field => $count) {
      if (!isset($node->content[$field]['#items']) || count($node->content[$field]['#items']) <= $count) {
        $node->content[$field]['#access'] = FALSE;
      }
    }
  }
}