I am using the Drupal for Facebook module on both a D6 and D7 site to post to a FB page (not a group or a personal account/wall) via streams.

It is working fine except I would like the 'Post to Facebook' checkbox be automatically checked only when the node is created, but not on subsequent edits. My users won't check this box on their own (and I may not even want them to see it).

I've tried using Rules, but there seems to be no FB posting action there.

I'm assuming it can be done via hook_form_alter, but have had no luck with that. The Element ID for the checkbox is... fb_stream[fb_stream_do_post]

Any thoughts???



  • Can you dump the entire contents of the $form variable when you do your alter to see what the exact path to it is? (Or use a debugger if you have one handy and set a break at the first line of your empty alter hook) – webkenny Jan 31 '15 at 3:01
  • Using level_themer to dump... @webkenny #type (String, 8 characters ) checkbox #title (String, 16 characters ) Post to Facebook #post (Array, 0 elements) #array_parents (Array, 2 elements) 0 (String, 9 characters ) fb_stream 1 (String, 17 characters ) fb_stream_do_post #processed (Boolean) TRUE #attributes (Array, 0 elements) #required (Boolean) FALSE #input (Boolean) TRUE #return_value (Integer) 1 #name (String, 17 characters ) fb_stream_do_post #id (String, 22 characters ) edit-fb-stream-do-post #value (String, 0 characters ) #defaults_loaded (Boolean) TRUE – Eric Aitala Jan 31 '15 at 6:20
  • You could use the hook_node_presave function to set the value of the checkbox to 1. – Akko Jan 31 '15 at 9:13
  • That would work but it wouldn't provide the feedback to the user in case they do not want to post. – webkenny Jan 31 '15 at 12:42

Thanks everyone.

What I was able to do was build a Rule with Form Events that sets the 'Post to Facebook' checkbox (and one for Twitter) if the current page URL matches the node edit URL. This way only the creation of a node, and not subsequent edits, are posted to FB...

I was initially going to do a hook_form_alter, but once the initial Rules test worked I figured I would go with it.

Rule Export >>

{ "rules_form_alter_post_to_fb" : {
    "LABEL" : "Form Alter - Article Post to FB and Twitter",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_forms" ],
    "ON" : { "rules_forms_article_node_form_form_built" : [] },
    "IF" : [
      { "text_matches" : {
          "text" : [ "site:current-page:url" ],
          "match" : "http:\/\/www.example.com\/node\/add\/article"
    "DO" : [
      { "rules_forms_set_default" : {
          "form" : [ "form" ],
          "element" : "checkbox:fb_stream:fb_stream_do_post",
          "value" : 1
      { "rules_forms_set_default" : { "form" : [ "form" ], "element" : "checkbox:twitter:post", "value" : 1 } }

Ok, so based on your example. Recall that the checked property of a toggle is a HTML attribute. Then you'd want to do something like this:

$form_state['fb_stream']['fb_stream_do_post']['#attributes']["checked"] = "checked"

This would produce the following HTML, IIRC:

<input type="checkbox" name="fb_stream_do_post" checked="checked" />

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.