Problem/Motivation

The hook_block_view_alter() function in the Block Example doesn't work as written. The problem is that it eliminates all content that is not a string.

if (!isset($data['content']) || !is_string($data['content'])) {
    return;
  }

But the configurable block created by the module returns the content in $data['content']['#markup'], an array.
Also, blocks created in the UI return the title in $block->title. As written, the only blocks it will affect are blocks created in code that return a string for $data['content']

Proposed resolution

That leaves three possibilities:

  1. Change the code to work for the configurable block created by the module.
  2. Change the code to work for UI created blocks or other blocks created programmatically that return a string.
  3. Change the code to accommodate both.

Remaining tasks

  1. The original poster tried to write a solution to fit #1. He says it doesn't work. Possibly because he targeted the default title then changed it in the UI.
  2. The patch in comment #3 offers a solution for #2 above but can be ignored.
  3. The patch in #7 solves #3 and needs to be reviewed. Tests need to be revised.

User interface changes

n/a

API changes

n/a

Original report by dashohoxha

First of all, I found out that it had some bugs and corrected it like this:

/**
 * Implements hook_block_view_alter().
 *
 * This hook allows you to modify the output of any block in the system.
 *
 * In addition, instead of hook_block_view_alter(), which is called for all
 * blocks, you can also use hook_block_view_MODULE_DELTA_alter() to alter a
 * specific block.
 *
 * We are going to uppercase the title of any block if the string "magic string"
 * is encountered in the content. If we were changing only our block using
 * hook_block_view_MODULE_DELTA_alter to do this, we would have used the
 * function:
 * block_example_block_view_block_example_example_configurable_text_alter()
 *
 * To demonstrate the effect of this hook, you can use the
 * 'configurable_text_string' block created by this module and add the
 * text 'magic string' into the configuration.
 */
function block_example_block_view_alter(&$data, $block) {
  if ($block->module != 'block_example') { return; }
  debug($data, 'data', true);

  // If the content contains the string: 'magic string', uppercase the title.
  if (strstr($data['content']['#markup'], 'magic string')) {
    $data['subject'] = isset($data['subject']) ? drupal_strtoupper($data['subject']) : '';
    debug($data['subject'], 'data_subject');
  }
}

However it still does not work. The subject seems to have the default value, which is later replaced by the custom value. I don't know how to fix this.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rfay’s picture

Status: Active » Postponed (maintainer needs more info)

Hi - Thanks for your report.

Please, though, explain *what* bugs. That's the first thing in an issue. Explain what you expected and what happened instead.

Then, since you've got the ability to fix it, please provide a patch that fixes the problem you've discovered.

You can set this to "active" or "needs review" (if you've provided a patch) when you respond. Please feel free to edit the node itself and put a good issue summary there.

Thanks,
-Randy

jn2’s picture

I've been meaning to look into this for awhile.

As I see it, the problem is that strstr only works on a string, and the configurable block returns an array. So this 'magic string' exercise won't work on the block created by the module.

It does work on another block I tried it with. This one was created in code by a different module and has the title in $data['subject'].

It does not work on a block created in the UI. Those blocks place the title in $block->title, not in $data['subject']. I'll be back soon with a possible patch.

jn2’s picture

Title: Block example's hook_block_view_alter() needs work » hook_block_view_alter() does not work
FileSize
2.55 KB

Here's an attempt to fix this. Open to better ideas.

rfay’s picture

Status: Postponed (maintainer needs more info) » Needs review

Thanks so much! Could you update the issue summary and the title please?

jn2’s picture

Issue summary: View changes

Updating with better understanding of the problem and new patch.

jn2’s picture

Issue summary: View changes

minor edit

jn2’s picture

Title: hook_block_view_alter() does not work » Block example's hook_block_view_alter() needs work

@rfay
Do you know why the test status is 'postponed'? Don't think I've seen that before.

rfay’s picture

It's postponed because the branch test is broken. http://qa.drupal.org/pifr/test/136969

I fixed that in D6; I guess I didn't realize it was also broken in D7.

jn2’s picture

Title: hook_block_view_alter() does not work » Block example's hook_block_view_alter() needs work
FileSize
6.02 KB

I did more work on the patch, and now it works for both the configurable text block created by the module and any block created in the UI. It may be a bit confusing to read since there are two commits. I'll have to learn how to squash commits in Git.

Also, I ran the block tests, and this breaks the hook_block_view_alter test. (Passed the test when it didn't work, now breaks the test when it does.) I don't have much experience with tests, but I'll try to look at it.

jn2’s picture

Issue summary: View changes

minor edit

Status: Needs review » Needs work

The last submitted patch, fixed_hook_block_view_alter-1296452-7.patch, failed testing.

jn2’s picture

Status: Needs work » Needs review
FileSize
4.18 KB

This one should work. Test is fixed.

jn2’s picture

This one should work. Test is fixed.

Hmmm. Don't know why this saved twice.

rfay’s picture

Status: Needs review » Needs work

Thanks for all your effort on this!

This seems too complex to me. I worked on it a bit on the plane yesterday but didn't quite get it done. The idea is use drupal_render() (on a copy of $data) to render the $data and look for the string in it. Becomes much simpler.

It's OK with me to completely change the driver. In other words, we're trying to demonstrate something. It *doesn't* have to be "looking for a string in the content". We're just using an arbitrary thing.

Let's either render or use a different approach. The fact that I did some random thing in D6 doesn't mean we have to stick with that demonstration. We just want to do an alter that people can understand :-)

rfay’s picture

Status: Needs work » Needs review
FileSize
6.04 KB

Here's a new version for your review. This one provides another sample block (to be capitalized) and triggers off of the block title instead of other stuff.

jn2’s picture

This demonstration is much cleaner. I tried it out, created a block with the word 'uppercase' in the title, ran the tests. Everything works fine.

I tweaked the patch a bit, but you could go with #12 as is. There was one typo in the 'uppercase this please' block content (first sentence not capitalized); you might want to fix that.

I also expanded a little on the subject vs title keys. Might be overkill so could be omitted.

jn2’s picture

Here's another patch with slight clarifications, no code changes.

Status: Needs review » Needs work

The last submitted patch, hook_block_view_alter-1296452-14.patch, failed testing.

jn2’s picture

Status: Needs work » Needs review
FileSize
0 bytes

Oops. This one should work.

jn2’s picture

This is fixed for 7.x-1.x. Need to test it on D8. Edited: that patch didn't make right either.

rfay’s picture

Version: 7.x-1.x-dev »

The one that "should work", #16 was empty...

jn2’s picture

Yeah, I just noticed that.

jn2’s picture

Trying again. At least this one's not empty. :)

jn2’s picture

Version: » 7.x-1.x-dev

Needs to test again 7.x-1.x. (Testing postponed for some reason.)

rfay’s picture

8.x-1.x has been broken for some months. That's why it's postponed.

jn2’s picture

Okay, then this should test for 7.x-1.x.

Status: Needs review » Needs work

The last submitted patch, hook_block_view_alter-1296452-20.patch, failed testing.

jn2’s picture

Status: Needs work » Needs review
jn2’s picture

@rfay

Here's what happened. My FAPI reference habits got to me. There I don't make patches, I just push. So I made one small change to the patch in #13 and pushed before realizing I needed a patch for the issue.

#14 was a mistake made against Master, then #16 of course is empty because once the changes are pushed, there's no diff. Now #13 doesn't apply because the small change I made conflicts with it. #20 was an attempt to create a patch with the change, but that experiment obviously didn't work either.

Unless you want to revert the commit, this does remain fixed for 7.x-1.x. Tests on the local install run fine.

What happens with 8.x-1.x? Is it abandoned? The patch in #20 applied cleanly to 8.x-1.x. It works as it should, but testing is broken.

rfay’s picture

Thanks for your work on this.

I recommend just posting here the link to the commit that fixed it. Don't worry about a correct patch.

8.x-1.x is supposed to be maintained by tr, but there are a couple of difficult problems. See http://drupal.org/project/issues/examples?priorities=1

jn2’s picture

Version: 7.x-1.x-dev »
Status: Needs review » Postponed

Here's the commit:
http://drupalcode.org/project/examples.git/commit/446e475

The patch in #20 should work for D8, when that is ready. For now, I'll mark this version 8.x-1.x, postponed.

rfay’s picture

imo it's better just to commit it. otherwise your work will just get lost.

rfay’s picture

Version: » 7.x-1.x-dev
Status: Postponed » Fixed

#29 is no longer true, as we're starting over on 8.x. We'll fork 7.x later. So no need to work any further with this, IMO. Marking fixed.

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Issue summary: View changes

Updating for new patch