EdgeEntityAliasConfigFormBase.php in Apigee Edge 8
File
src/Form/EdgeEntityAliasConfigFormBase.php
View source
<?php
namespace Drupal\apigee_edge\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
abstract class EdgeEntityAliasConfigFormBase extends ConfigFormBase {
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config($this
->getConfigNameWithLabels());
$form['label'] = [
'#type' => 'fieldset',
'#title' => $this
->t('How to refer to a @entity_type on the UI', [
'@entity_type' => $this
->entityTypeName(),
]),
'#collapsible' => FALSE,
];
$form['label']['entity_label_singular'] = [
'#type' => 'textfield',
'#title' => $this
->t('Singular format'),
'#default_value' => $config
->get('entity_label_singular'),
'#description' => $this
->t('Leave empty to use the default "@singular_label" label.', [
'@singular_label' => $this
->originalSingularLabel(),
]),
];
$form['label']['entity_label_plural'] = [
'#type' => 'textfield',
'#title' => $this
->t('Plural format'),
'#default_value' => $config
->get('entity_label_plural'),
'#description' => $this
->t('Leave empty to use the default "@plural_label" label.', [
'@plural_label' => $this
->originalPluralLabel(),
]),
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config($this
->getConfigNameWithLabels());
if ($config
->get('entity_label_singular') !== $form_state
->getValue('entity_label_singular') || $config
->get('entity_label_plural') !== $form_state
->getValue('entity_label_plural')) {
$config
->set('entity_label_singular', $form_state
->getValue('entity_label_singular'))
->set('entity_label_plural', $form_state
->getValue('entity_label_plural'))
->save();
drupal_flush_all_caches();
}
parent::submitForm($form, $form_state);
}
protected function getConfigNameWithLabels() : string {
$configs = $this
->getEditableConfigNames();
return reset($configs);
}
protected abstract function entityTypeName() : string;
protected abstract function originalSingularLabel() : string;
protected abstract function originalPluralLabel() : string;
}