certificate-4.0.0-alpha1/src/Form/CertificateConfigForm.php

src/Form/CertificateConfigForm.php
<?php

namespace Drupal\certificate\Form;

use Drupal;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Class CertificateConfigForm.
 */
class CertificateConfigForm extends ConfigFormBase {

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $settings = $this->config('certificate.settings');
    $certificate_mappers = Drupal::service('plugin.manager.certificate_mapper');
    $mapper_definitions = $certificate_mappers->getDefinitions();

    // Certificate Snapshots
    $form['certificate']['snapshot_fieldset'] = [
      '#title' => $this->t('Certificate snapshots'),
      '#type' => 'fieldset',
    ];
    $form['certificate']['snapshot_fieldset']['snapshots'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enabled'),
      '#description' => $this->t('Certificates will only be generated once per entity and user.'),
      '#default_value' => $settings->get('snapshots'),
    ];

    // Global mappings
    $form['certificate']['maps'] = [
      '#title' => $this->t('Default mappings'),
      '#description' => $this->t('When no mappings are defined, these will be used.'),
      '#type' => 'fieldset',
      '#tree' => TRUE,
    ];
    // Get certs and prep options
    foreach ($mapper_definitions as $map_type => $map) {
      $plugin = $certificate_mappers->createInstance($map_type, ['of' => 'configuration values']);
      $form['certificate']['maps'][$map_type] = [
        '#title' => Html::escape($map['label']),
        '#type' => 'details',
        '#group' => TRUE,
        '#description' => Xss::filterAdmin($map['description']),
        '#open' => FALSE,
      ];

      $keys = $plugin->getMapKeys();
      if (!empty($keys)) {
        foreach ($keys as $key => $title) {
          $form['certificate']['maps'][$map_type][$key] = [
            '#type' => 'select',
            '#title' => Xss::filter($title),
            '#options' => $this->getCertificateTemplateOptions(),
            '#default_value' => $settings->get("maps.$map_type.$key") ?? '',
          ];
        }
      }
      else {
        $form['certificate']['maps'][$map_type]['empty'] = [
          '#markup' => '<p>' . $this->t('There are no mappings available for %title.', ['%title' => $map['label']]) . '</p>',
        ];
      }
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->config('certificate.settings');
    $form_state->cleanValues();
    $vals = $form_state->getValues();
    foreach ($vals as $key => $value) {
      $config->set($key, $value);
    }
    $config->save();
    parent::submitForm($form, $form_state);
  }

  /**
   * Return a list of certificate templates suitable as options in a select list
   */
  public static function getCertificateTemplateOptions() {
    $options = ['-1' => t('- prevent certificate -'),];
    $certificates = Drupal::entityTypeManager()->getStorage('certificate_template')->loadMultiple();
    foreach ($certificates as $cid => $cert_ent) {
      $options[$cert_ent->get('cid')->value] = $cert_ent->label();
    }
    return $options;
  }

}

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

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