Skip to content
Snippets Groups Projects
Commit 9a68398b authored by Marcos Rios's avatar Marcos Rios Committed by SUDISHTH KUMAR
Browse files

Issue #3151725 by mpaulo, kavitasonawane, Swapnil_Kotwal: t() calls should be avoided in classes

parent c3a859d1
Branches
Tags
No related merge requests found
......@@ -4,6 +4,7 @@ namespace Drupal\date_range_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeCustomFormatter;
use Drupal\datetime_range\DateTimeRangeTrait;
......@@ -24,6 +25,7 @@ use Drupal\datetime_range\DateTimeRangeTrait;
class DateRangeFormatterRangeFormatter extends DateTimeCustomFormatter {
use DateTimeRangeTrait;
use StringTranslationTrait;
/**
* {@inheritdoc}
......@@ -65,7 +67,7 @@ class DateRangeFormatterRangeFormatter extends DateTimeCustomFormatter {
if (date('d.m.Y', $start_date) === date('d.m.Y', $end_date)) {
$format = $this->getSetting('one_day');
}
$date_str = \Drupal::service('date.formatter')->format($start_date, 'custom', preg_replace('/\{([a-zA-Z])\}/', '{\\\$1}', t($format)));
$date_str = \Drupal::service('date.formatter')->format($start_date, 'custom', preg_replace('/\{([a-zA-Z])\}/', '{\\\$1}', $this->t($format)));
$matches = array();
if (preg_match_all('/\{([a-zA-Z])\}/', $date_str, $matches)) {
foreach ($matches[1] as $match) {
......@@ -78,7 +80,7 @@ class DateRangeFormatterRangeFormatter extends DateTimeCustomFormatter {
if (!array_key_exists($delta, $elements)) {
// No end date provided or end date equals start date use single formatting.
$single_format = $this->getSetting('single');
$elements[$delta] = ['#markup' => \Drupal::service('date.formatter')->format($start_date, 'custom', t($single_format))];
$elements[$delta] = ['#markup' => \Drupal::service('date.formatter')->format($start_date, 'custom', $this->t($single_format))];
}
}
}
......@@ -91,31 +93,41 @@ class DateRangeFormatterRangeFormatter extends DateTimeCustomFormatter {
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
unset($form['date_format']);
$form['single'] = [
'#type' => 'textfield',
'#title' => $this->t('Date format for single date'),
'#default_value' => $this->getSetting('single') ? : 'd F Y',
];
$form['single_all_day'] = [
'#type' => 'textfield',
'#title' => $this->t('Date format for the single date if the date is "all day"'),
'#default_value' => $this->getSetting('single_all_day') ? : 'd F Y',
];
$form['one_day'] = [
'#type' => 'textfield',
'#title' => t('Date format for the single day date range'),
'#title' => $this->t('Date format for the single day date range'),
'#default_value' => $this->getSetting('one_day') ? : 'd F Y',
];
$form['one_month'] = [
'#type' => 'textfield',
'#title' => t('Date format for the single month date range'),
'#title' => $this->t('Date format for the single month date range'),
'#default_value' => $this->getSetting('one_month') ? : 'd - {d} F Y',
];
$form['several_months'] = [
'#type' => 'textfield',
'#title' => t('Date format for the single year date range'),
'#title' => $this->t('Date format for the single year date range'),
'#default_value' => $this->getSetting('several_months') ? : 'd F - {d} {F} Y',
];
$form['several_years'] = [
'#type' => 'textfield',
'#title' => t('Date format for multiple years date range'),
'#title' => $this->t('Date format for multiple years date range'),
'#default_value' => $this->getSetting('several_years') ? : 'd F Y - {d} {F} {Y}',
];
$form['help'] = [
'#type' => 'markup',
'#markup' => t('A user-defined date format. See the <a href="@url">PHP manual</a> for available options.', ['@url' => 'http://php.net/manual/function.date.php']) .
'<br />' . t('Use letters in braces for end date elements, for example, {d} means the day of the end date.') .
'<br />' . t('These format values are translated, for example, t("d F Y") instead of "d F Y" will be used as the actual date format.'),
'#markup' => $this->t('A user-defined date format. See the <a href="@url">PHP manual</a> for available options.', ['@url' => 'http://php.net/manual/function.date.php']) .
'<br />' . $this->t('Use letters in braces for end date elements, for example, {d} means the day of the end date.') .
'<br />' . $this->t('These format values are translated, for example, t("d F Y") instead of "d F Y" will be used as the actual date format.'),
];
return $form;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment