2

I'm looking for a way to apply a grayscale image style to a content type image field when certain conditions are met. A date is reached (date field) for example

Can the rules modules do this ?

Thanks for the suggestions & ideas

3 Answers 3

1

Yes you can do it with rules module with the help of Field formatter conditions module

Adds conditions to field formatters. Configuration happens on the "Manage Display" screens of every entity type on a per field basis when clicking on the cogwheel on the right.

enter image description here

Supports

  • Rules
  • Field API fields
  • Display Suite fields (only branch 2)

Simple conditions and actions

  • Hide source field when target field is empty.
  • Hide source field when target field is not empty.
  • Hide source field when current user has specific roles.
  • Hide source field on specific pages.
  • Hide source field when target field contains a specific string.
  • Hide source field when target field doesn't contain a specific string.
  • Hide link field if title is empty.
  • Hide date field if from/to has expired.

Rules actions

  • Hide a field.
  • Change image style.

Rules should always be a 'A field is rendered' event before they can be selected in the conditions dropdown on the "Manage Display" screens per field.

0

If a date is reached, then you want to change the image in to graycale, there is no need to use rules. Create two image presets using ImageCache Actions. Then print your image field in your node tpl according to your conditions:

<?php
  print theme('image_style', array('path' => $node->field_image['und'][0]['uri'], 'style_name' => [STYLE NAME]));
?>
0

Hope this will help you,

In Drupal 7 you can achieve it by creating a template file. Based on the your certain condition you can change the image style with Desaturate Effect.

Steps

  • If you don't have the template file for a particular content type then, just create new template file inside your active theme sites/all/themes/ACTIVE_THEME_NAME/templates/. Just copy and paste node.tpl.php and rename it to node--your_CONTENT_TYPE_MACHINE_NAME.tpl.php.

    • For example to create an template file for an ARTICLE CONTENT TYPE rename to node--article.tpl.php.
  • Now get the image field by printing the $node object. User drupal_set_message() to print the object with in PHP tags. You can print it after the variables listing

    <?php
     drupal_set_message("<pre>" . print_r($node, 1) . "</pre>");
    ?>
    
  • Add the grayscale image style using admin/config/media/image-styles/add with Desaturate Effect.

    enter image description here

  • After printing get the image field name

    • For this example Im using the field field_image.
  • Based on your certain conditions change the image style and render the image using theme()

    enter image description here

  • Now its time to render the image.

    enter image description here

Note

  • In the custom template file you can go through the available variables at the top of the file. And you can use the variable as per your requirement.
  • Variable $content will have an array of node items.
  • print render($content); will print all the node items. You can use hide($content['field_name']) to hide the specific field.
  • You can also use $node object to add fields as per your requirement.
2
  • well, you explained about the implementation method, but not about the OP's question- "way to apply a grayscale image style to a content type image field when certain conditions are met"
    – Bala
    Commented Dec 9, 2013 at 8:39
  • Need to use Desaturate effect in the image style
    – Inizio
    Commented Dec 9, 2013 at 10:07

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.