-
Notifications
You must be signed in to change notification settings - Fork 0
/
social_post_twitter.module
126 lines (111 loc) · 3.48 KB
/
social_post_twitter.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* @file
* Contains social_post_twitter.module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Session\AccountInterface;
/**
* Implements hook_form_FORM_ID_alter().
*/
function social_post_twitter_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_twitter_can_grant_permission($current_user)) {
// Add a button to authorize twitter autoposting.
$form += _social_post_twitter_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 tweeting on the user's behalf.
*/
function _social_post_twitter_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 twitter 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_twitter_user_edit_form(AccountInterface $current_user) {
$accounts = _social_post_twitter_get_accounts_by_uid($current_user->id());
$form['social_post_twitter'] = [
'#type' => 'details',
'#title' => t('Social Post Twitter'),
'#open' => TRUE,
];
$form['social_post_twitter']['accounts'] = [
'#type' => 'table',
'#header' => [t('Screen name'), t('Operations')],
'#empty' => t('You have not added any accounts yet.'),
];
/* @var \Drupal\social_post\Entity\SocialPost $account */
foreach ($accounts as $id => $account) {
$form['social_post_twitter']['accounts'][$id]['screen_name'] = [
'#type' => 'link',
'#title' => $account->getName(),
'#url' => Url::fromUri('https://twitter.com/' . $account->getProviderUserId()),
];
$form['social_post_twitter']['accounts'][$id]['operations'] = [
'#type' => 'operations',
'#links' => [
'delete' => [
'title' => t('Delete'),
'url' => Url::fromRoute('entity.social_post.delete_form',
[
'provider' => 'twitter',
'social_post' => $account->getId(),
'user' => $current_user->id(),
]
),
],
],
];
}
$form['social_post_twitter']['button'] = [
'#type' => 'link',
'#title' => t("Add account"),
'#attributes' => [
'class' => ['button'],
],
'#url' => Url::fromRoute('social_post_twitter.redirect'),
];
return $form;
}
/**
* Gets the accounts associated to the Drupal user.
*
* @param int $user_id
* The user id.
*
* @return \Drupal\Core\Entity\EntityInterface[]
* Accounts associated to the user id.
*/
function _social_post_twitter_get_accounts_by_uid($user_id) {
$accounts = \Drupal::entityTypeManager()->getStorage('social_post')->loadByProperties([
'user_id' => $user_id,
'plugin_id' => 'social_post_twitter',
]);
return $accounts;
}