Return to Snippet

Revision: 58612
at July 24, 2012 23:46 by chasecrawford


Initial Code
<?php

if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) { # if is node
  global $user; # get info of logged in user
  $nid = arg(1); # define node id
  $node = node_load($nid); # load node info

  $items = field_get_items('node', $node, 'FIELD_LABEL_HERE', $node->language); # get nodes by field value
  if($items) {
    foreach ($items as $item)
    {

      # loop through matching nodes
      # Example below: if a matching node has a field value equal to the uid of the logged in user, return false
      if($item['target_id'] == $user->uid) { return false; }

    }
  }

}

?>

Initial URL


Initial Description
This is a callback example that can be used to conditionally display blocks on specific pages. In this example, I load all nodes with a specific field and return false if the value of that field is equal to the user id of the logged in user.  The snippet in it's entirety probably won't be helpful, but it contains functions that have many applications.

Initial Title
Drupal Block Callback Example

Initial Tags
drupal

Initial Language
PHP