widen_media-1.0.x-dev/widen_media.module
widen_media.module
<?php
/**
* @file
* Contains widen_media hooks.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\widencollective\WidencollectiveAuthService;
/**
* Implements hook_form_alter().
*/
function widen_media_form_alter(&$form, FormStateInterface $form_state, $form_id) {
global $base_url;
if ($form_id === 'widencollective.admin_form') {
$widenAccount = \Drupal::state()->get('widencollective_auth', FALSE);
$collective_domain = \Drupal::config('widencollective.settings')->get('collective_domain');
if (!empty($collective_domain)) {
$form['widen'] = [
'#type' => 'details',
'#title' => t('Widen Collective Authorization'),
'#weight' => 7,
'#open' => TRUE,
];
if (isset($widenAccount['widen_token'])) {
$widen_username = $widenAccount['widen_username'];
$form['widen']['widen_remove_auth'] = [
'#type' => 'checkbox',
'#title' => t('Remove Widen Collective authorization'),
'#prefix' => t('Currently authorized with Widen Collective as "@username".', ['@username' => $widen_username]) . '<br />',
];
$form['actions']['submit']['#submit'][] = 'widen_media_unauthorize';
}
else {
$return_link = $base_url . Url::fromRoute('widen_media.auth')->toString();
$form['widen']['widen_auth_link'] = [
'#markup' => '<a href="' . WidencollectiveAuthService::generateAuthUrl($return_link) . '">' . t('Authorize with Widen Collective.') . '</a>',
];
}
}
}
}
/**
* Custom user form submit handler.
*/
function widen_media_unauthorize($form, FormStateInterface $form_state) {
if ($form_state->getValue('widen_remove_auth')) {
$widen_account = \Drupal::state()->get('widencollective_auth', FALSE);
if ($widen_account !== FALSE) {
WidencollectiveAuthService::cancel($widen_account['widen_token']);
\Drupal::state()->delete('widencollective_auth');
}
}
}
