Description:

Scenario:

I have 2 different content types, type1 and type2. I have a vocabulary (vocabulary ID value of 4) with these terms:

-termA (the term ID is 7 for termA)
--termA1
--termA2
-termB
--termB1
--termB2

I have a field_topic content taxonomy field that is shared by type1 and type2.

Question:

What do I have to enter in the "PHP Code for selecting the parent term" box in order to limit the list of selectable terms for type1 (to only allow the user to select termA, termA1, or termA2), but show the whole list for type2?

I was thinking it would go something like this:

if($form['type']['#value']='type1' {
  return '7';
}else {
  return '0';
}

...but that doesn't work (it's as if the "if" function doesn't work and it returns the first value within the if statement no matter what, i.e. it limits the list for both content types).

Comments

pbarnett’s picture

Haven't seen this old chestnut of a coding error for a while :-)

if ($variable = 'value')...
will assign 'value' to $variable, and return 'value' which for the string 'type1' (or any string that's not empty) is TRUE.
What you want is
if ($variable == $value)...
which tests if the two are of the same value, or
if ($variable === $value)...
which tests if the two are of the same value AND type.

Pete.

Chris Einkauf’s picture

Thanks for pointing that out to me. I completely forgot about the whole =/==/=== thing.

Unfortunately, even with the modified ==, I still can't get it to filter correctly. Perhaps the content taxonomy function that accepts PHP code doesn't take if statements?

Has anyone successfully filtered a content taxonomy field using the PHP code box?

Izz ad-Din’s picture

didnt seem to work after all :(

Izz ad-Din’s picture

I used the following code:

if ($form['type']['#value']='type') {
return '8'; }
else { return '12'; }

which seems to work.

Izz ad-Din’s picture

didnt seem to work after all :(

Izz ad-Din’s picture

Put this in your template.php

function getNodeformType() {
	$path = drupal_get_path_alias($_GET['q']);
	$path = explode('/', $path);

	if ($path[2] == 'Type1') {
		return 'TermA';
	} elseif ($path[2] == 'Type2') {
		return 'TermB';
	} elseif ($path[2] == 'Type3') {
		return 'TermC';
	} elseif ($path[2] == 'Type4') {
		return 'TermD';  //etc.
	}
}

And then, in the CCK PHP field for parent term, put the following code:

$getType = getNodeformType();
return $getType;

Kind regards,

Izz ad-DIn

pvanerk’s picture

Hi,

You also want to select the right parent node when editing a node. The problem is the $node object is not available to determine $node->type. But you can use other variables.

I use the following code to select the parent node. It works for node creation and editing an existing node.

Thanks to ablommers.

// Get available variables and load into object
$vars = get_defined_vars();
//$vars is now an array object; get contenttype
$type = $vars['field']['type_name'];
//derive termid for termname = contenttype
$tids = taxonomy_get_term_by_name($type);
// returnvalue is an array containing all matching terms, that needs to be filtered on vocabulary
foreach($tids as $tid) {
   if ($tid->vid == $vars['field']['vid'])
     return ($tid->tid);
}
// if no matching taxonomy term is found return null
return null;