3

I have a taxonomy vocabulary with the below heirarchy:

*Property Type

 -Commercial
    -Office
    -Building
    -Warehouse
 -Residential
    -Villa
    -Apartment
    -Penthouse

I have two content types for the property information that has a common field called: 'Property Type' relating to that vocab above, again, for the two content types: "Commercial Property" & "Residential Property" they are using the same field declaration that relates to the same vocab group.

I want to use the php advance code to filter 'commercial' and 'residential' whichever content type is concerned. E.g. Villa, Apartment and Penthouse should only show under the "Residential Property" content type.

My Current code is this but it does not work:

if (arg(0) == 'node' && is_numeric(arg(1))) $nodeid = arg(1);
if($nodeid) $type = noad_load($nodeid);
if($type->type=='commercial_for_rent' || $type->type=='commercial_for_sale') return 798;
else if($type->type=='residential_for_rent' || $type->type=='residential_for_sale') return 799;

In other words: Show only the sub category items of 'Property Type' vocab (e.g. "Residential terms") if content type being edited is this (e.g. "Residential Property" content type)...

Can someone help me point to the right direction please.

Thank you!

  • Where are you using this code? Why are you returning term ids? It seems that you are dealing with views arguments. Are you? Are you calling a proper function 'node_load()' instead 'noad_load()'? – Shoaib Nawaz Jul 14 '11 at 14:10
  • when editing my content type, under manage fields, edit the property type field (select list, which is a content taxonomy field) you get to select vocabs and under 'Advanced settings for hierarchical vocabularies' you can edit 'Advanced PHP Code'.. – jan Jul 14 '11 at 14:16
2

Apparently, I am answering my own question. This may not be the best solution (the drupal way) but it works and at least for the meantime I can continue the project. Here is how I solved it:

arg(0), arg(1).. will always refer to the current url arguments. so arg(0) will be the first arg after the siteroot, arg(1) is next.. etc..

With my problem, when you are editing a content type here is my current url path for example: http://localhost/mysite/node/add/commercial-for-rent, where arg(0) = node, arg(1)=add etc..

simply posting this to php snippet worked:

if(arg(2)=='commercial-for-rent' || arg(2)=='commercial-for-sale') return 798; else return 799;

That's at least as far as the url pattern goes. ../node/98/edit is when editing the content which is again this solution will no longer work...

As I said this is not the best solution if there is anyone out there who has better ideas please do tell me so I can edit the snippet and solve this the drupal way.

1

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.

<?php
// 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;
?>

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.