EdgeEntityCacheConfigFormBase.php in Apigee Edge 8
File
src/Form/EdgeEntityCacheConfigFormBase.php
View source
<?php
namespace Drupal\apigee_edge\Form;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
abstract class EdgeEntityCacheConfigFormBase extends ConfigFormBase {
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config($this
->getConfigNameWithCacheSettings());
$form['cache'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Caching'),
'#collapsible' => FALSE,
];
$form['cache']['cache_expiration'] = [
'#type' => 'number',
'#title' => $this
->t('Expires'),
'#description' => $this
->t('Number of <strong>seconds</strong> until a cached item expires. Use <em>-1</em> to cache items until they have been updated on the Developer Portal (ignore changes made on the Apigee Edge Management UI or in an external application). Use <em>0</em> to completely disable caching.'),
'#default_value' => $config
->get('cache_expiration'),
'#min' => -1,
'#required' => TRUE,
];
$form['cache']['actions'] = [
'#type' => 'actions',
];
$form['cache']['actions']['invalidate_cache'] = [
'#type' => 'submit',
'#value' => $this
->t('Invalidate cache'),
'#limit_validation_errors' => [],
'#submit' => [
'::invalidateCache',
],
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->config($this
->getConfigNameWithCacheSettings())
->set('cache_expiration', $form_state
->getValue('cache_expiration'))
->save();
parent::submitForm($form, $form_state);
}
public function invalidateCache(array $form, FormStateInterface $form_state) {
Cache::invalidateTags([
$this
->getEntityType(),
]);
}
protected function getConfigNameWithCacheSettings() : string {
$configs = $this
->getEditableConfigNames();
return reset($configs);
}
protected abstract function getEntityType() : string;
}