3

Hiding a node field works great by default when the field is empty.

But I wonder how to hide a field when viewing a node if the value of a field is for example 3.

For now, I just need it for integer field.

Any suggestions?

  • You want to hide the field itself (not any other field) if it's value is for example 3, don't you? – herci Aug 13 '15 at 7:48
4

Views Conditional or Conditional Fields can help you reach your goal .

Views Conditional is a simple module that allows you to define conditionals (if xxx then yyy) with fields in views. Conditions include:

  • Equal To
  • Not Equal To
  • Greater Than
  • Less Than
  • Empty
  • Not Empty

Views conditional allows you to output text based on the result of the condition.

  • He needs to hide a field in node displaying page not when he edit it – M a m a D Aug 13 '15 at 7:56
  • In this situation Conditional Fields still works . – Alireza Tabatabaeian Aug 13 '15 at 8:31
  • I do not need it in views. Just simple node display. – Azz Kawa Aug 13 '15 at 8:40
  • Views Conditional is used for views but Conditional Fields can be used on nodes display ;) – Alireza Tabatabaeian Aug 13 '15 at 10:26
3

The Field formatter conditions module is a good starting point to answer your question. However, if the "Simple conditions and actions" are not sufficient, then you should consider using its integration with the Rules module.

Refer to the video about Field formatter conditions for a demo of what is pretty close to what you're looking for. Especially starting from around 4:50 in the video, you can see the Rules Action "Hide a field".

So your question is reduced to something like "How should my the event and optional conditions for the Rules module look like (to apply the "Hide a field" action)?". The answer to that question is something like so:

  • Rules Event: A field is rendered.
  • Rules Condition: Data comparison (to check if the value of your field is 3).
  • Rules Action: Hide a field.

Note: The article about Introducing Field formatter conditions for Drupal provides some more background reading about this video also.

1

I think you go for 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.

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.
  • This module is great, but does not work in my case. As you can read from your post, it only reacts based on "target" field. What i need is : label = cat, value = 3. If the value is 3, hide the field. – Azz Kawa Aug 13 '15 at 8:42
1

How did you implement your node display(view) page?

if you are using default themes like Bartik and the field name is field_body you can use kpr() function of devel module in hook_preprocess_node function to get the field then use unset() function to remove it from displaying. you also need to install entity module. As an example the following function

function bartik_preprocess_node(&$variables) {
    if($variables['node']->type=="memory"){
        kpr($variables);
        $wrapper = entity_metadata_wrapper("node",node_load($variables['node']->nid));
        $value = $wrapper->field_body->value();
        if($value == 3)
            unset($variables['content']['field_body']);
    }
}

This is the kpr result, I removed the field_body

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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