Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/custom_forms / src / Form / CustomFormsSettingsForm.php
Size: Mime:
<?php

namespace Drupal\custom_forms\Form;

use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Class CustomFormsSettingsForm
 *
 * The settings form for the global custom form settings.
 *
 * @package Drupal\custom_forms\Form
 */
class CustomFormsSettingsForm extends ConfigFormBase {

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

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

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

    $form['field_list_settings'] = [
      '#type' => 'details',
      '#title' => $this->t('Field list settings'),
      '#description' => $this->t("These settings are all related to a custom form's field list.")
    ];
    $form['field_list_settings']['off_screen_field_settings'] = [
      '#type' => 'radios',
      '#title' => $this->t('Updating field list on field settings save'),
      '#description' => $this->t("This determines what should happen when a field's settings is saved via the off-screen dialog, and the field list itself has unsaved changes."),
      '#options' => [
        'rebuild' => $this->t('Rebuild the field list, discarding any unsaved field list changes.'),
        'reload' => $this->t('Reload the field list, saving any unsaved field list changes.')
      ],
      '#default_value' => !empty($config->get('off_screen_field_settings'))? $config->get('off_screen_field_settings') : 'reload'
    ];

    $salt = !empty($config->get('submission_salt'))? $config->get('submission_salt') : '';
    if (empty($salt)) {
      $salt = \Drupal\Component\Utility\Crypt::randomBytesBase64(55);
    }

    $form['submission_settings'] = [
      '#type' => 'details',
      '#title' => t('Submission settings'),
      '#description' => t("These settings are all related to a custom forms form's submission functionality.")
    ];
    $form['submission_settings']['submission_salt'] = [
      '#type' => 'textfield',
      '#title' => t('Salt'),
      '#description' => t('The salt key used to generate the unique ID\'s used for submission id.'),
      '#default_value' => $salt
    ];

    $bundle_type_config = $config->get('receipt_bundle_types');

    $form['entity_list'] = [
      '#type' => 'details',
      '#title' => $this->t('Receipt field'),
      '#description' => $this->t('Choose which bundles should get the submission receipt added to them.')
    ];

    /** @var ContentEntityType[] $content_entity_types */
    $content_entity_types = [];
    $entity_type_definations = \Drupal::entityTypeManager()->getDefinitions();
    /* @var $definition EntityTypeInterface */
    foreach ($entity_type_definations as $definition) {
      if ($definition instanceof ContentEntityType) {
        $content_entity_types[] = $definition;
      }
    }

    foreach ($content_entity_types as $content_entity_type) {
      if (!empty($content_entity_type->getBundleEntityType()) && $content_entity_type->id() !== 'custom_form') {

        $bundles = \Drupal::entityTypeManager()->getStorage($content_entity_type->getBundleEntityType())->loadMultiple();

        if (!empty($bundles)) {
          $form['entity_list'][$content_entity_type->id()] = [
            '#parents' => ['receipt_bundle_types', $content_entity_type->id()],
            '#type' => 'checkboxes',
            '#title' => $content_entity_type->getLabel()
          ];

          if (!empty($bundle_type_config[$content_entity_type->id()])) {
            $form['entity_list'][$content_entity_type->id()]['#default_value'] = $bundle_type_config[$content_entity_type->id()];
          }

          foreach ($bundles as $bundle) {
            $form['entity_list'][$content_entity_type->id()]['#options'][$bundle->id()] = $bundle->label();
          }
        }
      }
    }

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

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->configFactory->getEditable('custom_forms.settings')
      ->set('off_screen_field_settings', $form_state->getValue('off_screen_field_settings'))
      ->set('submission_salt', $form_state->getValue('submission_salt'))
      ->set('receipt_bundle_types', $form_state->getValue('receipt_bundle_types'))
      ->save();

    parent::submitForm($form, $form_state);
  }

}