From 9e310a701c282c9122b533fe2246a997f8dbe944 Mon Sep 17 00:00:00 2001 From: Shivam-Innoraft <shivam.tiwari@innoraft.com> Date: Thu, 2 Jan 2025 18:51:15 +0530 Subject: [PATCH 1/2] Make code compatible with unpublished node functionality. --- node_weight.module | 13 +++++++++++-- src/Form/NodeOrderForm.php | 6 +++--- src/Form/NodeWeightForm.php | 24 +++++++++++++++++++++++- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/node_weight.module b/node_weight.module index 1a0e84c..26a8138 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 539aef1..ecbc82f 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 44dd5c4..f2e9b3f 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(); -- GitLab From 01c6b4e5e5494d0a3c99f831121e7dc4697735b0 Mon Sep 17 00:00:00 2001 From: Shivam-Innoraft <shivam.tiwari@innoraft.com> Date: Tue, 28 Jan 2025 17:30:11 +0530 Subject: [PATCH 2/2] Updated condition related to default values. --- src/Form/NodeWeightForm.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Form/NodeWeightForm.php b/src/Form/NodeWeightForm.php index f2e9b3f..998ef67 100644 --- a/src/Form/NodeWeightForm.php +++ b/src/Form/NodeWeightForm.php @@ -83,14 +83,14 @@ class NodeWeightForm extends ConfigFormBase { '#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, + '#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, + '#default_value' => $config->get('node_weight.max_weight') ?? 10, ]; $form['include_unpublished'] = [ -- GitLab