social_post_facebook-8.x-1.x-dev/src/Plugin/SocialPlatform/Facebook.php

src/Plugin/SocialPlatform/Facebook.php
<?php

namespace Drupal\social_post_facebook\Plugin\SocialPlatform;

use Drupal\Core\Link;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\mposse\Annotation\SocialPlatform;
use Drupal\mposse\SocialPlatformBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\social_post_facebook\FacebookPostAuthManager;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * A service for posting Drupal content to Facebook.
 *
 * @SocialPlatform(
 *   id = "facebook",
 *   label = "Facebook",
 *   description = @Translation("Posts to facebook"),
 *   max_char_count = 1000
 * )
 */
#[SocialPlatform(
  id: "facebook",
  label: "Facebook",
  description: new TranslatableMarkup("Posts to facebook"),
  max_char_count: 1000
)]
class Facebook extends SocialPlatformBase implements ContainerFactoryPluginInterface {

  /**
   * The social post facebook post manager service.
   *
   * @var \Drupal\social_post_facebook\FacebookPostAuthManager
   */
  protected $postManager;

  /**
   * Constructs a facebook Social Platform plugin instance.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\social_post_facebook\FacebookPostAuthManagere $post_manager
   *   The social post facebook post auth manager service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, FacebookPostAuthManager $post_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->postManager = $post_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('facebook_post.social_post_auth_manager'),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function checkStatus() {
    $user_id = 0;
    $pages = [];
    $accounts = _social_post_facebook_get_accounts_by_uid(($user_id));
    $account = array_pop($accounts);
    if (!$account) {
      return FALSE;
    }
    $pages = social_post_facebook_get_pages($account);
    return !empty($pages);
  }

  /**
   * {@inheritdoc}
   */
  public function setupDocs() {
    $url = Url::fromRoute('social_post_facebook.sitewide_account_form');
    $link = Link::fromTextAndUrl('sitewide Facebook account', $url);
    $link_string = $link->toString();
    return t('Activation of the Facebook plugin requires that the site is authorized to post to pages on behalf of a %sitewide_account', ['%sitewide_account' => $link_string]);
  }

  /**
   * {@inheritdoc}
   */
  public function postMicroblog(array $post_data) {
    // The "user id" of the sitewide Facebook account.
    $user_id = 0;
    $message = $post_data['message'];
    $this->postManager->doPost($message, $user_id);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc