Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
It is common that tweets have media attached (image, animated GIF, or video). Many Drupal sites also use at least an image for a post, or you can attach a media file.
The Twitter API allows for uploading of media (https://developer.twitter.com/en/docs/media/upload-media/overview), but it would be a two step process:
- Upload the media either via simple upload (https://developer.twitter.com/en/docs/media/upload-media/api-reference/p...) or via chunked upload (https://developer.twitter.com/en/docs/media/upload-media/api-reference/p...), depending on media type and/or size. We would then get the media_id from the response.
- Include the media_ids in the status update
Would people think that's useful?
Comment | File | Size | Author |
---|---|---|---|
#15 | 2975062-add_option_to_attach_media_to_tweet.patch | 3.62 KB | joshua.boltz |
Comments
Comment #1
Anonymous (not verified) CreditAttribution: Anonymous at Netuxo Ltd (RIP) commentedAndreas Speck created an issue. See original summary.
Comment #2
gvsoYes, I agree that we should allow this kind of tweets. It might be nice if we can actually customize tweets in the content editor.
But since the module currently doesn't support that, there are always ways around to get the SDK (the library used by Social Post Twitter) and perform any requests to Twitter API #2949855: Port to Social Post 2.x
Hint:
\Drupal::service('plugin.network.manager')->createInstance('social_post_twitter')->getSdk2($access_token, $access_token_secret);
should return the library and allow you to execute any requests.Comment #3
DamienMcKennaThere's some code in #1981472: Support media/upload endpoint that might help with this.
Comment #4
joshua.boltz CreditAttribution: joshua.boltz at Mediacurrent commentedAttached is a patch that adds support for media with status text.
Mainly, it just allows $this->status to be the entire array Twitter needs to send a Media + Text.
So, it's on the developer to update their own custom implementation logic that does the posting and to make make use of the ->getSdk2() method (per @gvso's comment above) to post the media up to Twitter, and then pass along the media IDs like Twitter needs to include them in the post.
In my case, I am getting an image field URL path, pushing the media up to Twitter, then after media has been added, construct the $params array with the status and media_ids to actually create the Twitter post.
This is currently only against 8.x-1.x. I have not had much luck with the 8.x-2.x versions of the Social post modules.
Comment #5
DamienMcKennaComment #6
DamienMcKennaThanks for putting that together.
A few things about the patch:
Comment #7
joshua.boltz CreditAttribution: joshua.boltz at Mediacurrent commentedComment #8
joshua.boltz CreditAttribution: joshua.boltz at Mediacurrent commentedThe patch was updated to be backward-compatible, and also removed the unnecessary changes to the info.yml.
With this, an implementer can supply a simple text string (as is the case without the patch), or an array that contains the media information + the tweet text in their own ->doPost() call.
Comment #9
DamienMcKennaComment #10
Anonymous (not verified) CreditAttribution: Anonymous at Netuxo Ltd (RIP) commentedDo I understand this correctly - the patch supports to provide the media ids as part of the array, which means I need to deal with uploading the media to Twitter to obtain the ids separately?
Comment #11
DamienMcKennaI think some documentation should be added showing how to use the API for attaching media.
Comment #12
joshua.boltz CreditAttribution: joshua.boltz at Mediacurrent commentedYes, the patch updates the ->doPost() method in social post twitter to allow an array to be passed in instead of just a string like pre-patch.
The Twitter API needs an array with a status key, which is for the text, and a media_ids keys, which is a comma separated list of media ids, so yes, before doing that doPost, you need to perform the API call to Twitter to upload the media and get their IDs back in the request.
You would need to apply the patch, and then in your implementation logic when the node inserts/updates, you need to add logic to upload the media to Twitter and build an array like
Hope that helps.
Comment #13
joshua.boltz CreditAttribution: joshua.boltz at Mediacurrent commentedHere's what I've done.
I have a custom function I run inside of hook_entity_insert() and hook_entity_update that does some stuff, including posting to Twitter.
In that function, I have this logic, which is essentially what you can find in the Social Post Twitter docs (https://www.drupal.org/node/2780771):
Comment #14
gvsoThanks @joshua.boltz! It might be nicer if we have a helper method to upload images in Social Post Twitter itself. The ideal API might allow the following:
Comment #15
joshua.boltz CreditAttribution: joshua.boltz at Mediacurrent commentedNew patch that includes a helper method to upload media, if needed. It's up to the developer's implementation logic to work out and pass along the media paths, so Twitter knows what it's uploading and attaching to the post.
This worked out really well, and cleaned up and removed the need for some of the logic in the implementation logic.
Here is an example of the updated implementation logic that can be added in a node creation or update life cycle hook or event:
Let me know if any thoughts.
Comment #16
joshua.boltz CreditAttribution: joshua.boltz at Mediacurrent commentedComment #17
Anonymous (not verified) CreditAttribution: Anonymous at Netuxo Ltd (RIP) commentedThis looks interesting. Is this likely to be committed?
Comment #18
Anonymous (not verified) CreditAttribution: Anonymous at Netuxo Ltd (RIP) commentedI can confirm that the patch works nicely. It would be really good to get this into the next release!
Comment #19
DamienMcKennaComment #20
gvsoI haven't tested the patch yet, but I can see that the example would produce an error since there's not such an entity type named
social_post_twitter_user
in 2.x. Instead, we use the general entity type provided by Social Post itself.Comment #21
DamienMcKennaPutting back to "needs work" as it needs to be updated to match the 2.0 APIs.
Comment #22
joshua.boltz CreditAttribution: joshua.boltz at Mediacurrent commentedThe patch I've included has been updated to work with 2.0 APIs.
The snippet in #20 is the code a developer should use in their own implementation in something like a hook_entity_update(), and that also has been updated to work with the change in 2.0 that @gvso is speaking of.
Apparently, I somehow posted the wrong thing. Here is what I meant to post, which is the code that would be in a custom module, in something like a hook_entity_update():
Comment #23
gvsoThank you very much @joshua.boltz!
Just to make things shorter to focus on code related to Social Post Twitter, here's an example of a tweet with an image when a new node is created.
Comment #25
gvsoAwesome job everyone!