You are here

protected function AddToCalTypeBase::extractEventDetails in Add to Cal 8

Parameters

\Drupal\Core\Entity\EntityInterface $entity:

array $settings:

int $delta:

Return value

array

3 calls to AddToCalTypeBase::extractEventDetails()
Generic::downloadSubmit in src/Plugin/AddToCal/Type/Generic.php
Google::downloadSubmit in src/Plugin/AddToCal/Type/Google.php
@inheritdoc
Yahoo::downloadSubmit in src/Plugin/AddToCal/Type/Yahoo.php
@inheritdoc

File

src/AddToCalTypeBase.php, line 64

Class

AddToCalTypeBase

Namespace

Drupal\addtocal

Code

protected function extractEventDetails(EntityInterface $entity, array $settings, $delta) {
  $entity_type = $entity
    ->getEntityTypeId();
  $field_name = $settings['field_name'];

  /** @var DateTimeFieldItemList $dates */
  $dates = $entity
    ->get($field_name);

  // Date range field has different structure
  if (!empty($dates[$delta]->start_date) && !empty($dates[$delta]->end_date)) {
    $start_date_object = $dates[$delta]->start_date;
    $end_date_object = $dates[$delta]->end_date;
  }
  elseif (!empty($dates[$delta]->start_date) && empty($dates[$delta]->end_date)) {
    $start_date_object = $end_date_object = $dates[$delta]->start_date;
  }
  else {
    $start_date_object = $end_date_object = $dates[$delta]->date;
  }
  $timezone = !empty($settings['timezone_override']) ? $settings['timezone_override'] : NULL;
  $start_date = $this->dateFormatter
    ->format($start_date_object
    ->getTimestamp(), 'custom', $settings['date_format'], $timezone);
  $end_date = $this->dateFormatter
    ->format($end_date_object
    ->getTimestamp(), 'custom', $settings['date_format'], $timezone);
  if ($settings['location']['value']) {
    $location = $this
      ->extractFieldValue($entity, $settings['location']['value']);
  }
  else {

    // @TODO: Token replace
    $location = $settings['location']['tokenized'];
  }
  if ($settings['description']['value']) {
    $description = $this
      ->extractFieldValue($entity, $settings['description']['value']);
  }
  else {

    // @TODO: Token replace
    $description = $settings['description']['tokenized'];
  }
  if (strlen($description) > 1024) {
    $description = Unicode::truncate($description, 1024, TRUE, TRUE);
  }
  $url = $entity
    ->toUrl()
    ->setAbsolute()
    ->toString();
  $title = $entity
    ->label();
  $overridable_items = [
    'override_status' => false,
    'title' => $title,
    'location' => $location,
    'description' => $description,
  ];

  // Trigger hook_addtocal_alter().
  // Allow modules to override addtocal fields.
  \Drupal::service('module_handler')
    ->alter('addtocal', $overridable_items);

  // If any override was found - set the fields to the override values.
  if ($overridable_items['override_status']) {
    $title = $overridable_items['title'];
    $location = $overridable_items['location'];
    $description = $overridable_items['description'];
  }
  return [
    'title' => $title,
    'start' => $start_date,
    'end' => $end_date,
    'timezone' => $timezone,
    'location' => $location,
    'description' => $description,
    'entity_id' => $entity
      ->id(),
    'entity_type' => $entity_type,
    'url' => $url,
    'rfc3339' => $this
      ->rfc3339Date($start_date_object, $end_date_object, $timezone),
  ];
}