Views module quirks

From WolfTech
Revision as of 15:08, 29 September 2009 by Rjhodson (talk | contribs) (New page: ==Uploaded files have all the same link== When linking in uploaded files in a view, they all share the same link. Discussion and solution at [http://drupal.org/node/378724 http://drupal....)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Uploaded files have all the same link

When linking in uploaded files in a view, they all share the same link. Discussion and solution at http://drupal.org/node/378724

Specific solution: replace modules\views\modules\upload\views_handler_field_upload_fid.inc with http://drupal.org/files/issues/views_handler_field_upload_fid.inc_.txt

contents of file in case it gets misplaced

// $Id: views_handler_field_upload_fid.inc,v 1.4 2009/01/30 00:01:42 merlinofchaos Exp $
/**
 * Field handler to provide a list of roles.
 */
class views_handler_field_upload_fid extends views_handler_field_prerender_list {
  function construct() {
    parent::construct();
  }

  function option_definition() {
    $options = parent::option_definition();
    $options['link_to_file'] = array('default' => FALSE);
    $options['only_listed'] = array('default' => FALSE);
    return $options;
  }

  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $form['link_to_file'] = array(
      '#title' => t('Link this field to download the file'),
      '#type' => 'checkbox',
      '#default_value' => !empty($this->options['link_to_file']),
    );

    $form['only_listed'] = array(
      '#title' => t('Only show "listed" file attachments'),
      '#type' => 'checkbox',
      '#default_value' => !empty($this->options['only_listed']),
    );
  }


  /**
   * Render whatever the data is as a link to the file.
   *
   * Data should be made XSS safe prior to calling this function.
   */
  function render_link($data, $values) {
    if (!empty($this->options['link_to_file']) && $data !== NULL && $data !== '') {
      $path = file_create_url($values->filepath);
      $data = l($data, $path);
    }
    return $data;
  }
  
  function render($values) {
  	$files = array();
     // Support "only listed files" option.
    $where = '';
    if (!empty($this->options['only_listed'])) {
      $where = " AND u.list <> 0";
    }
    $result = db_query("SELECT u.vid, u.fid, f.filepath, u.description FROM {upload} u LEFT JOIN {files} f ON f.fid = u.fid WHERE u.vid = %d $where ORDER BY u.weight", $values->{$this->field_alias});
    while ($file = db_fetch_object($result)) {
      $files[] = $this->render_link(check_plain($file->description), $file);
    }
    return implode(', ', $files);
  }
}