Skip to content
Snippets Groups Projects
Commit d8faad5b authored by abdulaziz zaid's avatar abdulaziz zaid
Browse files

Issue #2933183 by abdulaziz zaid, mostafadev, samaphp: Support Hijri format for views

parent bfac4841
No related branches found
No related tags found
1 merge request!1Issue #2933183: Support Hijri format for views
......@@ -6,4 +6,5 @@ core_version_requirement: ^8 || ^9
configure: hijri.admin_settings
dependencies:
- drupal:field
- drupal:views
- block
......@@ -182,3 +182,83 @@ if ($hijri_display != 'none') {
}
}
}
/**
* Implements hook_views_data_alter().
*/
function hijri_views_data_alter(array &$data): array {
$data['node_field_data']['created'] = [
'title' => t('Authored on'),
'help' => t('The time that the node was created.'),
'field' => [
// ID of field handler plugin to use.
'id' => 'hijri_views_field',
],
'sort' => [
'id' => 'date',
],
'filter' => [
'id' => 'date',
],
'argument' => [
'id' => 'date',
],
];
$data['node_field_data']['changed'] = [
'title' => t('Changed'),
'help' => t('The time that the node was last edited.'),
'field' => [
// ID of field handler plugin to use.
'id' => 'hijri_views_field',
],
'sort' => [
'id' => 'date',
],
'filter' => [
'id' => 'date',
],
'argument' => [
'id' => 'date',
],
];
$data['users_field_data']['created'] = [
'title' => t('Created'),
'help' => t('The time that the user was created.'),
'field' => [
// ID of field handler plugin to use.
'id' => 'hijri_views_field',
],
'sort' => [
'id' => 'date',
],
'filter' => [
'id' => 'date',
],
'argument' => [
'id' => 'date',
],
];
$data['users_field_data']['changed'] = [
'title' => t('Updated date'),
'help' => t('The time that the user was last edited.'),
'field' => [
// ID of field handler plugin to use.
'id' => 'hijri_views_field',
],
'sort' => [
'id' => 'date',
],
'filter' => [
'id' => 'date',
],
'argument' => [
'id' => 'date',
],
];
return $data;
}
<?php
namespace Drupal\hijri\Plugin\views\field;
use Drupal\views\Plugin\views\field\Date;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\ResultRow;
/**
* A handler to provide custom displays for Hijri dates.
*
* @ingroup views_field_handlers
*
* @ViewsField("hijri_views_field")
*/
class HijriViewsField extends Date {
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$hijri_formatter = \Drupal::service('hijri.formatter');
$form['date_format']['#options']['hijri_full'] = t('Hijri full format: @hijri on @gregorian', [
'@hijri' => $hijri_formatter->format(time(), 'custom', 'l j F Y'),
'@gregorian' => \Drupal::service('date.formatter')->format(time(), 'custom', 'F j, Y'),
]);
$form['date_format']['#options']['hijri_long'] = $this->t('Hijri long format: @date', ['@date' => $hijri_formatter->format(time(), 'long', NULL)]);
$form['date_format']['#options']['hijri_medium'] = $this->t('Hijri medium format: @date', ['@date' => $hijri_formatter->format(time(), 'medium', NULL)]);
$form['date_format']['#options']['hijri_short'] = $this->t('Hijri short format: @date', ['@date' => $hijri_formatter->format(time(), 'short', NULL)]);
$form['date_format']['#options']['hijri_custom'] = $this->t('Hijri custom');
$form['custom_date_format']['#states']['visible'][] = [
':input[name="options[date_format]"]' => ['value' => 'hijri_custom'],
];
}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$list = [
'hijri_long',
'hijri_medium',
'hijri_short',
];
$value = $this->getValue($values);
$hijri_formatter = \Drupal::service('hijri.formatter');
$format = $this->options['date_format'];
if ($format == 'hijri_full') {
return t('@hijri on @gregorian', [
'@hijri' => $hijri_formatter->format($value, 'custom', 'l j F Y'),
'@gregorian' => \Drupal::service('date.formatter')->format($value, 'custom', 'F j, Y'),
]);
}
elseif (in_array($format, $list)) {
$explode = explode("_", $format);
return $hijri_formatter->format($value, $explode[1]);
}
elseif ($format == 'hijri_custom') {
$custom_format = $this->options['custom_date_format'];
return $hijri_formatter->format($value, $format, $custom_format);
}
return parent::render($values);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment