0

Is there any elegant way to classify content in view, so that content with same value of some field gets the same value from some predefined list?

For example: I have nodes with entity reference field and would like to get different colors (from pre-defined palette) for different entities referenced through this field.

I know, that this might be solved by using Views PHP field, but this seems to be quite "hacky" to me. Also, classification lists can't be defined in user-friendly way.

Also, it's possible to add another field to referenced entity, which allows us to use this value for classification through views relationship. But this seems to overkill - and even then I would need to use something like Computed field to get randomly generated colors, which are compatible with each other (or use computed field in combination with list field - but that seems to be completely insane).

Do you know about any better solution?

1 Answer 1

1

I haven't done this myself, but off the top of my head...

On the node type, I would add a CCK Colour Field to allow the person adding the node to determine a colour for that node. (Then in the content type setting, manage display and hide the colour field).

Then in views, you can add the colour field, and exclude from display.

Then in the same view, add the node reference field you wish to change the colour for (for example node title colour or background colour). (Make sure the colour field is first in the list of view fields)

Then in the view admin, click on advanced, and click "Theme Information". Find the field you wish to override (something like views-view-field--title.tpl.php).

Then copy the field.tpl.php file from the theme folder in the views module folder, and place it in your themes template folder, renaming the file views-view-field--title.tpl.php

You can then place your own php in that template file, around the default $output variable, something like...

$color = $row->{$field->color_field};

<div style="color:<?php print $color ?>;">
<?php print $output ?>
</div>

I think that's what you mean? Please let me know if I have misread the question.

1
  • 1
    Thanks, that's roughly the same as the second solution I mentioned. CCK colour field module seems to be interesting, I will probably try it soon. Commented Oct 30, 2014 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.