4

I'm using public function fieldLabels() in my DataObject to translate all field labels (as well as the labels for $summary_fields). This works fine for all fields, except one that has a value returned from a function (rather than value extracted from database).

Summary Fields

static $summary_fields = array(
    'Label' => 'Label',
    'Type' => 'Type',
    'getRequiredLabel' => 'Required'
);

FieldLabels

public function fieldLabels($includerelations = true) {
    $labels = parent::fieldLabels(true);

    $labels['Label'] = _t('UserForm.Label', 'Label');
    $labels['Type'] = _t('UserForm.Type', 'Type');
    $labels['Required'] = _t('UserForm.Required', 'Required');

    return $labels;
}

All fields are neatly translated through fieldLabels() except for Required because this has a custom value from a function rather than from data. Changing getRequiredLabel to Required fixes this.

Any way I can keep the value getRequiredLabel for the record fields and have the translated label in the top column?

enter image description here

Semicolon
  • 1,904
  • 14
  • 26

1 Answers1

4

Set $summary_fields getRequiredLabel variable to RequiredLabel and use $field_labels to set the field labels.

private static $summary_fields = array(
    'Label',
    'Type',
    'RequiredLabel'
);

private static $field_labels = array(
    'RequiredLabel' => 'Required'
);

Then in fieldLabels the RequiredLabel column should be accessed with $labels['RequiredLabel'].

public function fieldLabels($includerelations = true) {
    $labels = parent::fieldLabels(true);

    $labels['Label'] = _t('UserForm.Label', 'Label');
    $labels['Type'] = _t('UserForm.Type', 'Type');
    $labels['RequiredLabel'] = _t('UserForm.Required', 'Required');

    return $labels;
}

You could also use the Boolean modifier Nice here to achieve the same effect.

private static $summary_fields = array(
    'Label',
    'Type',
    'Required.Nice'
);

private static $field_labels = array(
    'Required.Nice' => 'Required'
);

public function fieldLabels($includerelations = true) {
    $labels = parent::fieldLabels(true);

    $labels['Label'] = _t('UserForm.Label', 'Label');
    $labels['Type'] = _t('UserForm.Type', 'Type');
    $labels['Required.Nice'] = _t('UserForm.Required', 'Required');

    return $labels;
}
3dgoo
  • 15,716
  • 6
  • 46
  • 58
  • That doesn't seem to work oddly. If I rename everything to *Required* the translation works fine, but then a new issue arises. The required field is a boolean, and I have a function named **getRequiredLabel** (bad function naming, its not a label) that translates the boolean from 0 and 1 to more userfriendly Yes and No. – Semicolon Jul 31 '16 at 11:08
  • 1
    Hmm, interesting. I'll have to give this more of a test. As a side note we can use `Required.Nice` in `$summary_fields` to make the results displays _Yes_ and _No_. – 3dgoo Jul 31 '16 at 11:12
  • Ha, my getRequiredLabel function seems to be unnecessary then. Here's the whole code if you want to give it a test: http://www.sspaste.com/paste/show/579ddd716a48f – Semicolon Jul 31 '16 at 11:16
  • And the lang file **nl.yml**: http://www.sspaste.com/paste/show/579dde2fc7459 *Required* is translated to *Vereist* – Semicolon Jul 31 '16 at 11:17
  • I've tested this out and by using `$field_labels` to set the field labels this solution works. – 3dgoo Jul 31 '16 at 13:08
  • 1
    I have no idea what is happening here, but it works. Would it be unfair to say that this labeling structure is rather oddly built in the cms core files? – Semicolon Jul 31 '16 at 17:15
  • 1
    Yeah, that's a fair call. From my experience always set field labels with `$field_labels` and never make `$summary_fields` an associative array. While making `$summary_fields` an associative array works most of the time, on some occasions it breaks stuff, like what we had in the answer before. I feel it is better to be safe and never make `$summary_fields` an associative array. – 3dgoo Jul 31 '16 at 20:56