diff --git a/node_weight.module b/node_weight.module index 1a0e84cfb20b0c2e5d8b368e2ec9cee43bb704a4..26a81385b978c2b4c4cd7513999d9e6bd36f9e96 100644 --- a/node_weight.module +++ b/node_weight.module @@ -116,6 +116,9 @@ function node_weight_remove_node_type_from_config($node_type) { function node_weight_create_field_node_weight($node_type) { $field_storage = FieldStorageConfig::loadByName('node', 'field_node_weight'); $field = FieldConfig::loadByName('node', $node_type, 'field_node_weight'); + $config = \Drupal::config('node_weight.settings'); + $min_weight = $config->get('node_weight.min_weight'); + $max_weight = $config->get('node_weight.max_weight'); if (empty($field)) { FieldConfig::create([ @@ -129,8 +132,9 @@ function node_weight_create_field_node_weight($node_type) { 'translatable' => TRUE, 'default_value' => 0, 'settings' => [ - 'min' => -100, - 'max' => 100, + 'min' => $min_weight, + 'max' => $max_weight, + ], ])->save(); @@ -141,6 +145,11 @@ function node_weight_create_field_node_weight($node_type) { ->setComponent('field_node_weight', ['type' => 'weight_selector']) ->save(); } + else { + $field->setSetting('min', $min_weight); + $field->setSetting('max', $max_weight); + $field->save(); + } } /** diff --git a/src/Form/NodeOrderForm.php b/src/Form/NodeOrderForm.php index 539aef1f98b371b014f1b15c5d51881209d193f8..ecbc82fc33c1801ad64102aec8b37582ba8a1e44 100644 --- a/src/Form/NodeOrderForm.php +++ b/src/Form/NodeOrderForm.php @@ -115,16 +115,16 @@ class NodeOrderForm extends ConfigFormBase { $language = $this->languageManager->getCurrentLanguage()->getId(); // Get nodes. - $nids = $this->entityTypeManager->getStorage('node')->getQuery() + $query = $this->entityTypeManager->getStorage('node')->getQuery() ->accessCheck(TRUE) ->condition('type', $node_type) ->condition('langcode', $language); // Unpublished nodes. if ($config->get('node_weight.include_unpublished')) { - $nids->execute(); + $nids = $query->execute(); } else { - $nids->condition('status', TRUE)->execute(); + $nids = $query->condition('status', TRUE)->execute(); } $nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nids); diff --git a/src/Form/NodeWeightForm.php b/src/Form/NodeWeightForm.php index 44dd5c4f720ffb4f93da33ed448123c45c9f2b47..998ef673fbf5ab09e4bd7457dbc2ed784189830e 100644 --- a/src/Form/NodeWeightForm.php +++ b/src/Form/NodeWeightForm.php @@ -79,11 +79,25 @@ class NodeWeightForm extends ConfigFormBase { '#options' => $options, ]; + $form['min_weight'] = [ + '#type' => 'number', + '#title' => $this->t('Minimum Weight'), + '#description' => $this->t('Specify the minimum value for the weight field. This is the lowest weight that can be assigned to a node.'), + '#default_value' => $config->get('node_weight.min_weight') ?? -10, + ]; + + $form['max_weight'] = [ + '#type' => 'number', + '#title' => $this->t('Maximum Weight'), + '#description' => $this->t('Specify the maximum value for the weight field. This is the highest weight that can be assigned to a node.'), + '#default_value' => $config->get('node_weight.max_weight') ?? 10, + ]; + $form['include_unpublished'] = [ '#type' => 'checkbox', '#title' => $this->t('Include Unpublished Nodes'), '#description' => $this->t('The toggle option for including unpublished nodes.'), - '#default_value' => $config->get('node_weight.include_unpublished') ?: 1, + '#default_value' => $config->get('node_weight.include_unpublished'), ]; return $form; @@ -93,6 +107,12 @@ class NodeWeightForm extends ConfigFormBase { * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { + $min_weight = $form_state->getValue('min_weight'); + $max_weight = $form_state->getValue('max_weight'); + + if (($min_weight > $max_weight) || ($min_weight == $max_weight)) { + $form_state->setErrorByName('min_weight', $this->t('Minimum Weight must be less than Maximum Weight.')); + } } /** @@ -101,6 +121,8 @@ class NodeWeightForm extends ConfigFormBase { public function submitForm(array &$form, FormStateInterface $form_state) { $config = $this->config('node_weight.settings'); $config->set('node_weight.checked_node_types', array_filter(array_values($form_state->getValue('checked_node_types')))); + $config->set('node_weight.min_weight', $form_state->getValue('min_weight')); + $config->set('node_weight.max_weight', $form_state->getValue('max_weight')); $config->set('node_weight.include_unpublished', $form_state->getValue('include_unpublished')); $config->save();