Commit df5ea298 authored by Gaurav Garg's avatar Gaurav Garg Committed by Gaurav Garg
Browse files

Issue #2869575 by gg24, Tim Bozeman: Date range support

parent 6b75dac8
Loading
Loading
Loading
Loading
+3 −3
Changes for addtocalendar.module: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ function addtocalendar_help($route_name, RouteMatchInterface $route_match) {
 */
function addtocalendar_field_formatter_third_party_settings_form($plugin, $field_definition, $view_mode, $form, $form_state) {
  $element = [];
  if (in_array('datetime', $plugin->getPluginDefinition()['field_types'])) {
  if (count(array_intersect(['datetime', 'daterange'], $plugin->getPluginDefinition()['field_types']))) {
    $settings = $plugin->getThirdPartySettings('addtocalendar');
    $element = _addtocalendar_build_form($settings, $field_definition);
  }
@@ -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');
    }
@@ -48,7 +48,7 @@ function addtocalendar_field_formatter_settings_summary_alter(&$summary, $contex
 * Implements hook_preprocess_field().
 */
function addtocalendar_preprocess_field(&$variables) {
  if ($variables['field_type'] == 'datetime') {
  if ($variables['field_type'] == 'datetime' || $variables['field_type'] == 'daterange') {
    _addtocalendar_preprocess_field($variables);
  }
}
+21 −3
Changes for includes/addtocalendar.build.inc: 21 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -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',
@@ -64,7 +67,12 @@ function _addtocalendar_preprocess_field(&$variables) {

          default:
            $field = $settings['addtocalendar_settings'][$value]['field'];
            if (strip_tags($entity->{$field}->getFieldDefinition()->getType()) == 'daterange') {
              $class_value = strip_tags($entity->{$field}->end_value);
            }
            else {
              $class_value = strip_tags($entity->{$field}->value);
            }
            break;
        }
        $build['addtocalendar'][$value] = [
@@ -77,8 +85,17 @@ function _addtocalendar_preprocess_field(&$variables) {
        ];
      }

      // Assign end date the value of start date if no date is present.
      // 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',
@@ -140,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']);
    }
+1 −2
Changes for includes/addtocalendar.form.inc: 1 added line, 2 removed lines.
Original line number Diff line number Diff line
@@ -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,
    ];