1

Remember the create password page of Drupal! while you are entering characters it tells you how string is your password!

now I want to have something like this in a list box but a bit different. I want to show an image and a text depend on the item which is selected in the list box! How can I do it?

My first try was using conditional field but I have to add 120 image and text field to my content type and but lots of conditions to handle it. It doesn't make sense!

My second try was using markup module but I didn't know how to fetch the value of list box with ajax!

My third try was using entity referenced auto fill but when I select an item from the list box for the first time it fills the other fields correctly. but the next times it does not change any thing.

in addition, I need the image and text field to appear read only (non editable)

Do you know any other way to do that?

2
  • Are you looking for a module to handle this? Or is a programmatic solution ok too? Commented May 24, 2015 at 7:17
  • I need any possible way!
    – shekoufeh
    Commented May 25, 2015 at 7:44

1 Answer 1

0

Below is a sample of a rule (in Rules export format) that you can use as a prototype to answer your question by using the Rules module:

{ "rules_set_field_value_based_on_value_of_selection_list_field" : {
    "LABEL" : "Set field value based on value of selection list field",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_conditional" ],
    "ON" : { "node_presave" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_optionlist" } },
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_free_format_text" } }
    ],
    "DO" : [
      { "SWITCH" : {
          "USING" : { "data" : [ "node:field-optionlist" ] },
          "DO" : [
            { "CASE" : {
                "USING" : { "value" : "B" },
                "DO" : [
                  { "data_set" : { "data" : [ "node:field-free-format-text" ], "value" : "B.A.S.I.C." } }
                ]
              }
            },
            { "CASE" : {
                "USING" : { "value" : "A" },
                "DO" : [
                  { "data_set" : { "data" : [ "node:field-free-format-text" ], "value" : "A.D.V.A.N.C.E.D." } }
                ]
              }
            }
          ]
        }
      }
    ]
  }
}

Some more details to further explain the above above sample:

  • The Rules event I'm using is "before saving content".
  • In my case I have these fields:

    • a selection list field with machine name field_optionlist.
    • a text field with machine name field_free_format_text.
  • The selection list field actually has 3 possible values, like so:

    • A (=Advanced).
    • B (=Basic).
    • C (=Custom).
  • As a sample, I decided to set the values of the text field like so:

    • B.A.S.I.C. if "The selection list field's value is Basic".
    • A.D.V.A.N.C.E.D. if "The selection list field's value is Advanced".
    • Leave it unchanged if "The selection list field's value is Custom"

Part of the "clue" to make this rule work is the condition Entity has field. That's also what is mentioned in the Rules UI when you're adding a "data comparison" condition: somewhere it says "The data selector helps you drill down into the data available to Rules. To make entity fields appear in the data selector, you may have to use the condition 'entity has field' (or 'content is of type')". I wouldn't be surprised if that is what you are missing in the rules conditions you tried so far ...

For a video tutorial that illustrates the importance of this Entity has field condition, refer to Data types and data selection, especially what is shown between about 13:30 and 17:30 in that video.

However, to make the above work, and to be able to get this to work with only 1 rule (instead of writing 2 rules, ie one for "Basic", and one for "Advanced"), I also used the "Conditional Rules module. That way you could expand the above prototype to say dozens of different possible values. For a sample using the Rules Conditional module (for which there is not a lot of docu available), refer to my answer to the question "How to prevent a node being saved when using the Rules module?".

Note: the Field Permissions module should help you to make such text field read only.

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.