- Issue created by @interdruper
- 🇬🇧United Kingdom 2dareis2do
Hi interdruper,
Although not strictly required, Rules (or similar) is the recommended way to use this module.
e.g. If I want to post content on publish, I can set up a rule for this.
In fact, I have also created a rules plugin that helps prevent submission on no prod environments.
https://www.drupal.org/project/rules_condition_environment →
If you are using this differently please can you elaborate how?
Thanks.
- 🇬🇧United Kingdom 2dareis2do
Still interested to know how you are using this but I have removed rules Drupal dependency for now as it does seem reasonable that it can be used in other contexts.
- 🇪🇸Spain interdruper
Hi 2dareis2do
thanks for your fast response.
We use XPostManager encapsulated in a custom service, so it can contains logic for any specific context.
E.g.: for scheduling the post of tweets, with the help of modules like Scheduler → or our preferred Scheduler Field → .
I attached a code sample (skeleton) of a custom service using XPostManager, in case it is useful to someone with the same requirement:
<?php namespace Drupal\custom_module\Service; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\Url; use Drupal\node\Entity\Node; use Drupal\social_api\Plugin\NetworkManager; use Drupal\social_api\User\UserManagerInterface; use Drupal\social_post_x\Plugin\Network\XPostInterface; use Drupal\social_post_x\XPostManagerInterface; use Drupal\taxonomy\Entity\Term; use Drupal\user\Entity\User; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Post a tweet */ class PostTweet { use StringTranslationTrait; /** * The X post network plugin. * * @var \Drupal\social_post_x\Plugin\Network\XPostInterface */ protected $xPoster; /** * The network manager. * * @var \Drupal\social_api\Plugin\NetworkManager */ private NetworkManager $networkManager; /** * The social api user manager. * * @var \Drupal\social_post\User\UserManager */ protected $userManager; /** * The X post network plugin. * * @var \Drupal\social_post_x\XPostManager */ protected $postManager; /** * The entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; /** * The messenger. * * @var \Drupal\Core\Messenger\MessengerInterface */ protected $messenger; /** * The logger factory. * * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface */ private $logger; /** * The current user. * * @var \Drupal\Core\Session\AccountInterface */ protected $currentUser; /** * The current route match. * * @var \Drupal\Core\Routing\RouteMatchInterface */ protected $routeMatch; /** * The constructor. * * @param \Drupal\social_api\Plugin\NetworkManager $networkManager * The network manager. * @param \Drupal\social_api\User\UserManagerInterface $user_manager * The social user manager. * @param \Drupal\social_post_x\XPostManagerInterface $post_manager * The X post manager. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. * @param \Drupal\Core\Messenger\MessengerInterface $messenger * The messenger. * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory * The logger factory object. * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The string translation. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The current route match */ public function __construct(NetworkManager $networkManager, UserManagerInterface $user_manager, XPostManagerInterface $post_manager, EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger, LoggerChannelFactoryInterface $loggerFactory, TranslationInterface $string_translation, AccountInterface $current_user, RouteMatchInterface $route_match) { $this->networkManager = $networkManager; $this->userManager = $user_manager; $this->postManager = $post_manager; $this->entityTypeManager = $entity_type_manager; $this->messenger = $messenger; $this->logger = $loggerFactory->get('custom_module'); $this->stringTranslation = $string_translation; $this->currentUser = $current_user; $this->routeMatch = $route_match; $this->xPoster = $this->networkManager->createInstance('social_post_x'); $client = $this->xPoster->getSdk(); $this->postManager->setClient($client); } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('plugin.network.manager'), $container->get('social_post.user_manager'), $container->get('x_post.manager'), $container->get('entity_type.manager'), $container->get('messenger'), $container->get('logger.factory'), $container->get('string_translation'), $container->get('current_user'), $container->get('current_route_match') ); } /** * Post twweet. * * @param string $content * Tweet content. * * @param array $image_path * Array of full realpaths or URLs to image files. * * @call \Drupal::service('custom_module.post_tweet')->postTweet($content, $image_paths); */ public function postTweet(string $content, array $image_paths = []) : void { if (empty($content)) { return; } // Include your custom logic here, using the available injected services... $tweet['text'] = $content; foreach ($image_paths as $image_path) { $tweet['media_paths'][] = $image_path; } $this->postManager->doPost($tweet); } }
module_custom.service.yml
services: custom_module.post_tweet: class: '\Drupal\custom_module\Service\PostTweet' arguments: - '@plugin.network.manager' - '@social_post.user_manager' - '@x_post.manager' - '@entity_type.manager' - '@messenger' - '@logger.factory' - '@string_translation' - '@current_user' - '@current_route_match'
- Status changed to RTBC
9 months ago 1:28pm 27 March 2024