addtocalendar.form.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * @file
  4. * Contains form for addtocalendar module.
  5. */
  6. /**
  7. * Creates Settings form.
  8. *
  9. * @param $settings
  10. * Third party Settings array.
  11. * @param $field_definition
  12. * Field Definition of current field.
  13. *
  14. * @return array
  15. */
  16. function _addtocalendar_build_form($settings, $field_definition) {
  17. $element = [];
  18. $element['addtocalendar_show'] = [
  19. '#type' => 'checkbox',
  20. '#title' => t('Show Add to Calendar'),
  21. '#default_value' => !empty($settings['addtocalendar_show']) ? $settings['addtocalendar_show'] : 0,
  22. ];
  23. $element['addtocalendar_settings'] = [
  24. '#type' => 'fieldset',
  25. '#title' => t('Add to Calendar Settings'),
  26. '#states' => [
  27. 'visible' => [
  28. 'input[name*="addtocalendar_show"]' => ['checked' => TRUE],
  29. ],
  30. ],
  31. ];
  32. $element['addtocalendar_settings']['style'] = [
  33. '#type' => 'select',
  34. '#title' => t('Select Style'),
  35. '#options' => [
  36. 0 => t('No Styling'),
  37. 'blue' => t('Blue'),
  38. 'glow_orange' => t('Glow Orange'),
  39. ],
  40. '#default_value' => !empty($settings['addtocalendar_settings']['style']) ? $settings['addtocalendar_settings']['style'] : 'blue',
  41. ];
  42. $element['addtocalendar_settings']['display_text'] = [
  43. '#type' => 'textfield',
  44. '#title' => t('Display Text'),
  45. '#default_value' => !empty($settings['addtocalendar_settings']['display_text']) ? $settings['addtocalendar_settings']['display_text'] : t('Add to Calender'),
  46. ];
  47. // Fetching site name and site email id.
  48. $config = \Drupal::config('system.site');
  49. $site_name = $config->get('name');
  50. $site_mail = $config->get('mail');
  51. $info = [
  52. 'atc_title' => [
  53. 'label' => t('Title'),
  54. 'sample_data' => 'Title',
  55. ],
  56. 'atc_description' => [
  57. 'label' => t('Description'),
  58. 'sample_data' => 'Description',
  59. ],
  60. 'atc_location' => [
  61. 'label' => t('Location'),
  62. 'sample_data' => 'Location',
  63. ],
  64. 'atc_organizer' => [
  65. 'label' => t('Organizer'),
  66. 'sample_data' => $site_name,
  67. ],
  68. 'atc_organizer_email' => [
  69. 'label' => t('Organizer'),
  70. 'sample_data' => $site_mail,
  71. ],
  72. 'atc_date_end' => [
  73. 'label' => t('End Date'),
  74. 'sample_data' => '',
  75. ],
  76. ];
  77. $fields = Drupal::service('entity_field.manager')->getFieldDefinitions($field_definition->get('entity_type'), $field_definition->get('bundle'));
  78. $field_options = [];
  79. foreach ($fields as $id => $field) {
  80. if (is_a($field, 'Drupal\field\Entity\FieldConfig')) {
  81. $field_options[$id] = $field->getLabel();
  82. }
  83. }
  84. $field_options = array_merge(
  85. ['token' => t('Use Token/Static Content')],
  86. ['title' => t('Title')],
  87. $field_options
  88. );
  89. if (\Drupal::moduleHandler()->moduleExists('token')) {
  90. $element['addtocalendar_settings']['token_help'] = [
  91. '#type' => 'markup',
  92. '#token_types' => array('node'),
  93. '#theme' => 'token_tree_link',
  94. ];
  95. }
  96. foreach ($info as $id => $data) {
  97. $element['addtocalendar_settings'][$id]['field'] = [
  98. '#type' => 'select',
  99. '#title' => $data['label'] . t(': field'),
  100. '#options' => $field_options,
  101. '#description' => t('Select field to be used as @label for calendar events', array('@label' => $data['label'])),
  102. '#default_value' => !empty($settings['addtocalendar_settings'][$id]['field']) ? $settings['addtocalendar_settings'][$id]['field'] : '',
  103. ];
  104. $element['addtocalendar_settings'][$id]['tokenized'] = [
  105. '#type' => 'textfield',
  106. '#title' => $data['label'] . t(': Static/Tokenized Content'),
  107. '#default_value' => !empty($settings['addtocalendar_settings'][$id]['tokenized']) ? $settings['addtocalendar_settings'][$id]['tokenized'] : $data['sample_data'],
  108. ];
  109. }
  110. $element['addtocalendar_settings']['atc_privacy'] = [
  111. '#type' => 'select',
  112. '#title' => t('Privacy'),
  113. '#options' => [
  114. 'public' => t('Public'),
  115. 'private' => t('Private'),
  116. ],
  117. '#description' => t('Use public for free access to event information from any places. User private if the event is closed to public access.'),
  118. '#default_value' => !empty($settings['addtocalendar_settings']['atc_privacy']) ? $settings['addtocalendar_settings']['atc_privacy'] : '',
  119. ];
  120. $element['addtocalendar_settings']['data_secure'] = [
  121. '#type' => 'select',
  122. '#title' => t('Security level'),
  123. '#options' => [
  124. 'auto' => t('Auto'),
  125. 'true' => t('Use https only'),
  126. 'false' => t('Use http only'),
  127. ],
  128. '#default_value' => !empty($settings['addtocalendar_settings']['data_secure']) ? $settings['addtocalendar_settings']['data_secure'] : '',
  129. ];
  130. $element['addtocalendar_settings']['data_calendars'] = [
  131. '#type' => 'checkboxes',
  132. '#options' => [
  133. 'iCalendar' => t('iCalendar'),
  134. 'Google Calendar' => t('Google Calendar'),
  135. 'Outlook' => t('Outlook'),
  136. 'Outlook Online' => t('Outlook Online'),
  137. 'Yahoo! Calendar' => t('Yahoo! Calendar'),
  138. ],
  139. '#default_value' => !empty($settings['addtocalendar_settings']['data_calendars']) ? $settings['addtocalendar_settings']['data_calendars'] : '',
  140. '#title' => t('List of calendars to show in button list.'),
  141. ];
  142. $cardinality = $field_definition->getFieldStorageDefinition()->getCardinality();
  143. if ($cardinality != 1) {
  144. $element['addtocalendar_settings']['multiple_value'] = [
  145. '#type' => 'select',
  146. '#title' => t('AddToCalendar button visibility'),
  147. '#options' => [
  148. 1 => t('Single'),
  149. 2 => t('All'),
  150. ],
  151. '#default_value' => !empty($settings['addtocalendar_settings']['multiple_value']) ? $settings['addtocalendar_settings']['multiple_value'] : 1,
  152. ];
  153. $element['addtocalendar_settings']['delta'] = [
  154. '#type' => 'textfield',
  155. '#title' => t('Delta'),
  156. '#description' => 'AddToCalendar button visibility for a particular date field (default starts with 0).',
  157. '#states' => [
  158. 'visible' => [
  159. ':input[name="fields[' . $field_definition->get('field_name') . '][settings_edit_form][third_party_settings][addtocalendar][addtocalendar_settings][multiple_value]"]' => [
  160. 'value' => 1,
  161. ],
  162. ],
  163. ],
  164. '#default_value' => !empty($settings['addtocalendar_settings']['delta']) ? $settings['addtocalendar_settings']['delta'] : 0,
  165. ];
  166. }
  167. return $element;
  168. }