Hi

I was wondering if it is possible to load the parent nodes of a child node. Basically, the reverse of the nodefamily_relation_load() function. I'm trying to create tabbed navigation of different cck content types. I have usernode->profile_a and profile_a->profile_b relations set up with node family. If I'm displaying the page for profile_a, I can load the nodes of the children of profile_a and provide links to those children. However, if I'm looking at the node profile_b, it's not obvious to me how I can determine which nodes are parents of profile_b so that I can also provide links to them.

Thanks for any help you can provide
AC

Comments

fago’s picture

I've added the following function (to 4.7 and HEAD) for this purpose:

<code>
/*
 * Loads all parents for a node
 * The parent nodes will be loaded into the array $node->parents
 * 
 * @param $node The node object or the node's id
 */
function nodefamily_relation_load_parent(&$node) {

  if (!is_object($node)) {
    //$node is the nid
    $node2 = node_load($node);    
    return nodefamily_relation_load_parent($node2);
  }

  $result = db_query("SELECT parent_nid FROM {nodefamily} WHERE child_nid = %d", $node->nid);
  
  $node->parents = array();
  while ($row = db_fetch_object($result)) {
    $node->parents[] = $row->parent_nid;
  }
  return $node->parents;
}

Would be cool if you could publish your node-loading solution for the tabs, because this is a often requested feature.

aclight’s picture

Thanks for adding this function. I will test it soon to see if this gives me what I need (I think it will). Once I get the tab stuff working, I'll post the solution so others can see. There are a few other difficulties I'm having getting tabs to work like I want--maybe you will have suggestions.

  1. I want a generic but pretty form of the content type to be the text in the tag. So, for example, if I have a node with a content type of content_xyz_profile, I would like the tab's title to be XYZ, but maybe I also have a node type content_user_profile, and I want that title to be User. I could parse out the 'content_' and '_profile' part of the node type and display the rest, but it wouldn't be capitalized like I want it to be. I want to avoid putting specific case statements in my code so that it will be as flexible as possible. If there was some way of adding a field to the node that was called formatted_title or something like that, then maybe I could store the title that I want displayed in that field. I'm currently using the auto_nodetitle module to name the node profiles, and so they all have titles that are something like 'content_xyz_profile 457' which isn't so pretty.
  2. Hmmm....I'm forgetting my second question. I'll post a follow-up if I think of it.

Once again, thanks for adding that function. I'll let you know how it works.
AC

fago’s picture

what about using the readable name of the content-type?

you can get it by using a code similar to:

<code>
$types  = node_get_types();
$readable_type = $types['your_content_type'];

aclight’s picture

Thanks for the idea of using node_get_types(). That works and I hadn't thought of trying that.

Regarding the new function you added, I changed it a little to better match the output of nodefamily_relation_load(). The function now returns the actual loaded nodes, and not just the nids. Here's the changed function:

/*
 * Loads all parents for a node
 * The parent nodes will be loaded into the array $node->parents
 * 
 * @param $node The node object or the node's id
 */
function nodefamily_relation_load_parent(&$node) {

  if (!is_object($node)) {
    //$node is the nid
    $node2 = node_load($node);    
    return nodefamily_relation_load_parent($node2);
  }

  $result = db_query("SELECT parent_nid FROM {nodefamily} WHERE child_nid = %d", $node->nid);
  
  $node->parents = array();
  while ($row = db_fetch_object($result)) {
    //$node->parents[] = $row->parent_nid;
    if ($parent = node_load($row->parent_nid)) {
    	$node->parents[] = $parent;
    }
  }
  return $node->parents;
}

I also created a new function that loads the sibling nodes of a given node. I needed this for the tab theming to work. There are still other circumstances where the tabbed theming won't work as I'm doing it (eg. if there are grandchildren/grandparent or cousin or children of a sibling node relations in nodefamily). I think it would be best if the relationship discovery was done in the nodefamily module instead of the theme, since that is more flexible. Maybe you would want to create a function that returns an array containing the entire relationship of a family of nodes, including the current node.

Anyway, here is the new function I wrote (based on the parent and children functions).

/*
 * Loads all siblings for a node
 * The sibling nodes will be loaded into the array $node->siblings
 * In other words, all nodes that have the same parent as this
 * node will be loaded.  Note, however, that the children
 * of any sibling nodes will NOT be loaded.
 *
 * @param $node The node object or the node's id
 */
function nodefamily_relation_load_sibling(&$node) {

  if (!is_object($node)) {
    //$node is the nid
    $node2 = node_load($node);    
    return nodefamily_relation_load_sibling($node2);
  }


	// first, determine the parents of this node to see if there could be any siblings
  $result = db_query("SELECT parent_nid FROM {nodefamily} WHERE child_nid = %d", $node->nid);
  
  $node->parents = array();
  while ($row = db_fetch_object($result)) {
    if ($parent = node_load($row->parent_nid)) {
    	$node->parents[] = $parent;
    }
  }
  
  if (count($node->parents) > 0) {
  	$node->siblings = array();
  	// find and load the children of the parents
  	foreach ($node->parents as $parentnode) {
  		$children = nodefamily_relation_load($parentnode->nid);
  		foreach ($children as $child) {
  			if ($child->nid != $node->nid) {	// don't include current node in list of siblings
  			  $node->siblings[] = $child;
  		  }
  		}
  	}
  }
 return $node->siblings;
}

The other issue with regards to creating tabs that I'm running into is that the order of the children/parents returned isn't the same every time for every node. It would be helpful if there was some way to set the weight of nodes at the same level of a family. That way, the output of the relationship discovery functions could be sorted by the weights. Right now I'm just doing a
if ($children) sort($children);
operation on the output of the relationship functions, but that doesn't always give me the order I want them to be in.

Last issue with regards to tabbing--is it possible to change the display title of 'Usernode' to something else?

Thanks for all of your help on this issue. I've almost got this working like I had hoped it would.

AC

fago’s picture

Status: Active » Fixed

thanks for your code.

I've changed the parents loading function accordingly and added a function for loading siblings, that works similar as yours, but reuses the other loading functions and groups the output by parent nodeid, because there might be multiple parents.

http://cvs.drupal.org/viewcvs/drupal/contributions/modules/nodefamily/no...

Regarding the ordering: You can use the load_by_type function to get an array grouped by content types, which should solve all ordering troubles as long as you have only one node per content type.

mtk’s picture

I was looking for such a solution all over this forum...

I've downloaded latest release, but I get an empty array running both:

$nid = $node->nid;
$parents = nodefamily_relation_load_parents($nid);
print_r ($parents);

&

$parents = nodefamily_relation_load_parents($node);
print_r ($parents);

from a node-content_mycontent.tpl.php...

what did I do wring?

fago’s picture

I suppose your node has no nodefamily relation? -> goto admin settings nodefamily and verify your settings

aclight’s picture

Category: support » bug
Status: Fixed » Active

I tried using the most recent version of nodefamily.module (17Nov06) and I'm getting a recursion warning from PHP when I do

$parents = nodefamily_relation_load_parents($nid);
print_r($parents);

I won't reproduce the entire printout here, but my nodefamily structure is:
usernode->node_a
usernode->node_b
When getting the parents of node_a, the output includes the children of the parent usernode, which includes node_a, and thus triggers the recursion.

I'm assuming this is something that should be fixed, though it doesn't appear to cause any actual problems since it's caught by PHP.

Thanks
AC

fago’s picture

strange, for me this functions are working fine! (php5.2)

What php version are you using? Can you post the error?
I don't see where recursion should occur here, the function just loads the parents and returns them. Presumable there must be a problem with the node-id detection code.

Do you have troubles with all loading functions?

aclight’s picture

I'm using PHP 5.0.4.

I'll paste some information below. Let me explain it first. I'm viewing content type content_user_profile (node 484). In my template file for that type of content, I have the lines:

  $parents = nodefamily_relation_load_parents($nid);
  $children = nodefamily_relation_load($nid);
  $siblings = nodefamily_relation_load_siblings($nid);
  msg_r("parents");
  msg_r($parents);
  msg_r("siblings");
  msg_r($siblings);
  msg_r("children");
  msg_r($children);

When I view that page, I get the output below. Note that I have removed some information that is not relevant for security/privacy purposes. You can see the recursion warning towards the middle of the output. I will try some of the other functions and get back with you as to whether all functions cause this. Also, just so it's clear, my node family relationships are:

usernode -> content_user_profile
usernode -> content_abcd_user_profile


*     parents

*     Array
      (
          [470] => stdClass Object
              (
                  [nid] => 470
                  [vid] => 470
                  [type] => usernode
                  [status] => 1
                  [created] => 1162615188
                  [changed] => 1163225134
                  [comment] => 2
                  [promote] => 0
                  [moderate] => 0
                  [sticky] => 0
                  [revision_timestamp] => 1163225134
                  [title] => xxxx
                  [body] => 
                  [teaser] => 
                  [log] => 
                  [format] => 0
                  [uid] => 4
                  [name] => xxxx
                  [picture] => XXXXXX
                  [data] => XXXXXXX
                  [last_comment_timestamp] => 1162615188
                  [last_comment_name] => 
                  [comment_count] => 0
                  [taxonomy] => Array
                      (
                          [47] => stdClass Object
                              (
                                  [tid] => 47
                                  [vid] => 43
                                  [name] => 2010
                                  [description] => 
                                  [weight] => 0
                              )

                          [122] => stdClass Object
                              (
                                  [tid] => 122
                                  [vid] => 51
                                  [name] => XXXXX
                                  [description] => XXXXXX
                                  [weight] => 0
                              )

                      )

                  [files] => Array
                      (
                      )

                  [children] => Array
                      (
                          [484] => stdClass Object
                              (
                                  [nid] => 484
                                  [vid] => 484
                                  [type] => content_user_profile
                                  [status] => 1
                                  [created] => 1162618313
                                  [changed] => 1163806777
                                  [comment] => 2
                                  [promote] => 0
                                  [moderate] => 0
                                  [sticky] => 0
                                  [revision_timestamp] => 1163806777
                                  [title] => Personal Information 484
                                  [body] => 
*** This is the formatted body of the node ****

                                  [log] => 
                                  [format] => 0
                                  [uid] => 4
                                  [name] => XXXXX
                                  [picture] => XXXXX
                                  [data] => XXXXX
                                  [field_profile_image_id] => Array
                                      (
                                          [0] => Array
                                              (
                                                  [value] => XXXXX
                                                  [view] => XXXXX
                                              )

                                      )

                                  [field_nickname] => Array
                                      (
                                          [0] => Array
                                              (
                                                  [value] => 
                                                  [view] => 
                                              )

                                      )

                                  [field_birthday] => Array
                                      (
                                          [0] => Array
                                              (
                                                  [value] => 
                                                  [timezone] => 
                                                  [view] => 
                                              )

                                      )

                                  [field_home] => Array
                                      (
                                          [0] => Array
                                              (
                                                  [value] => 
                                                  [view] => 
                                              )

                                      )

                                  [field_other_information] => Array
                                      (
                                          [0] => Array
                                              (
                                                  [value] => 
                                                  [format] => 1
                                                  [view] => 




                                              )

                                      )

                                  [field_web_paging] => Array
                                      (
                                          [0] => Array
                                              (
                                                  [value] => 
                                                  [view] => 
                                              )

                                      )

                                  [field_cellular] => Array
                                      (
                                          [0] => Array
                                              (
                                                  [value] => 
                                                  [view] => 
                                              )

                                      )

                                  [field_pager] => Array
                                      (
                                          [0] => Array
                                              (
                                                  [value] => 
                                                  [view] => 
                                              )

                                      )

                                  [field_work] => Array
                                      (
                                          [0] => Array
                                              (
                                                  [value] => 
                                                  [view] => 
                                              )

                                      )

                                  [field_imageid_update] => Array
                                      (
                                          [0] => Array
                                              (
                                                  [view] => 
                                              )

                                      )

                                  [field_nickname_update] => Array
                                      (
                                          [0] => Array
                                              (
                                                  [view] => 
                                              )

                                      )

                                  [last_comment_timestamp] => 1162618313
                                  [last_comment_name] => 
                                  [comment_count] => 0
                                  [taxonomy] => Array
                                      (
                                          [47] => stdClass Object
                                              (
                                                  [tid] => 47
                                                  [vid] => 43
                                                  [name] => 2010
                                                  [description] => 
                                                  [weight] => 0
                                              )

                                          [122] => stdClass Object
                                              (
                                                  [tid] => 122
                                                  [vid] => 51
                                                  [name] => XXXXX
                                                  [description] => XXXXX
                                                  [weight] => 0
                                              )

                                      )

                                  [files] => Array
                                      (
                                      )

                                  [iid] => 
                                  [readmore] => 
                                  [links] => Array
                                      (
                                          [0] => add new comment
                                          [1] => 208 reads
                                      )

                                  [node_id] => 470
                                  [parents] => Array
                                      (
                                          [470] => stdClass Object
       *RECURSION*
                                      )

                                  [children] => Array
                                      (
                                      )

                                  [siblings] => Array
                                      (
                                          [470] => Array
                                              (
                                                  [543] => stdClass Object
                                                      (
                                                          [nid] => 543
                                                          [vid] => 543
                                                          [type] => content_abcd_user_profile
                                                          [status] => 1
                                                          [created] => 1162862734
                                                          [changed] => 1163268326
                                                          [comment] => 2
                                                          [promote] => 0
                                                          [moderate] => 0
                                                          [sticky] => 0
                                                          [revision_timestamp] => 1163268326
                                                          [title] => abcd_user_profile 543
                                                          [body] => 
                                                          [teaser] => 
                                                          [log] => 
                                                          [format] => 0
                                                          [uid] => 4
                                                          [name] => xxxx
                                                          [picture] => XXXXXX
                                                          [data] => XXXXXX
                                                          [field_field1] => Array
                                                              (
                                                                  [0] => Array
                                                                      (
                                                                          [value] => 
                                                                          [format] => 1
                                                                      )

                                                              )
                                                     
***** other fields for the content type are here but have been deleted to make this shorter *****

                                                          [last_comment_timestamp] => 1162862734
                                                          [last_comment_name] => 
                                                          [comment_count] => 0
                                                          [taxonomy] => Array
                                                              (
                                                              )

                                                          [files] => Array
                                                              (
                                                              )

                                                      )

                                              )

                                      )

                              )

                          [543] => stdClass Object
                              (
                                  [nid] => 543
                                  [vid] => 543
                                  [type] => content_abcd_user_profile
                                  [status] => 1
                                  [created] => 1162862734
                                  [changed] => 1163268326
                                  [comment] => 2
                                  [promote] => 0
                                  [moderate] => 0
                                  [sticky] => 0
                                  [revision_timestamp] => 1163268326
                                  [title] => abcd_user_profile 543
                                  [body] => 
                                  [teaser] => 
                                  [log] => 
                                  [format] => 0
                                  [uid] => 4
                                  [name] => xxxx
                                  [picture] => [G2:13276]
                                  [data] => XXXXX
                                  [field_field1] => Array
                                  (
                                      [0] => Array
                                          (
                                              [value] => 
                                              [format] => 1
                                          )

                                  )
                                                     
***** other fields for the content type are here but have been deleted to make this shorter *****

                                  
                                  [last_comment_timestamp] => 1162862734
                                  [last_comment_name] => 
                                  [comment_count] => 0
                                  [taxonomy] => Array
                                      (
                                      )

                                  [files] => Array
                                      (
                                      )

                              )

                      )

              )

      )



*     siblings

*     Array
      (
          [470] => Array
              (
                  [543] => stdClass Object
                      (
                          [nid] => 543
                          [vid] => 543
                          [type] => content_abcd_user_profile
                          [status] => 1
                          [created] => 1162862734
                          [changed] => 1163268326
                          [comment] => 2
                          [promote] => 0
                          [moderate] => 0
                          [sticky] => 0
                          [revision_timestamp] => 1163268326
                          [title] => abcd_user_profile 543
                          [body] => 
                          [teaser] => 
                          [log] => 
                          [format] => 0
                          [uid] => 4
                          [name] => xxxx
                          [picture] => XXXX
                          [data] => XXXX
                          [field_field1] => Array
                          (
                              [0] => Array
                                  (
                                      [value] => 
                                      [format] => 1
                                  )

                          )
                                                     
***** other fields for the content type are here but have been deleted to make this shorter *****


                          [last_comment_timestamp] => 1162862734
                          [last_comment_name] => 
                          [comment_count] => 0
                          [taxonomy] => Array
                              (
                              )

                          [files] => Array
                              (
                              )

                      )

              )

      )
*     children

*     Array
      (
      )



fago’s picture

yeah, I think I know whats happening.

could you try the latest development snapshot of drupal 4.7.x?
http://drupal.org/node/3060/release

I think this problem should be fixed there. (node_load() & php5 object references..)

aclight’s picture

yeah, it's definitely related to passing nodes by reference. It may be a bit before I can try the development version to see if that fixes the problem, but I'm assuming you are referring to http://drupal.org/node/86842 as the problem, and I read that thread and it looks like that would fix the problem. I'll let you know when I get a chance to test it.

Thanks
AC

fago’s picture

yep, that's what I meant.

fago’s picture

Status: Active » Fixed

please open another issue for this, if it isn't fixed with that.

mtk’s picture

I suppose your node has no nodefamily relation? -> goto admin settings nodefamily and verify your settings
actually I didn't have a parent...
I was able to create a child without its parent.

is there a way to block this?

Anonymous’s picture

Status: Fixed » Closed (fixed)