Hi Guys,
I'm trying to get a nid passed through to the arguments of a view, I have a simple function (in template.php) to work out a nid based on the page url. This works great and I'm using it to change theme elements on some sections of the website. However I'm trying to pass the nid through to a view in order to filter content based on it.

Here's my function:

function GetAssociatedProject()
{
	$tempnodes = db_query("SELECT nid FROM node WHERE lcase(title) = '".str_replace('-',' ',strtolower(arg(1)))."' AND type = 'project' AND node.status = 1 AND language = '".UI_lang()."'");
	$tempnode = db_fetch_object($tempnodes);
	$myTempNode = $tempnode->nid;
	
	return $myTempNode;
}

Just hoping someone can help after a good few days of trawling drupal.org looking for advice.

Comments

dawehner’s picture

 However I'm trying to pass the nid through to a view in order to filter content based on it.

So you try to execute a view to find a certain nid and use this further? I'm not 100% sure what you mean witht this sentences.


	$tempnodes = db_query("SELECT nid FROM node WHERE lcase(title) = '".str_replace('-',' ',strtolower(arg(1)))."' AND type = 'project' AND node.status = 1 AND language = '".UI_lang()."'");

Please don't tell people the url of your site, because this code is not secure. Please read how to use the database api of drupal.

andrew_hoffi’s picture

Thanks for the tips on the Database Api.

I thought I could call the function within the PHP default argument for the view (called without php tags):

<?php
return GetAssociatedProject()
?>

This would pass the nid through to the view and allow me to filter other content as the nid should reference a OG node.

dawehner’s picture

Sure you can do this in the php argument default settings.

So did you tried this out? If yes, what is your problem ;)

andrew_hoffi’s picture

Yeah sure... it doesn't seem to think the function exists. I have called it from page.tpl.php and its working fine, but it doesn't work within the default php view argument

Fatal error: Call to undefined function GetAssociatedProject() in /public_html/sites/all/modules/views/plugins/views_plugin_argument_default_php.inc(48) : eval()'d code on line 1

dawehner’s picture

Status: Active » Fixed

Sure you should place this function in your module file, if it should be usable in views, not in the template.php

The template.php is loaded much later.

andrew_hoffi’s picture

Just want to give you a HUGE thank you... not a huge task in the end, but still needed the push in the right direction.

andrew_hoffi’s picture

Just wanted to check if there could be anything that would stop a function being accessed by a view? I have a custom module all working correctly as it can be used elsewhere, just not by the view.

dawehner’s picture

There is from my perspective no reason as long you have placed it in the .module file.

Status: Fixed » Closed (fixed)

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