Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 482 fix event display #350

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 0 additions & 65 deletions modules/wri_event/src/Plugin/DsField/CalendarEvent.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Drupal\wri_event\Plugin\Field\FieldFormatter;

use Drupal\addtocal\Plugin\Field\FieldFormatter\AddtocalView;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\FieldItemListInterface;

/**
* Plugin implementation of the 'wri_event_add_to_cal' formatter.
*
* @FieldFormatter(
* id = "wri_event_wri_event_add_to_cal",
* label = @Translation("wri_event_add_to_cal"),
* field_types = {"addtocal"},
* )
*/
final class WriEventAddToCalFormatter extends AddtocalView {

/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$entity = $items->getEntity();
$field = $this->fieldDefinition;
$token_data = [
$field->getTargetEntityTypeId() => $entity,
];
$timezone_override = $this->getSetting('timezone_override') ?: NULL;
$element = [];
foreach ($items as $item) {
$info = [];

$start = '';
if (isset($item->value)) {
$start = $item->value;
$start_date = DrupalDateTime::createFromTimestamp($start);
}

$end = '';
if (isset($item->end_value)) {
$end = $item->end_value;
$end_date = DrupalDateTime::createFromTimestamp($end);
}

$today = DrupalDateTime::createFromTimestamp(time());

// If this event is in the future build button.
if (!$today->diff($end_date)->invert) {
$title = $this->token->replace($this->getSetting('event_title'), $token_data, ['clear' => TRUE]) ?: $entity->label();
$description = $this->token->replace($this->getSetting('description'), $token_data, ['clear' => TRUE]);
$timezone = $item->timezone ? $item->timezone : $timezone_override;
$location = '';
if (isset($entity->field_location->value)) {
$location = $entity->field_location->value;
}
$button_title = $this->t('Add to Calendar');
$info['#markup'] = '<div class="event-subscribe-wrapper"><a href="#" title="' . $button_title . '" class="addeventatc">' . $button_title;
$info['#markup'] .= '<span class="_start">' . $start_date->format('m/d/Y g:i a') . '</span>';
$info['#markup'] .= '<span class="_end">' . $end_date->format('m/d/Y g:i a') . '</span>';
$info['#markup'] .= '<span class="_zonecode">' . $timezone . '</span>';
$info['#markup'] .= '<span class="_location">' . $location . '</span>';
$info['#markup'] .= '<span class="_summary">' . $title . '</span>';
$info['#markup'] .= '<span class="_description">' . $description . '</span>';
$info['#markup'] .= '</a></div>';
$info['#attached']['library'][] = 'wri_event/event';

$element[] = $info;
}
}

return $element;
}

}
66 changes: 66 additions & 0 deletions modules/wri_event/src/Plugin/Field/FieldType/CopyToCalItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Drupal\wri_event\Plugin\Field\FieldType;

use Drupal\smart_date\Plugin\Field\FieldType\SmartDateItem;

/**
* Defines the 'wri_event_copy_to_cal' field type.
*
* @FieldType(
* id = "addtocal",
* label = @Translation("Add To Calendar button"),
* description = @Translation("Copies the value in the Date field."),
* default_formatter = "wri_event_wri_event_add_to_cal",
* no_ui = TRUE,
* )
*/
final class CopyToCalItem extends SmartDateItem {

/**
* Whether the value has been calculated.
*
* @var bool
*/
protected $isCalculated = FALSE;

/**
* {@inheritdoc}
*/
public function __get($name) {
$this->ensureCalculated();
return parent::__get($name);
}

/**
* {@inheritdoc}
*/
public function isEmpty() {
$this->ensureCalculated();
return parent::isEmpty();
}

/**
* {@inheritdoc}
*/
public function getValue() {
$this->ensureCalculated();
return parent::getValue();
}

/**
* Calculates the value of the field and sets it.
*/
protected function ensureCalculated() {
if (!$this->isCalculated) {
$entity = $this->getEntity();
if (isset($entity->field_date_time)) {
$this->parent->setValue($entity->field_date_time->getValue());
}
$this->isCalculated = TRUE;
}
}

}
22 changes: 22 additions & 0 deletions modules/wri_event/wri_event.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;

/**
* Implements hook_views_query_alter().
Expand All @@ -19,3 +21,23 @@ function wri_event_views_query_alter(ViewExecutable $view, QueryPluginBase $quer
$sort['field_date_time'] = 'DESC';
}
}

/**
* Implements hook_entity_base_field_info().
*/
function wri_event_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
if ($entity_type->id() === 'node') {
// Add a field that shows the parent of the term, or itself if no parent.
$fields['addtocal'] = BaseFieldDefinition::create('addtocal')
->setName('addtocal')
->setLabel(t('Calendar item'))
->setSetting('target_type', 'node')
->setComputed(TRUE)
->setClass('\Drupal\wri_search\WriCalculatedTexts')
->setDisplayConfigurable('view', TRUE)
->setCardinality(1);

return $fields;
}
}