简体   繁体   中英

Drupal Views - for user: id field, match content against an array

I have an array of user ids and I need to go through the a user: id field within a view, adding a bit of specific HTML for matching and non-matching results.

So I'm guessing something like this needs to go in views-view-field--uid.tpl.php:

<?php if (//Field content matches an array value): ?>
<span class="friend"><?php print $output; ?></span>
<?php endif; ?>

<?php if (//Field content doesn't match an array value): ?>
<span class="not-friend"><?php print $output; ?></span>
<?php endif; ?>

Can someone help me fill in the gaps, please? :)

Assuming $ouput is going to be only an integer representing the uid (and not HTML markup), you could do something like this:

<span class="<?php if(!in_array($output, $your_array)): ?>not-<?php endif; ?>friend">
    <?php print $output; ?>
</span>

see php in_array()

However, $output might be HTML. If that's the case you should use $row instead of $output. To see what $row contains I love doing this in template files:

<!-- <?php echo print_r($row,true); ?> -->

(then view source in browser)

Also, I'd recommend not doing this at all in your template file because it ties logic to the theme... Check out Views Customfield -- it will let you do PHP in a custom field... If you put it under the UID, and exclude UID from display, you can access the UID and execute the same code I've got above in the custom field, using the $data object instead of $row or $output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM