addtocalendar.build.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * @file
  4. * Contains preprocess helper function.
  5. */
  6. use \Drupal\Core\Entity\Entity\EntityViewDisplay;
  7. use \Drupal\Core\Datetime\DrupalDateTime;
  8. /**
  9. * Helper function to preprocess datetime field.
  10. */
  11. function _addtocalendar_preprocess_field(&$variables) {
  12. $entity = $variables['element']['#object'];
  13. $view_mode = $variables['element']['#view_mode'];
  14. $field_name = $variables['element']['#field_name'];
  15. // Get the field formatter settings...
  16. $entity_display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
  17. $field_display = $entity_display->getComponent($field_name);
  18. if (!empty($field_display['third_party_settings']['addtocalendar'])) {
  19. $build['addtocalendar'] = [];
  20. $settings = $field_display['third_party_settings']['addtocalendar'];
  21. $style = [];
  22. if ($settings['addtocalendar_show']) {
  23. $token_service = \Drupal::token();
  24. $token_options = ['langcode' => $entity->language()->getId(), 'callback' => '', 'clear' => TRUE];
  25. $timeZone = date_default_timezone_get();
  26. $values = $entity->{$field_name}->getValue();
  27. $delta = !empty($settings['addtocalendar_settings']['delta']) ? $settings['addtocalendar_settings']['delta'] : 0;
  28. if (empty($values[$delta]['value'])) {
  29. return;
  30. }
  31. // Unsetting values other than delta if single value is selected.
  32. if ($settings['addtocalendar_settings']['multiple_value'] == 1) {
  33. $temp = $values[$delta];
  34. unset($values);
  35. $values = array($delta => $temp);
  36. unset($temp);
  37. }
  38. foreach ($values as $index => $date_val) {
  39. $date = new DrupalDateTime(preg_replace('/T/', ' ', $values[$index]['value']), 'UTC');
  40. if (!empty($values[$index]['end_value']) && isset($values[$index]['end_value'])) {
  41. $end_date = new DrupalDateTime(preg_replace('/T/', ' ', $values[$index]['end_value']), 'UTC');
  42. }
  43. $build['addtocalendar'][$index]['atc_date_start'] = [
  44. '#type' => 'html_tag',
  45. '#tag' => 'var',
  46. '#value' => $date->format('Y-m-d H:i:s', ['timezone' => $timeZone]),
  47. '#attributes' => [
  48. 'class' => 'atc_date_start',
  49. ],
  50. ];
  51. $info = [
  52. 'atc_date_end',
  53. 'atc_title',
  54. 'atc_description',
  55. 'atc_location',
  56. 'atc_organizer',
  57. 'atc_organizer_email',
  58. ];
  59. foreach ($info as $value) {
  60. switch ($settings['addtocalendar_settings'][$value]['field']) {
  61. case 'token':
  62. $class_value = $settings['addtocalendar_settings'][$value]['tokenized'];
  63. $class_value = $token_service->replace($class_value, array('node' => $entity), $token_options);
  64. break;
  65. case 'title':
  66. $class_value = $entity->getTitle();
  67. break;
  68. default:
  69. $field = $settings['addtocalendar_settings'][$value]['field'];
  70. if (strip_tags($entity->{$field}->getFieldDefinition()
  71. ->getType()) == 'daterange'
  72. ) {
  73. $class_value = strip_tags($entity->{$field}->end_value);
  74. }
  75. else {
  76. $class_value = strip_tags($entity->{$field}->value);
  77. }
  78. break;
  79. }
  80. $build['addtocalendar'][$index][$value] = [
  81. '#type' => 'html_tag',
  82. '#tag' => 'var',
  83. '#value' => $class_value,
  84. '#attributes' => [
  85. 'class' => $value,
  86. ],
  87. ];
  88. }
  89. // Assign end date the value of start date if no end date is present and its not a daterange field.
  90. if (empty($end_date)) {
  91. $build['addtocalendar'][$index]['atc_date_end']['#value'] = (!empty($build['addtocalendar'][$index]['atc_date_end']['#value'])) ? $build['addtocalendar'][$index]['atc_date_end']['#value'] : $build['addtocalendar'][$index]['atc_date_start']['#value'];
  92. $build['addtocalendar'][$index]['atc_date_end']['#value'] = (!empty($build['addtocalendar'][$index]['atc_date_end']['#value'])) ? $build['addtocalendar'][$index]['atc_date_start']['#value'] : $date->format('Y-m-d H:i:s', ['timezone' => $timeZone]);
  93. }
  94. // Assign end date the value of end date if no end date is present and its a daterange field.
  95. else {
  96. $end_date_val = $end_date->__toString();
  97. $build['addtocalendar'][$index]['atc_date_end']['#value'] = (!empty($build['addtocalendar'][$index]['atc_date_end']['#value'])) ? $build['addtocalendar'][$index]['atc_date_end']['#value'] : $end_date_val;
  98. $build['addtocalendar'][$index]['atc_date_end']['#value'] = $end_date->format('Y-m-d H:i:s', ['timezone' => $timeZone]);
  99. }
  100. $build['addtocalendar'][$index]['atc_timezone'] = [
  101. '#type' => 'html_tag',
  102. '#tag' => 'var',
  103. '#value' => $timeZone,
  104. '#attributes' => [
  105. 'class' => 'atc_timezone',
  106. ],
  107. ];
  108. $build['addtocalendar'][$index]['atc_privacy'] = [
  109. '#type' => 'html_tag',
  110. '#tag' => 'var',
  111. '#value' => $settings['addtocalendar_settings']['atc_privacy'],
  112. '#attributes' => [
  113. 'class' => 'atc_privacy',
  114. ],
  115. ];
  116. $build['addtocalendar'][$index] = [
  117. '#type' => 'html_tag',
  118. '#tag' => 'span',
  119. '#value' => '<var class="atc_event">' . render($build['addtocalendar'][$index]) . '</var>',
  120. '#attributes' => [
  121. 'class' => [
  122. 'addtocalendar',
  123. ],
  124. ],
  125. ];
  126. if ($settings['addtocalendar_settings']['data_calendars']) {
  127. $value = '';
  128. foreach ($settings['addtocalendar_settings']['data_calendars'] as $key => $set) {
  129. if ($set) {
  130. $value .= $key . ', ';
  131. }
  132. }
  133. if ($value) {
  134. $build['addtocalendar'][$index]['#attributes']['data-calendars'] = $value;
  135. }
  136. }
  137. $build['addtocalendar'][$index]['#attributes']['data-secure'] = $settings['addtocalendar_settings']['data_secure'];
  138. // Styling.
  139. switch ($settings['addtocalendar_settings']['style']) {
  140. case 'blue':
  141. $style['class'] = 'atc-style-blue';
  142. $style['library'] = 'addtocalendar/blue';
  143. break;
  144. case 'glow_orange':
  145. $style['class'] = 'atc-style-glow-orange';
  146. $style['library'] = 'addtocalendar/glow_orange';
  147. break;
  148. }
  149. if (!empty($style)) {
  150. $build['addtocalendar'][$index]['#attributes']['class'][] = $style['class'];
  151. $variables['#attached']['library'][] = $style['library'];
  152. }
  153. }
  154. }
  155. // Setting #markup for all the date or daterange fields if multivalue is selected.
  156. if ($settings['addtocalendar_settings']['multiple_value'] == 2) {
  157. foreach ($variables['items'] as $k => $content) {
  158. if (isset($variables['items'][$k]['content']['#markup'])) {
  159. $variables['items'][$k]['content']['#markup'] .= render($build['addtocalendar'][$k]);
  160. }
  161. elseif (isset($variables['items'][$k]['content'])) {
  162. unset($variables['items'][$k]['content']['#theme']);
  163. $variables['items'][$k]['content']['#markup'] = $variables['items'][$k]['content']['#text'] . render($build['addtocalendar'][$k]);
  164. }
  165. $variables['#attached']['library'][] = 'addtocalendar/base';
  166. }
  167. }
  168. // Setting #markup for all the date or daterange fields if single value is selected.
  169. else {
  170. if (isset($variables['items'][$delta]['content']['#markup'])) {
  171. $variables['items'][$delta]['content']['#markup'] .= render($build['addtocalendar']);
  172. }
  173. elseif (isset($variables['items'][$delta]['content'])) {
  174. unset($variables['items'][$delta]['content']['#theme']);
  175. $variables['items'][$delta]['content']['#markup'] = $variables['items'][$delta]['content']['#text'] . render($build['addtocalendar']);
  176. }
  177. $variables['#attached']['library'][] = 'addtocalendar/base';
  178. }
  179. }
  180. }