7

I tried to put this code in a custom block that uses PHP as input format, but it doesn't work in Drupal 7.

$settings = theme_get_settings('my theme');
if (!$settings[toogle_logo] = 0) {
  print '<img src="' . base_path() . $settings['logo_path'] . '" alt="my logo" />'; 
}

How should I output an image in a block?

0

2 Answers 2

9

There are four issues in the code shown in the question; two of them explain why the code doesn't work as expected.

  • theme_get_settings() has been removed in Drupal 7; instead of that function, the code should use theme_get_setting()
  • $settings[toogle_logo] is using a literal string without using the string delimiters; PHP will take toogle_logo is a constant and, not founding its declaration, it will think its value is the literal string 'toogle_logo'
  • As already said from tim.plunkett, !$settings[toogle_logo] = 0 should really be !$settings[toogle_logo] == 0; differently, the code is setting $settings[toogle_logo] to 0, and then checking its value isn't zero, which means the if() statement will never be executed
  • Instead of rendering an image by writing directly the HTML markup, the code should use the theming functions available in Drupal

The correct code should be similar to the following one.

if (theme_get_setting('toggle_logo')) {
  $image = array(
    'path' => theme_get_setting('logo'),
    'alt' => 'my logo',
  );
  print theme('image', $image); 
}

If you can avoid using the PHP filter, you really should. Consider creating a custom module that implements a block, or include the block implementation in an existing custom module you are already using for the site you developed.

0
4

One problem with your code is that you use =, not == in your if statement.

Sounds like a job for Blockify. And if you don't want to use "a whole module" just for that, you can see how it's done and use the correct snippet.

1
  • Yes, this was one of the problems.
    – radu c
    Mar 21, 2011 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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