Blocks configuration: the setting "Show description" does not work as defined.

"No" works. But "Teaser" not: even when selected the full description is shown in the block. Apart from the random block: there is the setting fully ignored. This block displays the links always with no description.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

GStegemann’s picture

More things to be checked:

In weblinks_blocks_block_view we add a css file.

function weblinks_blocks_block_view($delta, $options = array()) {
  drupal_add_css(drupal_get_path('module', 'weblinks_blocks') . '/weblinks_blocks.css');

However, how we do it is wrong. To have JS/CSS be attached to a block when caching is on, the #attached property should be used. See also https://api.drupal.org/comment/44413#comment-44413

Next we might have a problem with the added parameter $option to pass the limit to a block. On my test site the amount of links for the random blocks seems to be fixed at 5. Or the evaluation of the limit should be reversed.

  // If the limit is specified in the options use it, otherwise get it from the
  // stored variable for this block configuration.
  $limit = isset($options['limit']) ? $options['limit'] : variable_get('weblinks_maxdisp_blocks_' . $blktid, 10);

See also https://api.drupal.org/comment/48768#comment-48768

GStegemann’s picture

Regarding the random block $limit problem: after running more tests it turns out to be likely a caching issue or will be in effect after a certain amount of cron runs.

jonathan1055’s picture

Title: Blocks setting "Show description" has no effect » Blocks setting "Show description" not working as defined. css attachment

Regarding the limit I think it is working ok, at least it is corect for other blocks. The Random one is slightly different in that the content is not refreshed until the time is up, so it may not relect your desired setting until the next rebuild.

Thanks for noticing the css attachment. Yes, we'll do that.

jonathan1055’s picture

Done some investigation and I've found that the teaser display problem is created in function theme_weblinks_block_item() where we derive our own summary value:

  $items = field_get_items('node', $node, 'body');
  $node_format = $items[0]['format'];
  switch ($desc) {
    case 'none':
      $description = NULL;
      break;
    case 'teaser':
      $description = '<div class="description">'
                   . check_markup(text_summary($items[0]['value'], $node_format),
                                  $node_format, $langcode = NULL /* TODO Set this variable. */, FALSE)
                   . '</div>';
      break;
    case 'body':
      $description = '<div class="description">'
                   . check_markup($items[0]['value'], $node_format, $langcode = NULL /* TODO Set this variable. */, FALSE)
                   . '</div>';
      break;
  }

This probably worked in Drupal6 but in D7 if a separate summary value has been entered for the node there is a 'summary' key value in the $items[0] array. Hence we should check if this exists and use it as the input to text_summary in preference to $items[0]['value'].

Function text_summary() can take a third parameter which is the trim length. The default is taken from variable 'teaser_length' which seems to be maintained for backwards compatibility but is no longer used, reading through the update functions in node.install. The old default was 600 but this is no longer configurable like it was in D6. It must be in the field config settings somewhere. Anyway, that's half of the problem discovered.

I think the Random block behaves in the same way as the others - for testing it is better to have the 'update every' value blank so that the content is refreshed as soon as the settings are changed.

GStegemann’s picture

Hence we should check if this exists ...

I have found sample code in the documentation of text_summary and can check how to apply it in theme_weblinks_block_item(). There is also described how to determine the trim length.

I think the Random block behaves in the same way as the others

You are probably right. I will perform more tests.

jonathan1055’s picture

Status: Active » Needs review
FileSize
2.31 KB

Here's a patch which addresses the teaser problem. The trim length is recovered using

        $body_info = field_info_instance('node', 'body', 'weblinks');

I have also done the work on the css #attached problem, but I think it is better to review these two things separately. Although they do not really affect each other, it just makes the patch look more complex. Two of the changes in this patch are to split up creating arrays and returning them. This is better practice as it allows debugging to check the values before return.

GStegemann’s picture

Thanks. I have tested the patch and it works.

But before I mark it as RTBC I want that you review the similar implementation in weblinks_weblinks_preprocess which I wrote during the port to D7:

  if ($items = field_get_items('node', $node, 'body')) {
    $instance = field_info_instance('node', 'body', $node->type);
    $field_langcode = field_language('node', $node, 'body');
    $sanitize = 1;  // Sanitize link description always
  }
  else {
    // There seems to be no body field?!
    $description = 'none';
  }
  switch ($description) {
    case 'none':
      $variables['weblinks_body'] = NULL;
      break;
    case 'teaser':
      if (!empty($items[0]['summary'])) {
        $variables['weblinks_body'] = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], 'summary') : $items[0]['summary'];
      }
      else {
        if (isset($instance['display']['teaser']['settings']['trim_length'])) {
          $trim_length = $instance['display']['teaser']['settings']['trim_length'];
        }
        else {
          // Use default value.
          $trim_length = NULL;
        }
        $format = $instance['settings']['text_processing'] ? $items[0]['format'] : NULL;
        $variables['weblinks_body'] = text_summary($items[0]['value'], $format, $trim_length);
      }
      break;
    case 'body':
      $variables['weblinks_body'] = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], 'value') : $items[0]['value'];
      break;
I have also done the work on the css #attached problem

Yes, it is better to handle these two things separately. And to store return values into variables before returning from a function is also OK.

jonathan1055’s picture

Title: Blocks setting "Show description" not working as defined. css attachment » Blocks setting "Show description" not working as defined
jonathan1055’s picture

Thanks for drawing that to my attention. OK, comparing the two implementation, the differences are:

  1. In weblinks_weblinks_preprocess() we use $field_langcode = field_language('node', $node, 'body' which is good. This is marked as a @TODO in theme_weblinks_block_item() so I could add that to the patch
  2. weblinks_weblinks_preprocess() caters for no 'body' field. Do we really need to allow for this?
  3. weblinks_weblinks_preprocess() uses _text_sanitize() which I've not used before but it looks better than always calling check_markup() because it conditionally calls check_markup() or check_plain(). I could change the Blocks version to use this
  4. weblinks_weblinks_preprocess() uses the entire summary if that field exists, and does not trim it at the shorter length. I think this is better and I could change the Blocks version to do likewise.

I also noticed that in weblinks_weblinks_preprocess() the variable $sanitize is only set where we have a body field, which is fine. But given that it is always 1 the variable is never going to be used when it is not 1, so it can be removed, and the following code simplified by removing the conditional tests.

GStegemann’s picture

You're welcome.

My answers:

  1. OK.
  2. I would say Yes. You never know. Basically it is possible to remove the body field from the Web Links content type.
  3. OK.
  4. Hm, I'm not sure. In blocks it might be more reasonable to trim the summary.

Regarding variable 'sanitize': I copied the code from somewhere and left it in to be able to optionally turning sanitization on or off. But you're right: this option is really not needed.

jonathan1055’s picture

Here is a patch (with debug) to show my work so far.
1. I have added the $langcode support
2. I've catered for the body field being deleted (but not actually tested this yet)
3. I have used _text_sanitize()
4. Always trim the text for teaser, even if using the separte summary field. This involved making a temporary array to pass to _text_sanitize() but it works well.

Maybe some more work to do, but thought you should see (and check) the direction of the work so far.

GStegemann’s picture

Thanks. I will test your patch later.

But one question: why did you remove the 'div' and the class 'description'? Just for testing or for a specific reason?

jonathan1055’s picture

That was an error, sorry! I had previously added the lines

  if ($description) {
    $description = '<div class="description">' . $description . '</div>';
  }

after the switch, so that if we need to adjust the html or classes then it is is all done in one place, to be consistent. I had some editting problems yesterday and those lines got deleted by mistake. Here's an updated patch.

GStegemann’s picture

I've tested the patch now. I think it works.

The only thing which does not look pretty is the formatting of the Link Description. See https://www.drupal.org/files/issues/d7_weblinks_blocks_popular_2406139_8.... The left margin is too far left. I already corrected the class names in the Blocks css.

And your debug code caused a notice: Notice: Undefined variable: field_langcode in theme_weblinks_block_item() (line 292 of /var/www/html/cm7/sites/all/modules/weblinks/contribs/weblinks_blocks/weblinks_blocks.module).

jonathan1055’s picture

Yes I know that the styling is untidy - we discovered that in #2406139: Last click time and click count doubled or trebled up in title and it is going to be fixed in that issue as mentioned here

I've tested the patch now. I think it works.

Based on your comment, are you happy if I commit the attached patch? It is the same apart from all debug removed and the output returned via variable $output not directly in the return statement.

GStegemann’s picture

Status: Needs review » Reviewed & tested by the community

Yes, please commit. Thanks.

  • jonathan1055 committed 8c72fa6 on 7.x-1.x
    Issue #2429855 by jonathan1055: Blocks setting "Show description" not...

  • jonathan1055 committed 6bad6b0 on 7.x-1.x
    Issue #2429855 by jonathan1055: changelog.txt for block description...
jonathan1055’s picture

Status: Reviewed & tested by the community » Fixed

Thank you for your testing. We are making really good progress now.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.