0

I am using the voting api, fivestar and rules modules to try to fetch the vote a user gave to a node (article) and apply that same vote/value to a fivestar field of the author of that node. How can I accomplish this?

{ "rules_cast_a_vote_when_the_author_casts_a_vote_on_their_article" : {
"LABEL" : "Cast a vote when the author casts a vote on their article",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"TAGS" : [ "cast", "fivestar", "vote" ],
"REQUIRES" : [ "rules", "rules_conditional", "voting_rules" ],
"ON" : { "voting_rules_insert_node" : [] },
"IF" : [
  { "entity_is_of_bundle" : {
      "entity" : [ "node" ],
      "type" : "node",
      "bundle" : { "value" : { "article" : "article" } }
    }
  },
  { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_my_fivestar_rating" } },
  { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_the_editor" } }
],
"DO" : [
  { "entity_fetch" : {
      "USING" : { "type" : "user", "id" : [ "node:field-the-editor:uid" ] },
      "PROVIDE" : { "entity_fetched" : { "editor_uid_fetched" : "Fetched editor uid" } }
    }
  },
  { "CONDITIONAL" : [
      {
        "IF" : { "entity_has_field" : {
            "entity" : [ "editor-uid-fetched" ],
            "field" : "field_my_fivestar_rating"
          }
        },
        "DO" : [
          { "drupal_message" : { "message" : "When conditional is TRUE: [editor-uid-fetched:name]\u0027s ID has been fetched and is: [editor-uid-fetched:uid]" } },
          { "data_set" : {
              "data" : [ "editor-uid-fetched:field-my-fivestar-rating" ],
              "value" : [ "node:field-my-fivestar-rating" ]
            }
          }
        ]
      }
    ]
  },
  { "drupal_message" : { "message" : "vote:value [vote:value]\u003Cbr\u003E\r\nnode author fetched ID: [node-author-fetched:uid]\u003Cbr\u003E\r\nnode author fetched name: [node-author-fetched:name]\u003Cbr\u003E\r\neditor rating now: [editor-uid-fetched:field_cr_rating]\u003Cbr\u003E\r\neditor rating value now: [editor-uid-fetched:field_cr_rating:value]\u003Cbr\u003E\r\neditor rating now (hyphens): [editor-uid-fetched:field-cr-rating]\u003Cbr\u003E\r\neditor rating value now (hyphens): [editor-uid-fetched:field-cr-rating:value]\u003Cbr\u003E\r\n\r\n" } }
]

} } enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

  • According to your details, you are trying to add rating the user himself (author) while adding rating to the node, right? – usmanjutt84 Apr 16 at 11:54
  • Please see my comment of Apr 3. – BassPlaya Apr 17 at 13:36
0

I undesrtand that your question is about a 'code' solution, this one is not but it works, you'll need rules conditional module, and voting rules module , it adds an action: 'a node has been rated', then fecth node-author-by-id, make shure both have the same field (user-bundle and contentype), set user-author-rating-field-value with your node-rating-field-value, or import rule, please edit your field-rating machine name and dependencies as needed. i hope it helps.

    { "rules_set_node_five_star_rate_to_node_author" : {
    "LABEL" : "set node five-star rate to node author",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_conditional", "RATING_MODULE_MACHINE_NAME" ],
    "ON" : { "voting_rules_insert_node" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_YOUR_FIVE_STAR_FIELD" } }
    ],
    "DO" : [
      { "entity_fetch" : {
          "USING" : { "type" : "user", "id" : [ "node:author:uid" ] },
          "PROVIDE" : { "entity_fetched" : { "entity_fetched_author_by_id" : "author_by_id" } }
        }
      },
      { "CONDITIONAL" : [
          {
            "IF" : { "entity_has_field" : {
                "entity" : [ "entity-fetched-author-by-id" ],
                "field" : "field_YOUR_FIVE_STAR_FIELD"
              }
            },
            "DO" : [
              { "data_set" : {
                  "data" : [ "entity-fetched-author-by-id:field_YOUR_FIVE_STAR_FIELD" ],
                  "value" : [ "node:field_YOUR_FIVE_STAR_FIELD" ]
                }
              }
            ]
          }
        ]
      }
    ]
  }
}

enter image description here

  • Thank you for your efforts. However, I can't get this to work. I followed your instructions but no value is being set. Also, I'm guessing that if you choose to "set a data value" this might not be the same as casting a 'vote' with the value fetched onto the user's field. Yes, my fields on the node and the user are both the same fields. – BassPlaya Mar 31 at 6:24
  • please attach images or code from your configuration, to see if i can help you better. – pinueve Apr 1 at 21:13
  • something is very confusing, you say: 'and apply that same vote/value to a fivestar field of the author of that node.' Node author is admin, and node editor is 'uid:2', you are fetching 'editor user', not author user, please clarify. – pinueve Apr 2 at 16:08
  • i tryed several configurations in rules, fivestar field is made to receive votes, is not ment to be assigned a value from another field, even that is shown in rules ui, it has several propieties (number of votes, vote average, single vote vale, ...) it handles a % value, i manage to assigng a value of 80 in an integer user field, but not as a fivestar vote, 'heavy' coding is needed, grabing code from module coudl be a starting point. take a look hrere: drupal.stackexchange.com/questions/188208/… – pinueve Apr 2 at 19:58
  • I feel that it is not confusing. Like the title says: 1) fetch the vote casted 2) apply that vote. Whom it is applied to doesn't really matter. It could be the author, it could be the editor, it's the "applying the vote" or "casting the vote" on an entity programmatically that is what I'm looking for. Please let me know if it is still unclear. By saying "the same vote" I mean the value of that vote. – BassPlaya Apr 3 at 3:58

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.