contextly-8.x-2.1/src/Form/ContextlyNodeTypesAdminForm.php
src/Form/ContextlyNodeTypesAdminForm.php
<?php
namespace Drupal\contextly\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* The ContextlyNodeTypesAdminForm class.
*/
class ContextlyNodeTypesAdminForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'contextly.contextly_node_types_admin',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'contextly_node_types_admin_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form,
FormStateInterface $form_state) {
$config = $this->config('contextly.contextly_node_types_admin');
$types = node_type_get_names();
$form['types'] = [
'#type' => 'details',
'#title' => $this->t('Enabled node types'),
'#open' => TRUE,
];
$form['types']['info'] = [
'#type' => 'item',
// @todo Move to help.
'#markup' => $this->t("Integration with Contextly will be enabled for selected node types only."),
];
$form['types']['contextly_all_node_types'] = [
'#type' => 'checkbox',
'#title' => $this->t('All node types.'),
'#default_value' => $config->get('contextly_all_node_types') == 0 ? 0 : 1,
];
$form['types']['contextly_node_types'] = [
'#title' => $this->t('Enabled types'),
'#type' => 'checkboxes',
'#options' => $types,
'#default_value' => $config->get('contextly_node_types'),
'#states' => [
'visible' => [
'input[name="contextly_all_node_types"]' => [
'checked' => FALSE,
],
],
],
];
// Custom handler to clear fields cache on submit.
// @todo $form['#submit'][] = 'contextly_settings_node_types_form_submit_clear_cache'.
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form,
FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this->config('contextly.contextly_node_types_admin')
->set('contextly_all_node_types', $form_state->getValue('contextly_all_node_types'))
->set('contextly_node_types', $form_state->getValue('contextly_node_types'))
->save();
}
}
