DateRecurFieldSimpleRender.php in Recurring Dates Field 8
File
src/Plugin/views/field/DateRecurFieldSimpleRender.php
View source
<?php
namespace Drupal\date_recur\Plugin\views\field;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Form\FormStateInterface;
use Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem;
use Drupal\views\Plugin\views\field\EntityField;
use Drupal\views\ResultRow;
class DateRecurFieldSimpleRender extends EntityField {
protected $display;
protected function defineOptions() {
$options = parent::defineOptions();
$options['hide_alter_empty'] = [
'default' => FALSE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
}
public function query($use_groupby = FALSE) {
$this
->ensureMyTable();
$params = $this->options['group_type'] != 'group' ? [
'function' => $this->options['group_type'],
] : [];
$this->field_alias = $this->query
->addField($this->tableAlias, $this->realField, NULL, $params);
$this
->addAdditionalFields();
}
public function getValue(ResultRow $values, $field = NULL) {
$alias = isset($field) ? $this->aliases[$field] : $this->field_alias;
if (isset($values->{$alias})) {
return $values->{$alias};
}
}
public function render(ResultRow $values) {
$entity = $this
->getEntity($values);
if (empty($this->display)) {
$this->display = EntityViewDisplay::create([
'targetEntityType' => $this
->getEntityType(),
'bundle' => $entity
->bundle(),
'status' => TRUE,
]);
$this->display
->setComponent($this->definition['field_name'], [
'type' => $this->options['type'],
'settings' => $this->options['settings'],
'label' => 'hidden',
]);
}
$build = $this->display
->build($entity);
return $build;
}
public function getEntity(ResultRow $values) {
$entity = parent::getEntity($values);
$field_name = $this->definition['field_name'];
$item = $entity->{$field_name}
->first();
$item->value = $values->{$this->field_alias};
if (!empty($this->aliases[$field_name . '_end_value'])) {
$item->end_value = $values->{$this->aliases[$field_name . '_end_value']};
}
$entity->{$field_name}
->filter(function (DateRecurItem $item) {
if ($item
->getDelta() === 0) {
return TRUE;
}
return FALSE;
});
return $entity;
}
public function allowAdvancedRender() {
return FALSE;
}
}