Question

My user accounts have an integer field Visits with the suffix visit|visits. It is very handy to let Drupal automatically decide which suffix is appropriate. I would now like to use this field in a status message with the simple Rules action Show a message on the site. Unfortunately, when I set the message to You currently have [site:current-user:field-visits]., the suffix is not included, only the integer appears.

Is there a way to solve this? I would like to prevent a cumbersome Rule with an extra check on the value of Visits and an according adaptation of the message.

Was it helpful?

Solution

AFAIK, field metadata isn't available in tokens. However, Rules to the rescue!

  1. Create a rule set component which takes the field value as an integer parameter and two additional string parameters and provides a string value in return.
  2. In your rule set, add a rule which checks to see if the value is one. If it is, set the provided string value with the integer value and the first string parameter.
  3. Then, add another rule which does the same thing if the value is > 1.
  4. Now you have a string which is pluralized!

Here is an export:

{ "rules_pluralize" : {
    "LABEL" : "Pluralize",
    "PLUGIN" : "rule set",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "integer" : { "label" : "Integer", "type" : "integer" },
      "string_one" : { "label" : "One", "type" : "text" },
      "string_many" : { "label" : "Many", "type" : "text" },
      "pluralized_version" : { "label" : "Pluralized version", "type" : "text", "parameter" : false }
    },
    "RULES" : [
      { "RULE" : {
          "IF" : [ { "data_is" : { "data" : [ "integer" ], "value" : "1" } } ],
          "DO" : [
            { "data_set" : {
                "data" : [ "pluralized-version" ],
                "value" : "[integer:value] [string-one:value]"
              }
            }
          ],
          "LABEL" : "Set string for one item."
        }
      },
      { "RULE" : {
          "IF" : [
            { "data_is" : { "data" : [ "integer" ], "op" : "\u003E", "value" : "1" } }
          ],
          "DO" : [
            { "data_set" : {
                "data" : [ "pluralized-version" ],
                "value" : "[integer:value] [string-many:value]"
              }
            }
          ],
          "LABEL" : "Set string for multiple items."
        }
      }
    ],
    "PROVIDES VARIABLES" : [ "pluralized_version" ]
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top