diff --git a/addtocalendar.module b/addtocalendar.module index a983a86..88739a4 100644 --- a/addtocalendar.module +++ b/addtocalendar.module @@ -37,7 +37,7 @@ function addtocalendar_field_formatter_third_party_settings_form($plugin, $field * Implements hook_field_formatter_settings_summary_alter(). */ function addtocalendar_field_formatter_settings_summary_alter(&$summary, $context) { - if (in_array('datetime', $context['formatter']->getPluginDefinition()['field_types'])) { + if (count(array_intersect(['datetime', 'daterange'], $context['formatter']->getPluginDefinition()['field_types']))) { if ($context['formatter']->getThirdPartySetting('addtocalendar', 'addtocalendar_show')) { $summary[] = t('Add to calendar enabled'); } diff --git a/includes/addtocalendar.build.inc b/includes/addtocalendar.build.inc index 46313c6..5e962c0 100644 --- a/includes/addtocalendar.build.inc +++ b/includes/addtocalendar.build.inc @@ -33,8 +33,11 @@ function _addtocalendar_preprocess_field(&$variables) { if (empty($values[$delta]['value'])) { return; } - $date = new DrupalDateTime(preg_replace('/T/', ' ', $values[$delta]['value']), 'UTC'); + if (!empty($values[$delta]['end_value']) && isset($values[$delta]['end_value'])) { + $end_date = new DrupalDateTime(preg_replace('/T/', ' ', $values[$delta]['end_value']), 'UTC'); + } + $build['addtocalendar']['atc_date_start'] = [ '#type' => 'html_tag', '#tag' => 'var', @@ -82,8 +85,17 @@ function _addtocalendar_preprocess_field(&$variables) { ]; } - // Assign end date the value of start date if no date is present. - $build['addtocalendar']['atc_date_end']['#value'] = (!empty($build['addtocalendar']['atc_date_end']['#value'])) ? $build['addtocalendar']['atc_date_end']['#value'] : $build['addtocalendar']['atc_date_start']['#value']; + // Assign end date the value of start date if no end date is present and its not a daterange field. + if (empty($end_date)) { + $build['addtocalendar']['atc_date_end']['#value'] = (!empty($build['addtocalendar']['atc_date_end']['#value'])) ? $build['addtocalendar']['atc_date_end']['#value'] : $build['addtocalendar']['atc_date_start']['#value']; + $build['addtocalendar']['atc_date_end']['#value'] = (!empty($build['addtocalendar']['atc_date_end']['#value'])) ? $build['addtocalendar']['atc_date_start']['#value'] : $date->format('Y-m-d H:i:s', ['timezone' => $timeZone]); + } + // Assign end date the value of end date if no end date is present and its a daterange field. + else { + $end_date_val = $end_date->__toString(); + $build['addtocalendar']['atc_date_end']['#value'] = (!empty($build['addtocalendar']['atc_date_end']['#value'])) ? $build['addtocalendar']['atc_date_end']['#value'] : $end_date_val; + $build['addtocalendar']['atc_date_end']['#value'] = $end_date->format('Y-m-d H:i:s', ['timezone' => $timeZone]); + } $build['addtocalendar']['atc_timezone'] = [ '#type' => 'html_tag', @@ -145,10 +157,11 @@ function _addtocalendar_preprocess_field(&$variables) { $variables['#attached']['library'][] = $style['library']; } } + if (isset($variables['items'][0]['content']['#markup'])) { $variables['items'][0]['content']['#markup'] .= render($build['addtocalendar']); } - elseif (isset($variables['items'][0]['content']['#theme'])) { + elseif (isset($variables['items'][0]['content'])) { unset($variables['items'][0]['content']['#theme']); $variables['items'][0]['content']['#markup'] = $variables['items'][0]['content']['#text'] . render($build['addtocalendar']); } diff --git a/includes/addtocalendar.form.inc b/includes/addtocalendar.form.inc index 36d4a8d..0da1d9b 100644 --- a/includes/addtocalendar.form.inc +++ b/includes/addtocalendar.form.inc @@ -85,7 +85,6 @@ function _addtocalendar_build_form($settings, $field_definition) { $fields = Drupal::service('entity_field.manager')->getFieldDefinitions($field_definition->get('entity_type'), $field_definition->get('bundle')); $field_options = []; - foreach ($fields as $id => $field) { if (is_a($field, 'Drupal\field\Entity\FieldConfig')) { $field_options[$id] = $field->getLabel(); @@ -162,7 +161,7 @@ function _addtocalendar_build_form($settings, $field_definition) { '#title' => t('Multiple Value Options'), '#options' => [ 1 => t('Show one of the values'), - // 2 => t('Show multiple AddtoCalendar Buttons'), + // 2 => t('Show multiple AddtoCalendar Buttons'),. ], '#default_value' => !empty($settings['addtocalendar_settings']['multiple_value']) ? $settings['addtocalendar_settings']['multiple_value'] : 1, ];