Skip to content
Snippets Groups Projects
Commit 1f5d0746 authored by Jon Minder's avatar Jon Minder
Browse files

Refactor and use state to not resave the options.

parent 615238bd
Branches
Tags 1.0.3
No related merge requests found
......@@ -23,9 +23,9 @@ function frontend_routing_form_node_form_alter(&$form, &$form_state) {
}
// Check if the alias matches for the pattern.
$keys = \Drupal::config('frontend_routing.settings')->get('keys') ?? [];
foreach ($keys as $key => $value) {
if (empty($value['entity_id']) && !empty($value['bundle'])) {
$types = \Drupal::state()->get('frontend_routing.settings_type') ?? [];
foreach ($types as $value) {
if (($value['generic']) && !empty($value['bundle'])) {
if (!$node->isNew() && $node->bundle() === $value['bundle']) {
$form['path']['#access'] = FALSE;
$form['path_replace']['#weight'] = $form['path']['#weight'];
......
......@@ -63,6 +63,7 @@ final class SettingsForm extends ConfigFormBase {
$config = $this->config('frontend_routing.settings');
$nodes = $this->state->get('frontend_routing.settings') ?? [];
$types = $this->state->get('frontend_routing.settings_type') ?? [];
$keys = $config->get('keys') ?? [];
......@@ -101,7 +102,7 @@ final class SettingsForm extends ConfigFormBase {
'#type' => 'select',
'#options' => $this->getNodeTypes(),
'#required' => TRUE,
'#default_value' => !empty($keys[$key]['bundle']) ? $keys[$key]['bundle'] : NULL,
'#default_value' => !empty($types[$key]['bundle']) ? $types[$key]['bundle'] : NULL,
];
$required = TRUE;
......@@ -228,6 +229,7 @@ final class SettingsForm extends ConfigFormBase {
public function submitForm(array &$form, FormStateInterface $form_state): void {
$nodes = [];
$types = [];
// Save data from the table.
$keys = $this->config('frontend_routing.settings')->get('keys') ?? [];
......@@ -257,16 +259,20 @@ final class SettingsForm extends ConfigFormBase {
$table = $form_state->getValue('table');
if (is_array($table)) {
foreach ($table as $key => $value) {
$keys[$key]['bundle'] = $value['bundle'];
$types[$key]['bundle'] = $value['bundle'];
$types[$key]['generic'] = TRUE;
if (!empty($value['entity_id'])) {
$nodes[$key] = $value['entity_id'];
$types[$key]['generic'] = FALSE;
}
}
$this->config('frontend_routing.settings')
->set('keys', $keys)->save();
}
$this->updateNodeAliasSetting($nodes);
$this->state->set('frontend_routing.settings_type', $types);
parent::submitForm($form, $form_state);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment