2

I have a function that returns sets of variables. Now, I want to control the return value by using node status. Basically I dont want the value to be returned when the node is unpublished. Any idea? Here is the function by the way. I just need to add a condition in the return value like this if($node->status == 1){ return $omniture_variables; }

function my_module_omniture_variables() {
  $current_day = date('D');
  $current_hour = date('G');
  .....

  $omniture_variables['variables'] = array(
    ....
  );

  //Sets omniture variables
  if(module_exists('omniture')) {
  ....
  }

  if(count($extra_variables) > 0) {
    $omniture_variables['variables'] = array_merge($omniture_variables['variables'], $extra_variables);
  }

  return $omniture_variables;
}

With an idea coming from Jimajamma I come up with a solution. Here's the code:

//Get the node status. This is required in order to disable the return if the value is 0. 
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $node_id = arg(1);
  }
  $query = db_query("SELECT status FROM {node} WHERE nid = %d", $node_id);
  $result = db_fetch_object($query);

then get the result using $result->status;

Thanks Jimajamma for you very useful input. Thanks to marcvanged also for pointing me to a right direction. Cheers!

2

To avoid a node_load() you can do:

  $query = db_query("SELECT * FROM {node} WHERE nid = %d LIMIT 1", $number);
  $result = db_fetch_object($query);

and then look at $result->status.

  • any idea why $result->status has no value...cause I'm expecting it to be 0 or 1..any hint? – ninjascorner Dec 7 '11 at 22:42
  • ...is the code above applicable to every node the page is loading? I'm so clueless how drupal works regarding this... – ninjascorner Dec 7 '11 at 23:02
  • this will work on any node in the system, but you do have to pass it the nid of the node you want to check...that's the $number variable up there...so no, it has no clues what nodes you've loaded already... – Jimajamma Dec 8 '11 at 3:23
6

Getting a node's status is quite simple:

// Get the node with node ID 123.
$node = node_load(123);
// Get its status.
$status = $node->status;

That said... I don't know what your function looks like or in which context it's running, so depending on your situation, there may be an even better way to get what you want.

  • @marc..is there a way to avoid noad_load()...the reason we are avoiding noad_load is for performance issue... – ninjascorner Dec 7 '11 at 15:11
  • Here is my function... function my_module_omniture_variables() { $current_day = date('D'); $current_hour = date('G'); ..... $omniture_variables['variables'] = array( .... ); //Sets omniture variables if(module_exists('omniture')) { .... } if(count($extra_variables) > 0) { $omniture_variables['variables'] = array_merge($omniture_variables['variables'], $extra_variables); } return $omniture_variables; } – ninjascorner Dec 7 '11 at 15:18
  • If performance is very important, check out jimajamma's answer. I'm not sure how much faster it is, but it must make a difference, especially if you're doing it for a large number of nodes. In case you're loading a node that has already been loaded during the current request, then you can safely use node_load, because it keeps the node in static cache. – marcvangend Dec 7 '11 at 19:29
0

Using the Drupal 7 Database API, you can get the status of a node to determine if it is published or not like this:

// Determine if the node is published or not.
$query = db_select('node', 'n')
  ->fields('n', array('status'))
  ->condition('n.nid', $nid)
  ->execute();
$result = $query->fetchObject();
$is_published = $result ? $result->status : FALSE;

if ($is_published) {
  // Take action...
}
else {
  // Relax...
}
0

Drupal 8 method for avoiding using node_load:

$bundle = $node->bundle(); // or $bundle='my_bundle_type';
$query = \Drupal::entityQuery('node');
$query->condition('status', 1);
$query->condition('type', $bundle); // Optional
$entity_ids = $query->execute();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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