2

UPDATED 8/29/2016

Thank you so much for everyone that responded and special thanks to Pierre.Vriens for the detailed explanation. I am sharing my answer in case anyone finds this useful.

After much research I found that the summary field was not available so I created my own summary (which is not nearly as good as the built in summary from Durpal) but it fits my own needs. It will break in the middle of a word but that is ok for me and also it breaks on 250 characters no matter what but that is ok in my case as well.

In my case I used the following for the facebook-description field (with php wrappers, or course).

print substr(strip_tags($node->body['und'][0]['value']), 0, 250) . " . . ." thanks again to everyone!

----- original question -----

When I add a new article my facebook autopost uses the correct image, correct title, and the correct URL but the body summary is from the previous article?

If I rebuild the rules cache it works correctly but only for the next entry then I need to rebuild the rules cache again.

{ "rules_fb_new_artice" : {
    "LABEL" : "FB - New Artice",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "facebook" ],
    "REQUIRES" : [ "rules", "php", "fb_autopost_entity" ],
    "ON" : { "node_insert--article" : { "bundle" : "article" } },
    "DO" : [
      { "entity_create" : {
          "USING" : {
          "type" : "facebook_publication",
          "param_type" : "post",
          "param_user" : [ "site:current-user" ]
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
        }
      },
      { "data_set" : {
          "data" : [ "entity-created:field-facebook-name" ],
          "value" : [ "node:title" ]
        }
      },
      { "data_set" : {
          "data" : [ "entity-created:field-facebook-picture" ],
          "value" : "\u003C?php\r\n if($node-\u003Efield_image) {\r\n if(array_key_exists(0, $node-\u003Efield_image[\u0027und\u0027])) {\r\n $fid = $node-\u003Efield_image[\u0027und\u0027][0][\u0027fid\u0027];\r\n\t $result = db_select(\u0027file_managed\u0027, \u0027fm\u0027)\r\n\t\t-\u003Efields(\u0027fm\u0027, array(\u0027uri\u0027))\r\n\t\t-\u003Econdition(\u0027fm.fid\u0027, $fid,\u0027=\u0027)\r\n\t\t-\u003Eexecute()\r\n\t\t-\u003EfetchAssoc();\r\n $path = $result[\u0027uri\u0027];\r\n $style_name = \u0027large\u0027;\r\n print (image_style_url($style_name, $path));\r\n }\r\n }\r\n?\u003E"
        }
      },
      { "data_set" : {
          "data" : [ "entity-created:field-facebook-link" ],
          "value" : [ "node:url" ]
        }
      },
      { "data_set" : {
          "data" : [ "entity-created:field-facebook-description" ],
          "value" : [ "node:body:summary" ]
        }
      },
      { "publish_to_facebook" : {
          "USING" : { "publication" : [ "entity-created" ], "pages" : "140044559353737" },
          "PROVIDE" : { "facebook_publication_id" : { "facebook_publication_id" : "Facebook publication returned ID" } }
        }
      },
      { "entity_save" : { "data" : [ "entity-created" ] } }
    ]
  }
}

Here is a screenprint of the above rule with proper formatting.

Some more details: When a new article is submitted a Facebook post is made using the article title, article url, article summary and article image.

  • Comments are not for extended discussion; this conversation has been moved to chat. – Clive Aug 26 '16 at 14:24
  • Note that I (manually) edited the formatting of your exported rule (ie to make the indenting look like in the (now removed again) image. I recommend you also add a module link in your question, and you paste (in text format, not as an image) that PHP code (in your 3rd rules action) in your question also (together with some words about what that code is doing/needed for). – Pierre.Vriens Aug 26 '16 at 17:53
0

Your publish_to_facebook is NOT your very last Rules action. In fact, as per the save_entity which follows it, it appears that you are publishing to Facebook something that hasn't been saved yet. So that at least seems to explain what you see happening.

Any attempt to get it to work should ensure that you only publish something that has been saved. So you may want to try by just swapping the last 2 rules actions.

The above is just a quick and dirty attempt, and I doubt you'll actually get it to work like that. But your question reminds me about issue # 430274, and somehow explains that "after saving something" actually happens "before saving something". This is not a bug, but simply how the Rules module works.

A possible compromise to avoid these kind of issues, is to move your publish_to_facebook action into a second Rules or component in order to ensure that the node is definitely saved correctly.

  • Method 1: Your original Rule "schedules the execution" (using the Rules Scheduler sub-module) of that Rules component. For example after only a few seconds or minutes if your cron job runs frequent enough (otherwise it'll be next time cron runs). This compromise will ensure that, at the time the Rules component is executed, the entity is for sure saved (so that can't be the reason anymore then why things don't work as expected).

  • Method 2: Add a Boolean field to the node type and when using Rules to create the node, set it to "off". Then add a redirect to the created node as the last action.
    After that, create a second rule for "Content is viewed" which will make the Facebook post.
    Condition: "Boolean field == Off".
    Action 1: Post to Facebook using viewed node's data.
    Action 2: Set a Data Value: Boolean field == "On".

If none of the above helps, maybe it's worth to do some Rules debugging, as detailed in "How can I display the value of a variable inside a condition?".

Note: In 1 of your (moved to chat) comments, you also added this:

Facebook returns a unique identifier that must be saved as part of the entity so I can further code updates or deletes if necessary.

If I was to make this work, I would first make the original question work, which is about "add a new article" (updates or deletes is an additional challenge). Because as long as you've not succeeded in that, there is nothing to update or delete ...

  • I'd like to add that an easy way of making sure a node is actually saved is to have a redirect to the node as the last action. Then a rule for "content is viewed" with a Boolean condition, then Facebook post action and finally change the Boolean so it never fires again. – Niall Murphy Aug 26 '16 at 18:22
  • @NiallMurphy : please go ahead with whatever you mean by that. If you want to edit my answer: approve. But why not add your own answer also? With or without some reference to my answer that's up to you. I bet I'd learn from it also ... – Pierre.Vriens Aug 26 '16 at 18:25
  • True true. I''ll revisit this tomorrow morning. – Niall Murphy Aug 26 '16 at 18:35
  • 1
    @NiallMurphy : too bad there is no FLAG module enabled on drupal.SE. If it was, I'd have flagged it with a 'wait for' GTD flag ... (and not for moderation intervention ...). If you want, please add a done-ping comment when the time is right. – Pierre.Vriens Aug 26 '16 at 18:39
  • Hey @NiallMurphy ... merci beaucoup for that "Method 2"-rework you did, what a genius way to t transform the entire Rules based process in a multi-step equivalent. But, why not MOVE that to a separated answer (combined with a re-edit of my answer, which I pre-approve. If you do, anybody who wants to do so, can (up?) vote for your answer also (I will for sure if you do). I might probably add an update to my answer, with something like "method 1 could/should work, but the other one from Niall is definitely worth considering also (I like it!)". – Pierre.Vriens Aug 27 '16 at 10:50

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.