2

By installing File Entity module I was hoping to be able to control the way all file items are rendered, for instance I need: file rendered as a link wrapped inside an <li> element, or a link with a given class to force display: block. I must say I have now more disorder among the widget, formatters and displays than I used to have before, and I can see no way to control the markup.

Please shed light on me, I assume it's my ignorance rather than the module imperfection. Thanks.

  • Since January when I asked this question I learned quite a bit about Drupal and managed to write custom formatters to do what I wanted. I still think though that for newbies some formatting options can be difficult. – Artur Jun 29 '12 at 14:23
  • Could you share what you did perhaps as an answer? – digitgopher Aug 26 '15 at 18:56
0

Since I've been no longer into Drupal for a long time now, I'm even not sure I remember the answer. But I managed to dig up the code below which smells like an answer @digitgopher asks for. So FWIW, and sorry for somewhat messy comments hanging around:

<?php

/**
* Implements hook_field_formatter_info().
*/
function fileformatter_field_formatter_info() {
  return array(
    'fileformatter_formatter' => array(
      'label' => t('File Formatter'),
      'field types' => array('file'),
      'settings' => array(
        'link_text' => '',
      )
    ),
  );
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function fileformatter_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state){
  //This gets the view_mode where our settings are stored
  $display = $instance['display'][$view_mode];

  //This gets the actual settings
  $settings = $display['settings'];

  //Initialize the element variable
  $element = array();

  //settings
  $element['link_text'] = array(
    '#type' => 'textfield',
    '#title' => t('Link text'),
    '#description' => t('The text to appear next to icon. Leave empty for raw file name. Possible tokens: %fn = file name, %fd = file description, %cp = node title'),
    '#default_value' => $settings['link_text'],
  );

  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function fileformatter_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = array();


  $summary = t('will be labeled ');
  $summary .= ($settings['link_text'] == '') ? t('using file name') :  $settings['link_text'];

  return $summary;
}

/**
 * Implements hook_field_formatter_view().
 */
function fileformatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $elements = array();
  $settings = $display['settings'];

  foreach ($items as $delta => $item) {
    $html = '';

    // LINK TEXT
    $link_text = ($settings['link_text'] == '') ? $item['filename'] : check_plain($settings['link_text']);

    /* if (module_exists('token')) {
      $link_text = token_replace($settings['link_text'], array('node' => $entity));
    } else { */
      $link_text = str_replace('%fn', $item['filename'], $link_text);
      $link_text = str_replace('%fd', $item['description'], $link_text);
      if($entity_type == 'node'){
        $link_text = str_replace('%cp', $entity->title, $link_text);
      } else {
        $link_text = str_replace('%cp', '', $link_text);
      }
    // }

    // last resort, so to not allow for empty links
    if($link_text == ''){
      $link_text = $item['filename'];
    }

    // ICON - as CSS class background, which reduces file requests and allows for easier vertical-alignment
    $dashed_mime = strtr($item['filemime'], array('/' => '-'));
    /* // img method:
      $icon = theme('file_icon', array('file' => file_load($item['fid']), 'icon_directory' => drupal_get_path('module', 'file') . '/icons'));
      $icon_html .= '<span class="link-icon">' . $icon . '</span>';
    */

    // LINK MARKUP
    $url = file_create_url($item['uri']);
    $link_options = array(
      'html'        => TRUE,
      'attributes'  => array(
        'title' => $item['filename'],
        'class' => 'file file-formatted ' . $dashed_mime . ' file-fid-' . $item['fid'],
        'type' => $item['filemime'] . '; length=' . $item['filesize'],
        'target' => '_blank',
       ),
    );

    $html .= l($link_text, $url, $link_options);

    $elements[$delta]['#markup'] = $html; // Assign it to the #markup of the element
  }
  return $elements;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.