Community & Support

PHP help - simple if/else and relating node to CCK field.

I need some basic PHP help with Drupal please. This is something I've been avoiding far too long. I've searched here and on Google and at php.net, but I don't even really know what to search for, so it's kinda hard.

Basically I need to run an if/else on the Taxonomy ID of a CCK field. If the tid is "5" I need to output one thing, and if it's not, I need to output another. I know a little PHP and I know the construct is probably easy - I just have been unable to find it.

So there are two tables involved: the "node" table and the "field_data_field_author_type" table. Within the second table is a column named "field_author_type_tid."

What I want to do (in english), is ask this:

"For the node I'm on now, what is the tid of the author type?

So, here's my poor attempt at the PHP. Can someone please tell me how to do it right?

<?php
if ($node->field_data_field_author_type->field_author_type_tid[0]['value'] == '5') {
echo
"Guest Contributor";
} else {
echo
"Altarum Author";
}
?>

I know this is completely wrong, because "field_data_field_author_type" is not even in the node table. I think I need to relate them with the entity_id. I just don't know how to do it.

Any help would be greatly appreciated.
Tim

Comments

I believe you want

I believe you want $node->field_author_type_tid[0]['value'], though 'value' may be 'tid'.

You can add this to the end of node.tpl.php file to check the node structure

<pre>
<?php print print_r($node, TRUE); ?>
</pre>

or if you have devel installed there will be a tab for the node that shows the structure.

Thanks for helping! That code

Thanks for helping! That code gave me this error, with both 'value' and 'tid' :

    Notice: Undefined variable: node in eval() (line 2 of /home/admin/domains/hpfd7.altaruminstitute.net/public_html/modules/php/php.module(74) : eval()'d code).
    Notice: Trying to get property of non-object in eval() (line 2 of /home/admin/domains/hpfd7.altaruminstitute.net/public_html/modules/php/php.module(74) : eval()'d code).

Also, I added the code at the end of page.tpl.php (I'm using Zen) and here's the output of interest:

[field_author_type] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [tid] => 4
                        )
                )

        )

So, I should be able to build the PHP from that output?

Thanks again,
Tim

Yes, you want

Yes, you want $node->field_author_type['und'][0]['tid']

Sorry, but I'm still getting

Sorry, but I'm still getting the same error:

    Notice: Undefined variable: node in eval() (line 2 of /home/admin/domains/hpfd7.altaruminstitute.net/public_html/modules/php/php.module(74) : eval()'d code).
    Notice: Trying to get property of non-object in eval() (line 2 of /home/admin/domains/hpfd7.altaruminstitute.net/public_html/modules/php/php.module(74) : eval()'d code).

Here's the code I'm using in a D7, Display Suite PHP field. Just trying to get the tid so I can label the author at the top of the post...

<?php
if ($node->field_author_type['und'][0]['tid'] == 5) {
echo
"Guest Contributor";
} else {
echo
"Altarum Author";
}
?>

Any ideas?

Thanks,
Tim

What is the context of you

What is the context of you code, where are you placing it?

It's in a D7, Display Suite

It's in a D7, Display Suite code field, set to PHP, and placed in the header of a two column stacked layout, in the "Full Content" display for the content type "Author."

Make sense?

Do you think it's a Display Suite issue?

Thanks again for your help. I'll check back in the morning. Gotta get some sleep. Drupal's been beating me up all day.

Tim

I am guessing that is term

I am guessing that is term reference field, the field should be directly available in under Manage Display, If it's simply a question of showing the term without the link, change the format on the Manage Display page.

If its a question of formatting the term reference field you might check out Taxonomy Formatter and/or Custom Formatters

Otherwise you would use $entity instead of $node (its in the help text and if you have tokens enabled (may need the contributed token module also) you can use [field_author_type]

I tried $entity and get the

I tried $entity and get the same error, and I'm not sure how to use tokens with PHP. But really I shouldn't even need PHP...

You're right, it is a term reference, and what I want to do is show the name of the term. I tried it using the tokens with "Display Code" option, but I could never get it quite right. Here's why: I've only got three terms, in this configuration:

  • Altarum Authors
    • With Center
    • Without Center
  • Guest Contributors

The "Altarum Authors" is the parent of the "with center" and "without center," and Guest Contributors has no children. The with and without centers terms are for internal reference only. All I want to do is, if an author is either of the "Altarum Authors" types, I just want to show "Altarum Authors." If an author is "Guest Contributor" I just want to show "Guest Contributor."

Well, using every combination possible of term:name, term:name:parent, root:parent, etc., offered in Display Suite I can't get it to behave that way. I can either get the full term name (with and without center), which I never want, in which case the Guest Contributors works... or If I say "Parent" or "Root," I can get just "Altarum Author" for either of those types, but then the Guest Contributor doesn't work! I just get the actual code. It's very puzzling, and I tried every option that seemed relevant to me.

That's the only reason I'm even trying the PHP, cuz the "Display Suite Code" using Tokens just isn't working for me. Am I missing something? Does this make sense?

Should this whole thread be in the "Display Suite" Issue Queue?

I've spent at least six hours on something that is so simple, looked like it would take ten minutes. Ugh.

Thanks again for the help though!
Tim

Since the field is only for

Since the field is only for administrative purposes, I would have considered making it two vocabularies and only displaying the one. That said this should work

<?php
$tid
= 0;
// Make sure the elements we need actually exist
if ( is_array($entity->field_author_type) ) {
  if ( !empty(
$entity->field_author_type['und']) ) {
    if ( !empty(
$entity->field_author_type['und'][0]) ) {
      if ( !empty(
$entity->field_author_type['und'][0]['tid']) ) {
        
$tid = $entity->field_author_type['und'][0]['tid'];
      }
    }
  }
}
if (
$tid == 5) {
echo
"Guest Contributor";
} else {
echo
"Altarum Author";
}
?>

I probably didn't explain it

I probably didn't explain it right. The children of Altarum Authors is for Admin purposes only, but the parent needs to be displayed, when the post author is one of our employees - an "Altarum Author."

So, from my content manager's perspective, when she creates a new author, she will choose whether they are "with or without a center" (the admin part) if they are an Altarum Author, or a Guest Contributor (subject matter experts in healthcare who do not work for us). I didn't want to make it two vocabularies cuz that would make the input form more confusing for her, and keeping it simple for her is a top priority.

Tried your code above and got the same thing: "Notice: Undefined variable: entity in eval() (line 4 of /home/admin/domains/hpfd7.altaruminstitute.net/public_html/modules/php/php.module(74) : eval()'d code)."

For some reason it just doesn't like the variables.

Thanks so much for your help - I really appreciate the time you are taking to try to help me. I've been Drupaling for a little over a year and I feel like I should be able to figure this stuff out. I'm over my head a bit with this project but my employer is really cool about it cuz I've built about a dozen little intranet Drupal sites for specific teams that they love. This is basically my first public facing Drupal site. I started with Zen and a dream and it's going pretty well, but these little details will make ya crazy, ya know?

I'm really looking forward to when I know enough that I can help people too!

Tim

Some things to check What

Some things to check

What version of Display Suite are you using, I have 7.1.4

What for the code field, what do you have the text format set to? It should be "Display Suite Code".

OMG! I feel so stupid! I had

OMG! I feel so stupid! I had the text format set to "PHP." I didn't realize (or look closely enough) to notice that you can use PHP in the "Display Suite Code" section. Doh!

It works perfectly now. :-)

Thank you so much! Now I can move on to the next issue... trying to put a YouTube Player on my site that's easy for my content manager to control. I tried MediaFront, which is promising, but inexplicably, will not play YouTube videos on iOS. Wow.

So I'm gonna give jPlayer a try.

Seriously, thanks. My day just got a lot better!
Tim

nobody click here