protected function IcalFieldsWizard::createDefaultEvent in Views iCal 8
Creates an event with default data.
Event summary, location and description are set as defaults.
Parameters
\Drupal\Core\Entity\ContentEntityInterface $entity: The entity to be used for default data.
array $field_mapping: Views field option and entity field name mapping. Example: [ 'date_field' => 'field_event_date', 'summary_field' => 'field_event_summary', 'description_field' => 'field_event_description', ] End of example. @param \Drupal\views\ResultRow $row The values retrieved from a single row of a view's query result.
Return value
\Eluceo\iCal\Component\Event A new event.
See also
\Drupal\views_ical\Plugin\views\style\Ical::defineOptions
4 calls to IcalFieldsWizard::createDefaultEvent()
- IcalFieldsWizard::addDateRangeEvent in src/
Plugin/ views/ row/ IcalFieldsWizard.php - Create an event based on a daterange field.
- IcalFieldsWizard::addDateRecurEvent in src/
Plugin/ views/ row/ IcalFieldsWizard.php - IcalFieldsWizard::addDateTimeEvent in src/
Plugin/ views/ row/ IcalFieldsWizard.php - Create an event based on a datetime field
- IcalFieldsWizard::addSmartDateEvent in src/
Plugin/ views/ row/ IcalFieldsWizard.php - Create an event based on a smartdate field.
File
- src/
Plugin/ views/ row/ IcalFieldsWizard.php, line 158 - Contains \Drupal\views_ical\Plugin\views\row\Fields.
Class
- IcalFieldsWizard
- The 'Ical Fields' row plugin
Namespace
Drupal\views_ical\Plugin\views\rowCode
protected function createDefaultEvent(ContentEntityInterface $entity, array $field_mapping, ResultRow $row) : Event {
if (isset($field_mapping['uid_field']) && ($field_mapping['uid_field'] == 'nid' || $field_mapping['uid_field'] == 'nothing')) {
// If the Uid field is the nid, access with the id method.
$uid = $entity
->id();
if (isset($this->view->field[$field_mapping['uid_field']]->options['alter']['alter_text']) && $this->view->field[$field_mapping['uid_field']]->options['alter']['alter_text']) {
// I need rewrite of the UID field to happen here.
// This is really hacky, It would be really nice to find a way to render as the row.
$alter_text = $this->view->field[$field_mapping['uid_field']]->options['alter']['text'];
$fields = array_keys($this->view->field);
foreach ($fields as $field) {
if ($entity
->hasField($field)) {
if ($entity
->get($field)
->getDataDefinition()
->getType() == 'created') {
$settings = $this->view->field['created']->options['settings'];
[
'custom_date_format',
];
if ($settings['date_format'] == 'custom') {
$field_value = \Drupal::service('date.formatter')
->format($entity
->get($field)
->getString(), 'custom', $settings['custom_date_format']);
}
else {
$field_value = \Drupal::service('date.formatter')
->format($entity
->get($field)
->getString(), $settings['date_format']);
}
}
else {
$field_value = $entity
->get($field)
->getString();
}
$alter_text = str_replace("{{ {$field} }}", $field_value, $alter_text);
}
}
$uid = $alter_text;
}
}
else {
if (isset($field_mapping['uid_field']) && $field_mapping['uid_field'] != 'none' && $entity
->hasField($field_mapping['uid_field']) && !$entity
->get($field_mapping['uid_field'])
->isEmpty()) {
$uid = $entity
->get($field_mapping['uid_field'])
->getString();
}
else {
$uid = null;
}
}
$event = new Event($uid);
// Summary field.
if (isset($field_mapping['summary_field']) && isset($this->view->field[$field_mapping['summary_field']])) {
// TODO: We repeat something similar to this 3 times. I like my code like I like my wine, DRY.
$renderer = $this
->getRenderer();
$summaryField = $entity->{$field_mapping['summary_field']}
->view();
if ($renderer
->hasRenderContext()) {
$html = $renderer
->render($summaryField);
}
else {
$html = $renderer
->renderPlain($summaryField);
}
$html = new Html2Text((string) $html);
$event
->setSummary($html
->getText());
}
// URL field
if (isset($field_mapping['url_field']) && $entity
->hasField($field_mapping['url_field'])) {
if ($field_mapping['url_field'] == 'body' && !$entity
->get('body')
->isEmpty()) {
$url = $entity
->get('body')
->getValue()[0]['value'];
$event
->setUrl($url);
}
else {
$url = $entity
->get($field_mapping['url_field'])
->getValue()[0]['uri'];
$event
->setUrl($url);
}
}
// Rrule field.
if (isset($field_mapping['rrule_field'])) {
if ($field_mapping['rrule_field'] == 'body') {
$rrule = $entity
->get('body')
->getValue()[0]['value'];
$event
->addRecurrenceRule($rrule);
}
else {
$helper = $entity->field_recur[0]
->getHelper();
$rrules = $helper
->getRules();
$exdates = $helper
->getExcluded();
// Parse EXDATEs.
if ($exdates) {
foreach ($exdates as $exdate) {
if ($exdate) {
$event
->addExDate($exdate);
}
}
}
// Calculate 2 years from today for limiting infinitely recurring dates.
$two_years_out = date('c', strtotime('+2 years'));
// Parse rrules into usable bits.
foreach ($rrules as $key => $rule) {
$parts = $rule
->getParts() ?? '';
$frequency = $parts['FREQ'] ?? '';
$byday = $parts['BYDAY'] ?? '';
$until = $parts['UNTIL'] ?? date_create($two_years_out);
$count = $parts['COUNT'] ?? '';
$interval = $parts['INTERVAL'] ?? '';
// Set recurrence rule
$recurrenceRule = new RecurrenceRule();
if ($frequency) {
$recurrenceRule
->setFreq($frequency);
}
if ($byday) {
$recurrenceRule
->setByDay($byday);
}
if ($until) {
$recurrenceRule
->setUntil($until);
}
if ($count) {
$recurrenceRule
->setCount($count);
}
if ($interval) {
$recurrenceRule
->setInterval($interval);
}
$event
->addRecurrenceRule($recurrenceRule);
}
}
}
// URL field
if (isset($field_mapping['url_field']) && $entity
->hasField($field_mapping['url_field'])) {
if ($field_mapping['url_field'] == 'body' && !$entity
->get('body')
->isEmpty()) {
$url = $entity
->get('body')
->getValue()[0]['value'];
$event
->setUrl($url);
}
else {
$url = $entity
->get($field_mapping['url_field'])
->getValue()[0]['uri'] ?? '';
$event
->setUrl($url);
}
}
// Location field
if (isset($field_mapping['location_field']) && isset($this->view->field[$field_mapping['location_field']])) {
$locationField = $entity->{$field_mapping['location_field']}
->view();
if ($renderer
->hasRenderContext()) {
$html = $renderer
->render($locationField);
}
else {
$html = $renderer
->renderPlain($locationField);
}
$html = new Html2Text((string) $html);
$event
->setLocation($html
->getText());
}
// Description field
if (isset($field_mapping['description_field']) && isset($this->view->field[$field_mapping['description_field']])) {
$descriptionField = $entity->{$field_mapping['description_field']}
->view();
if ($renderer
->hasRenderContext()) {
$html = $renderer
->render($descriptionField);
}
else {
$html = $renderer
->renderPlain($descriptionField);
}
$html = new Html2Text((string) $html);
$event
->setDescription($html
->getText());
}
// Transparency - This isn't a real field, but a default setting applied to all events.
if (isset($field_mapping['default_transparency']) && $field_mapping['default_transparency']) {
if ($field_mapping['default_transparency'] == Event::TIME_TRANSPARENCY_OPAQUE) {
$event
->setTimeTransparency(Event::TIME_TRANSPARENCY_OPAQUE);
}
else {
$event
->setTimeTransparency(Event::TIME_TRANSPARENCY_TRANSPARENT);
}
}
$event
->setUseTimezone(TRUE);
return $event;
}