You are here

protected function ViewsIcalHelper::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.

Return value

\Eluceo\iCal\Component\Event A new event.

See also

\Drupal\views_ical\Plugin\views\style\Ical::defineOptions

3 calls to ViewsIcalHelper::createDefaultEvent()
ViewsIcalHelper::addDateRangeEvent in src/ViewsIcalHelper.php
Create an event based on a daterange field.
ViewsIcalHelper::addDateRecurEvent in src/ViewsIcalHelper.php
Adds an event.
ViewsIcalHelper::addDateTimeEvent in src/ViewsIcalHelper.php
Create an event based on a datetime field

File

src/ViewsIcalHelper.php, line 47

Class

ViewsIcalHelper
Helper methods for views_ical.

Namespace

Drupal\views_ical

Code

protected function createDefaultEvent(ContentEntityInterface $entity, array $field_mapping) : 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)) {

          // This is where we really need to move this to a proper row handler.
          // for a date/created field, render according to the views definition.
          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']) && $entity
    ->hasField($field_mapping['summary_field'])) {
    if ($field_mapping['summary_field'] == 'body' && !$entity
      ->get('body')
      ->isEmpty()) {
      $summary = $entity
        ->get('body')
        ->getValue()[0]['value'];
    }
    else {
      $summary = $entity
        ->get($field_mapping['summary_field'])
        ->getString();
    }
    if ($summary) {
      $event
        ->setSummary($summary);
    }
  }

  // Rrule field.
  if (isset($field_mapping['rrule_field']) && $entity
    ->hasField($field_mapping['rrule_field'])) {
    $rrule = $entity
      ->get($field_mapping['rrule_field'])
      ->getString();
    if ($rrule) {
      $event
        ->setRrule($rrule);
    }
  }

  // Location field
  if (isset($field_mapping['location_field']) && $entity
    ->hasField($field_mapping['location_field'])) {
    if ($field_mapping['location_field'] == 'body' && !$entity
      ->get('body')
      ->isEmpty()) {
      $location = $entity
        ->get('body')
        ->getValue()[0]['value'];
      $event
        ->setLocation($location);
    }
    else {
      $location = $entity->{$field_mapping['location_field']}
        ->first();
      $event
        ->setLocation($location
        ->getValue()['value']);
    }
  }

  // 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->{$field_mapping['url_field']}
        ->first();
      $event
        ->setUrl($url
        ->getValue()['value']);
    }
  }

  // Description field
  if (isset($field_mapping['description_field']) && $entity
    ->hasField($field_mapping['description_field'])) {
    if ($field_mapping['location_field'] == 'body') {

      /** @var \Drupal\Core\Field\FieldItemInterface $description */
      $description = $entity->{$field_mapping['description_field']}
        ->getValue()[0]['value'];
      $event
        ->setDescription($description);
    }
    else {

      /** @var \Drupal\Core\Field\FieldItemInterface $description */
      $description = $entity->{$field_mapping['description_field']}
        ->first();
      $event
        ->setDescription(\strip_tags($description
        ->getValue()['value']));
    }
  }
  $event
    ->setUseTimezone(TRUE);
  return $event;
}