View source
<?php
namespace Drupal\addtocalendar\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
class AddToCalendar extends FormatterBase {
public static function defaultSettings() {
return [] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
return $form;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#markup' => $this
->viewValue($item),
];
}
return $elements;
}
protected function viewValue(FieldItemInterface $item) {
if ($item->value == 1) {
$entity = $item
->getEntity();
$settings = $this->fieldDefinition
->getSettings();
$service = \Drupal::service('addtocalendar.apiwidget');
$config_values = [
'atcDisplayText' => $this->fieldDefinition
->getSetting('on_label'),
'atcTitle' => $this
->getProperValue($settings['addtocalendar_settings']['atc_title'], $entity),
'atcDescription' => $this
->getProperValue($settings['addtocalendar_settings']['atc_description'], $entity),
'atcLocation' => $this
->getProperValue($settings['addtocalendar_settings']['atc_location'], $entity),
'atcDateStart' => $this
->getProperValue($settings['addtocalendar_settings']['atc_date_start'], $entity, [
'use_raw_value' => TRUE,
]),
'atcDateEnd' => $this
->getProperValue($settings['addtocalendar_settings']['atc_date_end'], $entity, [
'use_raw_value' => TRUE,
'end_date' => TRUE,
]),
'atcOrganizer' => $this
->getProperValue($settings['addtocalendar_settings']['atc_organizer'], $entity),
'atcOrganizerEmail' => $this
->getProperValue($settings['addtocalendar_settings']['atc_organizer_email'], $entity),
];
if ($settings['addtocalendar_settings']['data_calendars']) {
$data_calendars = array();
foreach ($settings['addtocalendar_settings']['data_calendars'] as $key => $set) {
if ($set) {
$data_calendars[$key] = $key;
}
}
$config_values['atcDataCalendars'] = $data_calendars;
}
$service
->setWidgetValues($config_values);
$build = $service
->generateWidget();
$return = render($build);
}
else {
$return = $this->fieldDefinition
->getSetting('off_label');
}
return $return;
}
public function getProperValue(array $field_setting, $entity, array $options = array()) {
$entity_type = $entity
->getEntityTypeId();
$token_service = \Drupal::token();
$token_options = [
'langcode' => $entity
->language()
->getId(),
'callback' => '',
'clear' => TRUE,
];
switch ($field_setting['field']) {
case 'token':
$value = $field_setting['tokenized'];
$value = $token_service
->replace($value, [
$entity_type => $entity,
], $token_options);
break;
case 'title':
$value = $entity
->getTitle();
break;
default:
$field = $field_setting['field'];
if (isset($options['use_raw_value']) && $options['use_raw_value']) {
$value = strip_tags($entity->{$field}->value);
if (isset($options['end_date']) && strip_tags($entity->{$field}
->getFieldDefinition()
->getType()) == 'daterange') {
$value = strip_tags($entity->{$field}->end_value);
}
}
else {
$value = $entity
->get($field)
->view([
'label' => 'hidden',
]);
$value = strip_tags(render($value));
}
break;
}
return $value;
}
}