I'm trying to set up a multi-level taxonomy. Select major tag, enter minor tag (free tagging). So I have a major vocab and all that works ok. For the minor free tags I tried the php code with this:

  return $node->field_major[0]['value'];

without the of course. That didn't work. Why? I tried a plain return 331; where 331 is real vocab id and that seemed to work right. I even tried a global $node before the return but no joy with that either. Devel says that is the right field name so how come it doesn't work?

Comments

malc_b’s picture

My solution, FYI, is to add to the parent php box this:

if ($_POST['field_major']['value']) {
  return $_POST['field_major']['value'];
}else{
  return $field['parent'];
}

That gets the value of the major tag from the $_POST variable at submit time, which when the autotag runs. The other time it is used is for the autocomplete string. At that time it uses the default value as the $_POST doesn't have the major field. At least this way it doesn't come with suggestions from the major vocab. Ideally it should suggest from the child terms but that would mean knowing field_major value and I could see a way to do that.

malc_b’s picture

Category: support » bug

I think this should be re-labelled as bug since even though the widget allows php to select the parent there is next to nothing the php can do. If however the form_state had been passed to the check parent routine then the php could access any of the other values. As it stands the php has no variables to work with.

mh86’s picture

Title: Auto complete ignores parent php » Pass variables to 'Advanced PHP Code'
Category: bug » feature

hi!
it really depends on what you want to do. I know it's not that powerful, if you don't have any context information available, but initially it wasn't designed that way. You can compare it to the allowed values php code of text fields, same behaviour as here.
I took a look at the implementation and as far as I can see, at the point the php code gets evaluated no other useful variables are available. Take a look at the available global variables, I think there is nothing else I can offer.
I hope it's ok for you, if I change the status to feature request. For me it's not a bug report, because I never planed to support additional variables.

malc_b’s picture

Hi,

Well, it would be nice to more powerful. Input validation is one thing but this the parent so if you can't compute it then it may as well just be a fixed value. OK, you can compute it a bit. I did look at the globals and the comment above is a sort of fix. I got lost trying to understand it. I think I need to know more about FAPI. What is needed is the $form_state to be passed down to content_taxonomy_field_get_parent as well as the field. That way it could use the previous form values.

And/or it might be better to have auto complete as optional. At least that way the user is not confused by autocomplete suggestions from the wrong parent. I can sort of do that with the fix above if default points to an empty parent (perhaps an invalid might also work and not throw an error, I haven't tried that, 0 gives the root though, any the rotator would still go round).

BTW another suggestion, would be to have a defined list or defined not list for free tagging. And php. So much like plain text field. That way you define a list of allowed possible values that would be put into a vocab but not have them in the vocab until they were used. This would keep vocabs smaller and not full of empty entries. Making that the inverse would give a not list which would stop unsuitable free tags (e.g. impolite words).

I did consider the route of plain text fields, one as a defined list for major vocab and one as free for free tagging and then using computed field as in this http://drupal.org/node/297030 . But it seems a shame to go to that much effort for less return when this module is almost ideal.

that0n3guy’s picture

#1 doesnt work for me at all using 6.x-dev ....

My major field is an autocomplete content taxonomy field, my child field is the same.

Is there some kind of documentation on how to use the 'advanced php' section.

Izz ad-Din’s picture

Status: Active » Needs review
Izz ad-Din’s picture

Status: Needs review » Active

my bad

jibort’s picture

This is a heavy and dark work-arround that I have done.

The problem is that we don't have any context information about the form or the node inside the "advanced php" section. Is not that?

If you are editing a node, maybe a bit of context is in the URL.

For example, if you are editing node 1843, maybe your URL is /node/1843/edit

You could get this URL with:

$path = drupal_get_path_alias($_GET['q']);

from that URL you could get the $nid (my function for extracting the nid is horrible too, but works, :-) ).

When you have the $nid, you could load the $node

$node = node_load($nid);

You have a lot of information in that $node (last saved node).

Yes, I know that all of this is horrible, unsafe, dark,..... but are there better solutions?.