social_post_facebook-8.x-1.x-dev/social_post_facebook.module
social_post_facebook.module
<?php
/**
* @file
* Contains social_post_facebook.module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Session\AccountInterface;
use Drupal\social_post\Entity\SocialPost;
/**
* Implements hook_form_FORM_ID_alter().
*/
function social_post_facebook_form_user_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// If the for is the user edit form, not user register form or others.
// @see https://www.drupal.org/node/2854977
if ($form_id == 'user_form') {
$current_user = \Drupal::currentUser();
if (_social_post_facebook_can_grant_permission($current_user)) {
// Add a button to authorize facebook autoposting.
$form += _social_post_facebook_user_edit_form($current_user);
}
}
}
/**
* Check if the user is allowed to grant permission for autoposting.
*
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*
* @return bool
* The user can or cannot allow post on his behalf.
*/
function _social_post_facebook_can_grant_permission(AccountInterface $current_user) {
$routeMatch = \Drupal::service('current_route_match');
// If the current user has permission to autotwet and its id is the same as
// the user id of parameter.
if ($current_user->hasPermission('perform facebook autoposting tasks')
&& $current_user->id() == $routeMatch->getParameter('user')->id()) {
return TRUE;
}
return FALSE;
}
/**
* Creates elements to the user edit form.
*
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*
* @return array
* The elements to add to the user edit form.
*/
function _social_post_facebook_user_edit_form(AccountInterface $current_user) {
$accounts = _social_post_facebook_get_accounts_by_uid($current_user->id());
$form['social_post_facebook'] = [
'#type' => 'details',
'#title' => t('Social Post Facebook'),
'#open' => TRUE,
];
$form['social_post_facebook']['accounts'] = [
'#type' => 'table',
'#header' => [t('Screen name'), t('Operations')],
'#empty' => t('You have not added any account yet.'),
];
/* @var \Drupal\social_post\Entity\SocialPost $account */
foreach ($accounts as $id => $account) {
$form['social_post_facebook']['accounts'][$id]['screen_name'] = [
'#type' => 'link',
'#title' => $account->getName(),
'#url' => Url::fromUri('https://facebook.com/' . $account->getProviderUserId()),
];
$form['social_post_facebook']['accounts'][$id]['operations'] = [
'#type' => 'operations',
'#links' => [
'delete' => [
'title' => t('Delete'),
'url' => Url::fromRoute('entity.social_post.delete_form', ['provider' => 'facebook', 'social_post' => $account->getId(), 'user' => $current_user->id()]),
],
],
];
}
$form['social_post_facebook']['button'] = [
'#type' => 'link',
'#title' => t("Add account"),
'#attributes' => [
'class' => ['button'],
],
'#url' => Url::fromRoute('social_post_facebook.redirect_to_facebook_for_user'),
];
$form['social_post_facebook']['help'] = [
'#type' => 'item',
'#markup' => t('When adding an account, please only authorize the app to post to a single page.'),
'#access' => empty($accounts),
];
return $form;
}
/**
* Gets the accounts associated to the Drupal user.
*
* @param int $user_id
* The user id.
*
* @return \Drupal\social_post\Entity\SocialPost[]
* Accounts associated to the user id.
*/
function _social_post_facebook_get_accounts_by_uid($user_id) {
$accounts = \Drupal::entityTypeManager()->getStorage('social_post')->loadByProperties([
'user_id' => $user_id,
'plugin_id' => 'social_post_facebook',
]);
return $accounts;
}
/**
* Gets the accounts associated to the Drupal user.
*
* @param \Drupal\social_post\Entity\SocialPost $account
* A social post entity.
*
* @return array
* An array containing details about pages associated with the facebook account.
*/
function social_post_facebook_get_pages($account) {
$value = $account->get('additional_data')->first()->getValue();
return unserialize($value['value']);
}
function social_post_facebook_post_to_page($user_id, $page_id, $message, $link = '') {
$accounts = _social_post_facebook_get_accounts_by_uid(($user_id));
$account = array_pop($accounts);
if (!$account) {
\Drupal::logger('social_post_facebook')->error('Could not post to Facebook page @page_id. No such account for @user_id',
[
'@page_id' => $page_id,
'@user_id' => $user_id,
]
);
return FALSE;
}
$pages = social_post_facebook_get_pages($account);
$page = isset($pages[$page_id]) ? $pages[$page_id] : FALSE;
if (!$page) {
\Drupal::logger('social_post_facebook')->error('Could not post to Facebook page @page_id. No such page for @user_id',
[
'@page_id' => $page_id,
'@user_id' => $user_id,
]
);
return FALSE;
}
$token = $page['long_access_token'];
$client = \Drupal::service('plugin.network.manager')->createInstance('social_post_facebook')->getSdk();
return $client->postToPage($page_id, $token, $message, $link);
}
