public function FeedsYouTubeFetcherFeedForm::buildConfigurationForm in Feeds: YouTube Parser 8
Form constructor.
Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.
Parameters
array $form: An associative array containing the initial structure of the plugin form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().
Return value
array The form structure.
Overrides PluginFormInterface::buildConfigurationForm
File
- src/
Feeds/ Fetcher/ Form/ FeedsYouTubeFetcherFeedForm.php, line 66
Class
- FeedsYouTubeFetcherFeedForm
- Provides a form on the feed edit page for the FeedsYouTubeFetcher.
Namespace
Drupal\feeds_youtube\Feeds\Fetcher\FormCode
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FeedInterface $feed = NULL) {
$form['source'] = [
'#title' => $this
->t('Feed URL'),
'#type' => 'url',
'#default_value' => $feed
->getSource(),
'#maxlength' => 2048,
'#required' => TRUE,
];
// Authenticate/revoke access to Google Account.
$config = $this->plugin
->getConfiguration();
if ($feed
->id() && !empty($config['google_oauth_client_id']) && !empty($config['google_oauth_client_secret']) && !empty($config['google_developer_key'])) {
// Get existing token (if one exists) and check for code query parameter.
$cid = $this->plugin
->getAccessTokenCacheId($feed
->id());
$google_access_token = \Drupal::service('cache.feeds_youtube_tokens')
->get($cid);
$code = \Drupal::request()->query
->get('code');
$form['google_access_token'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Google OAuth 2.0 Authentication'),
];
if (!empty($code)) {
$state = \Drupal::request()->query
->get('state');
$tempstore_state = $this->privateTempStoreFactory
->get('feeds_youtube')
->get('state');
if (strval($tempstore_state) !== strval($state)) {
\Drupal::messenger()
->addError($this
->t('The session state did not match. Please try again.'));
}
else {
$client = $this->plugin
->getClientFactory($feed
->id());
$client
->authenticate($code);
// Save access token.
$cache_tags = [
'feeds_youtube:google_access_token',
$cid,
];
\Drupal::service('cache.feeds_youtube_tokens')
->set($cid, $client
->getAccessToken(), CacheBackendInterface::CACHE_PERMANENT, $cache_tags);
// Clean up and let the user know they successfully authenticated.
$this->privateTempStoreFactory
->get('feeds_youtube')
->delete('state');
\Drupal::messenger()
->addStatus($this
->t('You have successfully authenticated your website to use the Google API.'));
// Redirect back to the original page.
$current_path = \Drupal::service('path.current')
->getPath();
$response = new RedirectResponse($current_path, 302);
$response
->send();
}
}
elseif (empty($google_access_token->data)) {
$current_path = \Drupal::service('path.current')
->getPath();
$path_alias = \Drupal::service('path_alias.manager')
->getAliasByPath($current_path);
$form['google_access_token']['info'] = [
'#type' => 'markup',
'#markup' => '<p><em>' . $this
->t('Before authenticating, you must add <a href=":link">this URL</a> as an authorized redirect URI to your Google OAuth 2.0 client ID.', [
':link' => Url::fromUserInput($path_alias, [
'absolute' => TRUE,
])
->toString(),
]) . '</em></p>',
];
$client = $this->plugin
->getClientFactory($feed
->id());
$state = mt_rand();
$client
->setState($state);
$this->privateTempStoreFactory
->get('feeds_youtube')
->set('state', $state);
// Create authentication link.
$url = Url::fromUri($client
->createAuthUrl());
$auth_link = Link::fromTextAndUrl($this
->t('Authenticate Google Account'), $url);
$auth_link = $auth_link
->toRenderable();
$auth_link['#attributes'] = [
'class' => [
'button',
'button--primary',
],
];
$form['google_access_token']['grant_access'] = [
'#type' => 'markup',
'#markup' => '<p>' . render($auth_link) . '</p>',
];
}
else {
$form['google_access_token']['info'] = [
'#type' => 'markup',
'#markup' => '<p><em>' . $this
->t('Last authenticated: @date', [
'@date' => \Drupal::service('date.formatter')
->format($google_access_token->data['created'], 'long'),
]) . '</em></p>',
];
$form['google_access_token']['revoke_access'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Revoke access to Google Account'),
'#default_value' => FALSE,
];
}
}
return $form;
}