View source
<?php
namespace Drupal\commerce_reports\Plugin\views\field;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Field\FormatterPluginManager;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\RendererInterface;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\field\EntityField;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ReportDateField extends EntityField {
protected $dateFormatter;
protected $dateFormat;
protected $dateFormatString;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, FormatterPluginManager $formatter_plugin_manager, FieldTypePluginManagerInterface $field_type_plugin_manager, LanguageManagerInterface $language_manager, RendererInterface $renderer, EntityRepositoryInterface $entity_repository, EntityFieldManagerInterface $entity_field_manager, DateFormatterInterface $date_formatter) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $formatter_plugin_manager, $field_type_plugin_manager, $language_manager, $renderer, $entity_repository, $entity_field_manager);
$this->dateFormatter = $date_formatter;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('plugin.manager.field.formatter'), $container
->get('plugin.manager.field.field_type'), $container
->get('language_manager'), $container
->get('renderer'), $container
->get('entity.repository'), $container
->get('entity_field.manager'), $container
->get('date.formatter'));
}
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
if (!empty($this->options['settings']['date_format'])) {
$this->dateFormat = $this->options['settings']['date_format'];
if ($this->dateFormat == 'custom') {
$this->dateFormatString = $this->options['settings']['custom_date_format'];
}
else {
$formatter = $this->entityTypeManager
->getStorage('date_format')
->load($this->dateFormat);
$this->dateFormatString = $formatter
->getPattern();
}
}
}
public function query($use_groupby = FALSE) {
$fields = $this->additional_fields;
$entity_type_key = array_search('entity_type', $fields);
if ($entity_type_key !== FALSE) {
unset($fields[$entity_type_key]);
}
if ($use_groupby) {
$options = [];
if ($this->options['group_column'] != 'entity_id') {
$options = [
$this->options['group_column'] => $this->options['group_column'],
];
}
$options += is_array($this->options['group_columns']) ? $this->options['group_columns'] : [];
$fields = [];
$table_mapping = $this
->getTableMapping();
$field_definition = $this
->getFieldStorageDefinition();
foreach ($options as $column) {
$fields[$column] = $table_mapping
->getFieldColumnName($field_definition, $column);
}
$this->group_fields = $fields;
}
$this
->add_field_table($use_groupby);
$this
->ensureMyTable();
$params = $this->options['group_type'] !== 'group' ? [
'function' => $this->options['group_type'],
] : [];
$expression = $this->query
->getDateFormat("FROM_UNIXTIME({$this->tableAlias}.{$this->realField})", $this->dateFormatString);
$this->field_alias = $this->query
->addField(NULL, $expression, "{$this->tableAlias}_{$this->realField}", $params);
$this->query
->addGroupBy($this->field_alias);
$this->aliases[$this->definition['field_name']] = $this->field_alias;
$this
->getEntityFieldRenderer()
->query($this->query, $this->relationship);
}
public function getItems(ResultRow $values) {
if (!$this->displayHandler
->useGroupBy()) {
$build_list = $this
->getEntityFieldRenderer()
->render($values, $this);
}
else {
$alias = $this->aliases[$this->definition['field_name']];
return [
[
'rendered' => $values->{$alias},
],
];
}
if (!$build_list) {
return [];
}
if ($this->options['field_api_classes']) {
return [
[
'rendered' => $this->renderer
->render($build_list),
],
];
}
$items = [];
$bubbleable = BubbleableMetadata::createFromRenderArray($build_list);
foreach (Element::children($build_list) as $delta) {
BubbleableMetadata::createFromRenderArray($build_list[$delta])
->merge($bubbleable)
->applyTo($build_list[$delta]);
$items[$delta] = [
'rendered' => $build_list[$delta],
'raw' => $build_list['#items'][$delta],
];
}
return $items;
}
}