Even though file description is activated and filled, the module seems not pick up the description as the link text. Related code is as follows:

// Use the description as the link text if available.
if (empty($variables['description'])) {
$link_text = $file_entity->getFilename();
}
else {
$link_text = $variables['description'];
$options['attributes']['title'] = $file_entity->getFilename();
}

empty($variables['description'] is always evaluated as true, so the else part does not have a chance to work it out. Not sure how to fix this.

Comments

passengerabcd created an issue. See original summary.

idebr’s picture

Status: Active » Needs review
StatusFileSize
new1.06 KB

Attached passes the correct description to template_preprocess_download_file_link(), so the description correctly overrides the file name.

oliveyrc’s picture

Applies cleanly and works for me.

oliveyrc’s picture

Status: Needs review » Reviewed & tested by the community
NewZeal’s picture

Patch added and committed in 8.x-1.x-dev. Please check and confirm that it works as expected.

oliveyrc’s picture

I've tried using the latest dev version but it now has broken the display of the files.

Upon looking it appears that the $elements array is getting nuked when passing the field attributes to the theme function, around here

if (isset($item->_attributes)) {
          $elements[$delta] = array('#attributes' => array());
          $elements[$delta]['#attributes'] += $item->_attributes;
          // Unset field item attributes since they have been included in the
          // formatter output and should not be rendered in the field template.
          unset($item->_attributes);
        }

But it does appear that this patch has been applied okay and I can see the change in the source.

Rich

idebr’s picture

Status: Reviewed & tested by the community » Needs work

There are currently three problems with the file description on HEAD:

  1. Downloads are no longer rendered, since the field list is overwritten by an empty array:
    $elements[$delta] = array('#attributes' => array());
    
  2. The description is no longer applied in template_preprocess_download_file_link(). Instead the title is hardcoded to
    $link_text = $variables['title'];
    
  3. The description itself is no longer available in the template_preprocess_download_file_link() function because the 'description' was removed from its theme function in file_download_theme()

@New Zeal: can you explain if the description should be able to override the file name? If so, for what field formatter option?

NewZeal’s picture

My apologies, the cat has been fiddling with my code :)

  1. '+' restored in
    $elements[$delta] = array('#attributes' => array());
  2. Description reapplied
  3. Description added to theme function
    'download_file_link' => array(
          'variables' => array('file' => NULL, 'title' => NULL, 'description' => NULL, 'attributes' => array()),
        ),
    

Pushed to 8.x-1.x.

idebr’s picture

Status: Needs work » Fixed

The description now overwrites the filename (or any other title selected in the file download file formatter options) on 8.x-1.x.

NewZeal’s picture

Ok, I've had another look at the patch that you supplied and I see it is overwriting the $title for which we have options selected. Therefore I have reversed the original patch.

      $elements[$delta] = array(
           '#theme' => 'download_file_link',
           '#file' => $file,
-          '#description' => $title,
+          '#description' => $item->description,
           '#cache' => array(
             'tags' => $file->getCacheTags(),
           ),

There is some confusion in that the template_preprocess_download_file_link() function refers to description when it should be title (which I have now changed). I'm not sure where $item->description comes from or we would want to use in our output.

The description is still being passed, so it is still available in the template. Here is the output from the formatter:

$elements[$delta] = array(
          '#theme' => 'download_file_link',
          '#file' => $file,
          '#title' => $title,
          '#description' => $item->description,
          '#cache' => array(
            'tags' => $file->getCacheTags(),
          ),
        );

So the patch is not actually being reversed.

oliveyrc’s picture

Version: 8.x-1.0-beta2 » 8.x-1.0-beta1

Have tested latest version 8.x-1.0-beta3 and it works as expected.

Cheers

oliveyrc’s picture

Version: 8.x-1.0-beta1 » 8.x-1.0-beta3

Fixing version string

idebr’s picture

Status: Fixed » Needs work

It appears this behavior was changed (again) in 8.x-1.x. In HEAD the description is not used.

The description is added by the File module when the option 'Enable Description field' is selected in the Field settings. This option is explained as 'The description field allows users to enter a description about the uploaded file.'. Then in the content edit form, the description mentions: 'The description may be used as the label of the link to the file'.

The file module uses the description as the link text when a user enters it, see template_preprocess_file_link() at file.module:1263

  // Use the description as the link text if available.
  if (empty($variables['description'])) {
    $link_text = $file_entity->getFilename();
  }
  else {
    $link_text = $variables['description'];
    $options['attributes']['title'] = $file_entity->getFilename();
  }

Users entering a description may expect the description to be displayed as the link title, but this is currently not the case. Perhaps the option 'Title of file' should be reworded to 'Use the default file naming behavior' and let the description override the filename as the file.module does?

NewZeal’s picture

StatusFileSize
new953 bytes

OK, I see now. How about we add the description as one of the options for the link title? See patch.

I should note that the patch you originally supplied overwrote our title options in favour of the file description. The options allow the link title to be altered separately for each instance where the formatter is used, such as in Views. That is not the case with the file description.

idebr’s picture

Status: Needs work » Reviewed & tested by the community

OK, I see now. How about we add the description as one of the options for the link title? See patch.

Yes, that will work. Keep in mind the description may be empty, since it is an optional field. I suppose the link text will default to the filename in template_preprocess_download_file_link(), which matches the behavior for the file.module

NewZeal’s picture

Keep in mind the description may be empty, since it is an optional field. I suppose the link text will default to the filename

We should test just that. We are currently using:
if (empty($variables['title']))
I'm not sure if that returns empty if string is empty.

NewZeal’s picture

Status: Reviewed & tested by the community » Fixed

As far as I know this issue is now fixed.

NewZeal’s picture

Status: Fixed » Closed (fixed)