headless_cms-1.0.3/modules/headless_cms_preview/src/Form/HeadlessCmsPreviewSettingsForm.php

modules/headless_cms_preview/src/Form/HeadlessCmsPreviewSettingsForm.php
<?php

declare(strict_types=1);

namespace Drupal\headless_cms_preview\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\encrypt\EncryptionProfileManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a form for Headless CMS Preview settings.
 */
class HeadlessCmsPreviewSettingsForm extends ConfigFormBase {

  protected function __construct(
    ConfigFactoryInterface $configFactory,
    TypedConfigManagerInterface $typedConfigManager,
    protected readonly EntityTypeManagerInterface $entityTypeManager,
    protected readonly EntityTypeBundleInfoInterface $entityTypeBundleInfo,
    protected readonly ?EncryptionProfileManagerInterface $encryptionProfileManager,
  ) {
    parent::__construct($configFactory, $typedConfigManager);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
      $container->get('config.typed'),
      $container->get('entity_type.manager'),
      $container->get('entity_type.bundle.info'),
      $container->get('encrypt.encryption_profile.manager', $container::NULL_ON_INVALID_REFERENCE),
    );
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('headless_cms_preview.settings');

    $bundles = $this->entityTypeBundleInfo->getBundleInfo('node');
    $bundleOptions = array_keys($bundles);
    $bundleOptions = array_combine(
      array_keys($bundles),
      array_map(fn($bundle) => $bundle['label'], $bundles)
    );

    $encryptionProfiles = [];
    foreach ($this->encryptionProfileManager->getAllEncryptionProfiles() as $profile) {
      $encryptionProfiles[$profile->id()] = $profile->label();
    }

    $form['settings'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Node Preview'),
      '#open' => TRUE,

      'enabled_bundles' => [
        '#type' => 'checkboxes',
        '#title' => $this->t('Enable preview for node bundles'),
        '#description' => $this->t('Select which node bundles should have headless preview functionality enabled.'),
        '#options' => $bundleOptions,
        '#default_value' => $config->get('enabled_bundles') ?: [],
        '#required' => FALSE,
      ],

      'encryption_profile' => [
        '#type' => 'select',
        '#title' => $this->t('Encryption profile'),
        '#description' => $this->encryptionProfileManager
          ? $this->t('The encryption profile to use for preview tokens.')
          : $this->t('Please install the <code>Encrypt</code> module to enable support for encryption!'),
        '#options' => [
          '' => $this->t('None (no encryption - INSECURE!)'),
          ...$encryptionProfiles,
        ],
        '#default_value' => $config->get('encryption_profile'),
        '#required' => FALSE,
        '#disabled' => $this->encryptionProfileManager ? FALSE : TRUE,
      ],
    ];

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->config('headless_cms_preview.settings');

    $cleanValues = array_filter($form_state->getValue('enabled_bundles'));
    $config->set('enabled_bundles', $cleanValues);
    $config->set('encryption_profile', $form_state->getValue('encryption_profile'));

    $config->save();
  }

}

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

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