social_post_facebook-8.x-1.x-dev/src/Form/FacebookPostSitewideForm.php
src/Form/FacebookPostSitewideForm.php
<?php
namespace Drupal\social_post_facebook\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Sitewide Facebook account form.
*/
class FacebookPostSitewideForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'social_post_facebook.sitwide_account_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
/** @var \Drupal\social_post\Entity\SocialPost[] $accounts */
$accounts = _social_post_facebook_get_accounts_by_uid(0);
$form['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['accounts'][$id]['screen_name'] = [
'#type' => 'link',
'#title' => $account->getName(),
'#url' => Url::fromUri('https://facebook.com/' . $account->getProviderUserId()),
];
$form['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' => 0,
]),
],
],
];
}
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Add account'),
'#access' => empty($accounts),
];
$form['help'] = [
'#type' => 'item',
'#markup' => $this->t('When adding an account, please only authorize the app to post to a single page.'),
'#access' => empty($accounts),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$url = Url::fromRoute('social_post_facebook.redirect_to_facebook_for_sitewide');
$form_state->setRedirectUrl($url);
}
}
