Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow chart dropdown labels to be translatable. #22349

Merged
merged 1 commit into from
Jan 3, 2022

Conversation

braders
Copy link
Contributor

@braders braders commented Dec 31, 2021

Overview

Allow chart dropdown labels to be translatable.

Before

Prior to this change, the strings 'Tabular', 'Bar Chart' and 'Pie Chart' were hardcoded. This affects a handful of reports, which support bar and pie chart views:

Screenshot 2021-12-31 at 12 22 53

After

It is possible for the strings to be translated, or for word replacement to be performed against the strings.

Technical Details

The labels were previously hardcoded within the $_charts array property against each report. However, in PHP properties cannot be directly assigned from a function call, so this would not have worked:

protected $_charts = [
  '' => ts('Tabular'),
  'barChart' => ts('Bar Chart'),
  'pieChart' => ts('Pie Chart'),
];

As a result, I have re-worked the $_charts property to only contain information about the types of charts supported, not the labels. The labels are added in the new getChartLabels method.

Every report supports a tabular display, and this is the default value. Therefore, this is no longer defined in the $_charts property, but instead gets added by default in getChartLabels().

@civibot civibot bot added the master label Dec 31, 2021
@civibot
Copy link

civibot bot commented Dec 31, 2021

(Standard links)

@braders braders force-pushed the chart-label-translatable branch 2 times, most recently from 2c09d80 to 28a4c4f Compare December 31, 2021 12:34
*/
private function getChartLabels() {
$charts = [];
$charts[''] = 'Tabular';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ts() for Tabular?

It might be more "standard" to do the assignment of _charts in each subclass's constructor, and then you don't need this function, but this seems ok too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, extra ts added. I was so focused on then other two that I missed this one.

This approach seemed like it had the advantage that the strings couldn't get out of sync between each report - I have seen a few instances where repeated strings don't match in terms of spelling, capitalisation etc. But I'm not overly fussed either way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I have noticed that at least one third-party extension has instroduced their own keys, and other extensions are using the structure that CiviCRM core is using at the moment:

https://github.com/SyedShahiq/mainpeople/blob/master/sites/default/civicrm/extensions/net.ourpowerbase.report.advancedfundraising/CRM/Advancedfundraising/Form/Report/Contribute/Recovery.php

https://github.com/eileenmcnaughton/nz.co.fuzion.extendedreport/blob/master/CRM/Extendedreport/Form/Report/Contribute/Overview.php

I could extend getChartLabels to continue to support the old array strucutre, but might be that moving the labels to the constuctor of each report is cleaner whilst still retaining backwards compatiability. I'll have a think about the best way forward.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on this thread I've reworked the code so that the translations are defined in the constructor of each report.

@braders braders force-pushed the chart-label-translatable branch from 28a4c4f to 9051cf5 Compare December 31, 2021 19:06
@braders braders marked this pull request as draft December 31, 2021 19:28
@@ -1574,12 +1580,35 @@ public function addOptions() {
*/
public function addChartOptions() {
if (!empty($this->_charts)) {
$this->addElement('select', "charts", ts('Chart'), $this->_charts);
$this->addElement('select', "charts", ts('Chart'), $this->getChartLabels($this->_charts));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@braders your passing through $this->_charts as a function param but the function in L1594 doesn't accept parameters

@eileenmcnaughton
Copy link
Contributor

test fails were new-year related - test this please

@braders braders force-pushed the chart-label-translatable branch from 9051cf5 to 4e2dce6 Compare January 3, 2022 11:44
@braders braders marked this pull request as ready for review January 3, 2022 11:45
@braders braders requested a review from demeritcowboy January 3, 2022 11:46
Prior to this change, the strings 'Tabular', 'Bar Chart' and 'Pie Chart' were hardcoded.
@braders braders force-pushed the chart-label-translatable branch from 4e2dce6 to f787b3c Compare January 3, 2022 11:52
@demeritcowboy
Copy link
Contributor

Looks good. Ran a couple. Good call on support for extensions still using the property.

Interestingly I can't see how to make the dropdown appear for the soft-credit report. The trigger doesn't seem to be the _charts variable on its own but some combination of that and chartSupported and/or a field having 'chart' => TRUE. But doesn't change anything about the PR.

@demeritcowboy demeritcowboy merged commit dd0aaff into civicrm:master Jan 3, 2022
@braders
Copy link
Contributor Author

braders commented Jan 3, 2022

Looks good. Ran a couple. Good call on support for extensions still using the property.

Interestingly I can't see how to make the dropdown appear for the soft-credit report. The trigger doesn't seem to be the _charts variable on its own but some combination of that and chartSupported and/or a field having 'chart' => TRUE. But doesn't change anything about the PR.

Hm, good spot. It appears that each of the charts with _charts also have a $this->assign('chartSupported', TRUE);. This isn't called by the soft credit report. I tried adding chartSupported to soft-credit, but charts didn't actually appear. So I think soft-credit report doesn't actually support charts, and probably shouldn't be setting the _charts array.

@braders braders deleted the chart-label-translatable branch January 3, 2022 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants