Skip to content

Commit

Permalink
Issue #117: Tokens in field descriptions (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
indigoxela authored Sep 16, 2023
1 parent bd14fa1 commit 9291b67
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
4 changes: 2 additions & 2 deletions i18n_field/i18n_field.module
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ function i18n_field_field_widget_form_alter(&$element, &$form_state, $context) {
$alter_element['#description'] = theme('file_upload_help', $help_variables);
}
}
elseif ($alter_element['#description'] == field_filter_xss($instance['description'])) {
$alter_element['#description'] = field_filter_xss($instance_current['description']);
elseif ($alter_element['#description'] == field_filter_xss(token_replace($instance['description']))) {
$alter_element['#description'] = field_filter_xss(token_replace($instance_current['description']));
}
}
}
Expand Down
56 changes: 56 additions & 0 deletions i18n_field/tests/i18n_field.test
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,60 @@ class i18nFieldTestCase extends Backdropi18nTestCase {
$this->assertRaw($default_value_translation[$this->secondaryLanguage], 'Default value translated');
}

/**
* Test the translation of field description with token replacement.
*/
public function testTextFieldTranslationTokens() {
$field_name = backdrop_strtolower($this->randomName());
$description = 'Foobar description for [site:name]';

$field = array(
'field_name' => $field_name,
'type' => 'text',
'cardinality' => 1,
);
field_create_field($field);

$instance = array(
'field_name' => $field_name,
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'label' => $this->randomName(),
'description' => $description,
'widget' => array(
'type' => 'text_textfield',
),
);
field_create_instance($instance);

// Refresh i18n_strings.
$edit = array('groups[field]' => TRUE);
$this->backdropPost('admin/config/regional/translate/i18n_string', $edit, t('Refresh strings'));

// Save description translation.
// We can't use createStringTranslation because of the white spaces in our
// string.
$description_es = 'Foobar descripción para [site:name]';
$description_es_tok = 'Foobar descripción para ' . config_get('system.core', 'site_name');

$this->backdropLogin($this->translator);
$search = array(
'string' => $description,
'language' => 'all',
'translation' => 'untranslated',
);
$this->backdropPost('admin/config/regional/translate/translate', $search, t('Filter'));
$this->clickLink(t('Edit'));
$edit = array(
'translations[' . $this->secondaryLanguage . ']' => $description_es,
);
$this->backdropPost(NULL, $edit, t('Save translations'));
$this->assertText(t('The string has been saved.'));

// Verify that token got replaced in translated field display.
$this->backdropLogin($this->admin_user);
$this->backdropGet($this->secondaryLanguage . '/test-entity/add/test-bundle');
$this->assertText($description_es_tok);
}

}

0 comments on commit 9291b67

Please sign in to comment.