Optimize State Field for Dropping Center Form to Show Only Indian States#385
Optimize State Field for Dropping Center Form to Show Only Indian States#385belwalshubham merged 1 commit intodevelopfrom
Conversation
WalkthroughThe changes in this pull request enhance the Changes
Possibly related PRs
Suggested labels
Suggested reviewers
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
- wp-content/civi-extensions/goonjcustom/Civi/DroppingCenterService.php (3 hunks)
🧰 Additional context used
🔇 Additional comments (2)
wp-content/civi-extensions/goonjcustom/Civi/DroppingCenterService.php (2)
10-10: Import statement looks good!The new import for
Civi\Api4\StateProvinceis correctly placed and relevant to the new functionality being added.
39-39: New event hook added correctly!The
'&hook_civicrm_fieldOptions' => 'setIndianStateOptions'hook has been properly added to thegetSubscribedEventsmethod. It follows the existing naming convention and is associated with the newly added method.
| public static function setIndianStateOptions(string $entity, string $field, array &$options, array $params) { | ||
| if ($entity !== 'Eck_Collection_Camp') { | ||
| return; | ||
| } | ||
|
|
||
| $customStateFields = CustomField::get(FALSE) | ||
| ->addWhere('custom_group_id:name', '=', 'Dropping_Centre') | ||
| ->addWhere('name', '=', 'State') | ||
| ->execute(); | ||
|
|
||
| $stateField = $customStateFields->first(); | ||
|
|
||
| $stateFieldId = $stateField['id']; | ||
|
|
||
| if ($field !== "custom_$stateFieldId") { | ||
| return; | ||
| } | ||
|
|
||
| $activeIndianStates = StateProvince::get(FALSE) | ||
| ->addWhere('country_id.iso_code', '=', 'IN') | ||
| ->addOrderBy('name', 'ASC') | ||
| ->execute(); | ||
|
|
||
| $stateOptions = []; | ||
| foreach ($activeIndianStates as $state) { | ||
| if ($state['is_active']) { | ||
| $stateOptions[$state['id']] = $state['name']; | ||
| } | ||
| } | ||
|
|
||
| $options = $stateOptions; | ||
|
|
||
| } |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Good implementation, but room for improvement
The setIndianStateOptions method is well-structured and follows the single responsibility principle. However, consider the following suggestions:
- Move the filtering of active states to the API call for better efficiency:
$activeIndianStates = StateProvince::get(FALSE)
->addWhere('country_id.iso_code', '=', 'IN')
+ ->addWhere('is_active', '=', TRUE)
->addOrderBy('name', 'ASC')
->execute();
$stateOptions = [];
foreach ($activeIndianStates as $state) {
- if ($state['is_active']) {
- $stateOptions[$state['id']] = $state['name'];
- }
+ $stateOptions[$state['id']] = $state['name'];
}-
Consider adding error handling for API calls to improve robustness.
-
The custom field retrieval could be moved to a separate method for better reusability and to adhere to the DRY principle.
Would you like me to provide a more detailed refactoring suggestion for these improvements?
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| public static function setIndianStateOptions(string $entity, string $field, array &$options, array $params) { | |
| if ($entity !== 'Eck_Collection_Camp') { | |
| return; | |
| } | |
| $customStateFields = CustomField::get(FALSE) | |
| ->addWhere('custom_group_id:name', '=', 'Dropping_Centre') | |
| ->addWhere('name', '=', 'State') | |
| ->execute(); | |
| $stateField = $customStateFields->first(); | |
| $stateFieldId = $stateField['id']; | |
| if ($field !== "custom_$stateFieldId") { | |
| return; | |
| } | |
| $activeIndianStates = StateProvince::get(FALSE) | |
| ->addWhere('country_id.iso_code', '=', 'IN') | |
| ->addOrderBy('name', 'ASC') | |
| ->execute(); | |
| $stateOptions = []; | |
| foreach ($activeIndianStates as $state) { | |
| if ($state['is_active']) { | |
| $stateOptions[$state['id']] = $state['name']; | |
| } | |
| } | |
| $options = $stateOptions; | |
| } | |
| public static function setIndianStateOptions(string $entity, string $field, array &$options, array $params) { | |
| if ($entity !== 'Eck_Collection_Camp') { | |
| return; | |
| } | |
| $customStateFields = CustomField::get(FALSE) | |
| ->addWhere('custom_group_id:name', '=', 'Dropping_Centre') | |
| ->addWhere('name', '=', 'State') | |
| ->execute(); | |
| $stateField = $customStateFields->first(); | |
| $stateFieldId = $stateField['id']; | |
| if ($field !== "custom_$stateFieldId") { | |
| return; | |
| } | |
| $activeIndianStates = StateProvince::get(FALSE) | |
| ->addWhere('country_id.iso_code', '=', 'IN') | |
| ->addWhere('is_active', '=', TRUE) | |
| ->addOrderBy('name', 'ASC') | |
| ->execute(); | |
| $stateOptions = []; | |
| foreach ($activeIndianStates as $state) { | |
| $stateOptions[$state['id']] = $state['name']; | |
| } | |
| $options = $stateOptions; | |
| } |
PR Description:
This update enhances the Dropping Center form by ensuring the State field displays only Indian states. The implementation includes the following:
Summary by CodeRabbit
New Features
Bug Fixes