Using the modules Rules and Facebook Autopost, the previous post body summary is used instead of the current post

drupal.stackexchange https://drupal.stackexchange.com/questions/212851

  •  12-12-2020
  •  | 
  •  

Pergunta

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.

Foi útil?

Solução

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 ...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a drupal.stackexchange
scroll top