packthub_ebook_integration-1.0.0/src/Form/PacktForm.php

src/Form/PacktForm.php
<?php

namespace Drupal\packt\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class PacktForm extends FormBase {

  /**
   * The configuration factory.
   *
   * @var ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * @var MessengerInterface
   */
  protected $messenger;

  /**
   * Constructs a new CustomFormExampleForm object.
   *
   * @param ConfigFactoryInterface $config_factory
   *   The configuration factory.
   */
  public function __construct(ConfigFactoryInterface $config_factory, MessengerInterface $messenger) {
    $this->configFactory = $config_factory;
    $this->messenger = $messenger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container): PacktForm|static {
    return new static(
      $container->get('config.factory'),
      $container->get('messenger')
    );
  }

  /**
   * {@inheritdoc }
   */
  public function getFormId(): string {
    return "ebook_api_token_form";
  }

  /**
   *{@inheritdoc }
   */
  public function buildForm(array $form, FormStateInterface $form_state): array {
    $form['api_token'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Your Packthub token'),
      '#required' => TRUE,
    ];

    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Submit'),
    ];
    return $form;
  }

  /**
   *{@inheritdoc }
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {

    $config = $this->configFactory->getEditable('packt.settings');
    if($config->set('api_token', $form_state->getValue('api_token'))->save()) {
      $this->messenger->addMessage("Packthub api token saved");
    }
    else {
      $this->messenger->addError("Sorry failed to save packthub api token");
    }
  }
}

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

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