webex_client-1.0.5/src/Form/WebexForm.php

src/Form/WebexForm.php
<?php

declare(strict_types=1);

namespace Drupal\webex_client\Form;

use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Url;
use Drupal\webex_client\Entity\Webex;

/**
 * Provides a form for managing Webex entities.
 */
final class WebexForm extends EntityForm {

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state): array {
    /** @var \Drupal\webex_client\WebexInterface $entity */
    $entity = $this->entity;

    $form = parent::form($form, $form_state);

    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Label'),
      '#maxlength' => 255,
      '#default_value' => $entity->label(),
      '#required' => TRUE,
    ];

    $form['id'] = [
      '#type' => 'machine_name',
      '#default_value' => $entity->id(),
      '#machine_name' => [
        'exists' => [Webex::class, 'load'],
      ],
      '#disabled' => !$entity->isNew(),
    ];

    $form['webex_integration'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Webex Integration'),
    ];

    $form['webex_integration']['client_id'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Client ID'),
      '#default_value' => $entity->getClientId(),
      '#required' => TRUE,
      '#description' => $this->t('The Client ID for your Webex integration, used to identify your application.'),
    ];

    $form['webex_integration']['client_secret'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Client Secret'),
      '#default_value' => $entity->getClientSecret(),
      '#required' => TRUE,
      '#description' => $this->t('The Client Secret for your Webex integration, used to authenticate your application.'),
    ];

    $form['webex_integration']['scope'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Scope'),
      '#default_value' => $entity->getScope(),
      '#required' => TRUE,
      '#description' => $this->t(
        'URL-encoded list of requested @data_access_scopes and/or @OpenID_Connect_scopes separated by spaces. Specified data access scopes must be registered with your Webex integration. (OpenID Connect scopes do not need to be registered.) If scope is missing from the request the server will return an "invalid scope" error.', [
          '@data_access_scopes' => Markup::create('<a href="https://developer.webex.com/docs/integrations#scopes" target="_blank">data access scopes</a>'),
          '@OpenID_Connect_scopes' => Markup::create('<a href="https://developer.webex.com/docs/login-with-webex#openid-connect-scopes-and-claims" target="_blank">OpenID Connect scopes</a>'),
        ]),
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state): int {
    $result = parent::save($form, $form_state);
    /** @var \Drupal\webex_client\WebexInterface $entity */
    $entity = $this->entity;
    $entity
      ->set('status', 0)
      ->set('default', 0)
      ->setClientId($form_state->getValue('client_id'))
      ->setClientSecret($form_state->getValue('client_secret'))
      ->save();
    $redirect_uri = Url::fromRoute('webex_client.callback')->setAbsolute()->toString();

    $external_url = 'https://webexapis.com/v1/authorize?' . http_build_query([
      'client_id' => $form_state->getValue('client_id'),
      'response_type' => 'code',
      'redirect_uri' => $redirect_uri,
      'scope' => $form_state->getValue('scope'),
      'state' => $this->entity->id(),
    ]);

    $response = new TrustedRedirectResponse($external_url);
    $response->send();

    return $result;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc