-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocial_post_linkedin.module
137 lines (120 loc) · 3.73 KB
/
social_post_linkedin.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
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* @file
* Install, update, and uninstall functions for the Social Post LinkedIn.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Session\AccountInterface;
/**
* Implements hook_form_FORM_ID_alter().
*/
function social_post_linkedin_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_linkedin_can_grant_permission($current_user)) {
// Add a button to authorize LinkedIn autoposting.
$form += _social_post_linkedin_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 their behalf.
*/
function _social_post_linkedin_can_grant_permission(AccountInterface $current_user) {
$routeMatch = \Drupal::service('current_route_match');
// If the current user has permission to share on LinkedIn and its id is the
// same as the user id of parameter.
if ($current_user->hasPermission('perform linkedin 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_linkedin_user_edit_form(AccountInterface $current_user) {
$accounts = _social_post_linkedin_get_accounts_by_uid($current_user->id());
$form['social_post_linkedin'] = [
'#type' => 'details',
'#title' => t('Social Post LinkedIn'),
'#open' => TRUE,
];
$form['social_post_linkedin']['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) {
$link = $account->getLink();
if ($link->isEmpty()) {
$form['social_post_linkedin']['accounts'][$id]['screen_name'] = [
'#type' => 'label',
'#title' => $account->getName(),
];
}
else {
$form['social_post_linkedin']['accounts'][$id]['screen_name'] = [
'#type' => 'link',
'#title' => $link->title,
'#url' => $link->getUrl(),
];
}
$form['social_post_linkedin']['accounts'][$id]['operations'] = [
'#type' => 'operations',
'#links' => [
'delete' => [
'title' => t('Delete'),
'url' => Url::fromRoute('entity.social_post.delete_form',
[
'provider' => 'linkedin',
'social_post' => $account->getId(),
'user' => $current_user->id(),
]
),
],
],
];
}
$form['social_post_linkedin']['button'] = [
'#type' => 'link',
'#title' => t("Add account"),
'#attributes' => [
'class' => ['button'],
],
'#url' => Url::fromRoute('social_post_linkedin.redirect_to_linkedin'),
];
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_linkedin_get_accounts_by_uid($user_id) {
$accounts = \Drupal::entityTypeManager()->getStorage('social_post')->loadByProperties([
'user_id' => $user_id,
'plugin_id' => 'social_post_linkedin',
]);
return $accounts;
}