You are here

public function AddToCalendarApiWidget::generateWidget in Add To Calendar Button (AddEvent.com) 8.4

Same name and namespace in other branches
  1. 8.3 src/AddToCalendarApiWidget.php \Drupal\addtocalendar\AddToCalendarApiWidget::generateWidget()

Constructs and returns a renderable array widget for add to calendar.

File

src/AddToCalendarApiWidget.php, line 76

Class

AddToCalendarApiWidget
Class AddToCalendarApiWidget.

Namespace

Drupal\addtocalendar

Code

public function generateWidget() {

  // Start building the build array.
  $build['addtocalendar'] = [];
  $display_text = t(':text', array(
    ':text' => $this->atcDisplayText,
  ));
  $build['addtocalendar'] = [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#value' => $display_text
      ->__toString(),
    '#attributes' => [
      'class' => 'addeventatc',
      'title' => 'Add to Calendar',
    ],
  ];
  $timeZone = $this->timeZone;

  // Assuming date and end_date is provide in UTC format.
  $date = DrupalDateTime::createFromTimestamp($this->atcDateStart, 'UTC');
  $end_date = DrupalDateTime::createFromTimestamp($this->atcDateEnd, 'UTC');
  $info = [
    'start' => $date
      ->format('d-m-Y H:i', [
      'timezone' => $timeZone,
    ]),
    'end' => $end_date
      ->format('d-m-Y H:i', [
      'timezone' => $timeZone,
    ]),
    'title' => $this->atcTitle,
    'description' => $this->atcDescription,
    'location' => $this->atcLocation,
    'organizer' => $this->atcOrganizer,
    'organizer_email' => $this->atcOrganizerEmail,
    'timezone' => $timeZone,
  ];
  foreach ($info as $key => $value) {
    $build['addtocalendar'][$key] = [
      '#type' => 'html_tag',
      '#tag' => 'span',
      '#value' => $value,
      '#attributes' => [
        'class' => $key,
      ],
    ];
  }
  $build['#attached']['library'][] = 'addtocalendar/base';
  return $build;
}