7

I have a block which I would only like displayed on a KYN node. I'm currently using the option to display the block only on pages with "node/*" which worked when I only was using one node type but now that I have two (kYNs and Pages) I need to come up with a solution.

I'd like to use some PHP that returns TRUE when the content type of a node KYN.

How can I achieve this?

2
  • 5
    The default block visibility rules in Drupal 7 provide options to show/hide blocks based on node type. Why do you need to use PHP?
    – sheena_d
    Jul 17, 2012 at 20:19
  • If interested in using php to determine a node's content type, then my node_load()-less approach on this related question should come in handy: drupal.stackexchange.com/a/238904/10645 Jun 20, 2017 at 15:20

4 Answers 4

11
// Only show if $match is true
$match = FALSE;

// Which node types
$types = ['kYn'];

// Match current node type with array of types
if (arg(0) == 'node' && is_numeric(arg(1))) {
  $nid = arg(1);
  $node = node_load($nid);
  $type = $node->type;
  $match |= in_array($type, $types);
}

return $match;

source : http://drupal.org/node/115419

0
2

To expand on sheena_d's comment:

In Drupal 7 showing a block for one or more specific node types is available out of the box, with no need to write any PHP.

Where you would normally add PHP in the block 'visibility settings' select 'content types' from the menu on the left. Then select the content types you wish the block to display on.

I hope this helps simplify this for someone.

Where you would normally add PHP in the block 'visibility settings' select 'content types' from the menu on the left.

Then select the content types you wish the block to display on

1

Blocks don't know nothing about the content area, so you have to check other conditions, like the current Drupal path:

$is_kyn_page = false;

if (arg(0) == 'node' && is_numeric(arg(1))) {
  $node = node_load(arg(1));
  if ($node && $node->type == 'kYN') {
    $is_kyn_page = true;
  }
}

Note that 'kYN' is the machine name of the node type, not the human readable name.

0
0

If you are asking as administrator of a Drupal site, you don't need to write PHP code to achieve what you described: Whenever it's a block you created from the UI, or it's a block implemented from a module, you can change for which content types the block is shown.

On admin/structure/block, find the block you want to show only for some content types. (In the following screenshots, I will use the System help block as example.)

screenshot

Click on the configure link that you find at the right side. You will access the settings for that block.

screenshot

Click on Content types. You will get the list of content type defined in the site.

screenshot

As the description says, selecting a content type will make the block appear on the nodes of those content types. Not selecting any content type will make the block appear on every node. (It means, there isn't the need to select every content type to show the block for all the nodes.)

If you are asking for a block implemented by a module you develop, then you can decide when the block is shown, in hook_block_view().

function mymodule_block_view($delta = '') {
  switch ($delta) {
    // Replace 'block_id' with the block identifier.
    case 'block_id':
      // $node->type is case sensitive; use the exact content type identifier.
      if ($node = menu_get_object() && $node->type == 'KYN') {
        // Add the block title in $block['subject'],
        // the block content in $block['content'], and return $block.
      }
      break;
  }
}

I used menu_get_object() because it loads the correct node object, even when the page is for a node revision (for example, /node/1/revisions/2/view).

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.