smsplatform-1.0.x-dev/modules/smsplatform_sendtophone/src/Form/AdminOverviewForm.php

modules/smsplatform_sendtophone/src/Form/AdminOverviewForm.php
<?php

declare(strict_types=1);

namespace Drupal\smsplatform_sendtophone\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Defines admin overview form.
 */
class AdminOverviewForm extends ConfigFormBase {

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new AdminOverviewForm.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger) {
    parent::__construct($config_factory);
    $this->entityTypeManager = $entity_type_manager;
    $this->setMessenger($messenger);
  }

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

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'smsplatform_sendtophone_admin_overview';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $node_types = $this->entityTypeManager->getStorage('node_type')->loadMultiple();
    $types = [];
    foreach ($node_types as $type) {
      $types[$type->get('type')] = $type->get('name');
    }
    $form['content_types'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Content types'),
      '#default_value' => $this->config('smsplatform_sendtophone.settings')->get('content_types'),
      '#options' => $types,
      '#description' => $this->t('Which content types to show the Send To Phone feature.'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->config('smsplatform_sendtophone.settings')
      ->set('content_types', array_filter($form_state->getValue('content_types')))
      ->save();

    $this->messenger()->addMessage($this->t('The configuration options have been saved.'));
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['smsplatform_sendtophone.settings'];
  }

}

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

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