ApiDocSettingsForm.php in Apigee API Catalog 8
File
src/Entity/Form/ApiDocSettingsForm.php
View source
<?php
namespace Drupal\apigee_api_catalog\Entity\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ApiDocSettingsForm extends FormBase {
public const CONFIG_NAME = 'apigee_api_catalog.apidoc.config';
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function getFormId() {
return 'apigee_api_catalog_settings';
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity_type = $this->entityTypeManager
->getStorage('apidoc')
->getEntityType();
$config = $this
->configFactory()
->getEditable(static::CONFIG_NAME);
$options = $form_state
->getValue('options');
$config
->set('default_revision', (bool) $options['new_revision'])
->save();
$this
->messenger()
->addStatus($this
->t('@type settings have been updated.', [
'@type' => $entity_type
->getLabel(),
]));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$entity = $this->entityTypeManager
->getStorage('apidoc')
->create();
$form['apigee_api_catalog_settings']['#markup'] = $this
->t('Settings for Apigee API catalog. Manage field settings using the tabs above.');
$form['additional_settings'] = [
'#type' => 'vertical_tabs',
];
$form['workflow'] = [
'#type' => 'details',
'#title' => $this
->t('Publishing options'),
'#group' => 'additional_settings',
];
$workflow_options = [
'new_revision' => $entity
->shouldCreateNewRevision(),
];
$workflow_options_keys = array_keys(array_filter($workflow_options));
$workflow_options = array_combine($workflow_options_keys, $workflow_options_keys);
$form['workflow']['options'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Default options'),
'#default_value' => $workflow_options,
'#options' => [
'new_revision' => $this
->t('Create new revision'),
],
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
];
return $form;
}
}