Version 1
[yaffs-website] / web / modules / contrib / fontyourface / src / Form / FontDisplayForm.php
1 <?php
2
3 namespace Drupal\fontyourface\Form;
4
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\fontyourface\FontDisplayInterface;
8 use Drupal\fontyourface\Entity\Font;
9
10 /**
11  * Class FontDisplayForm.
12  *
13  * @package Drupal\fontyourface\Form
14  */
15 class FontDisplayForm extends EntityForm {
16
17   /**
18    * {@inheritdoc}
19    */
20   public function form(array $form, FormStateInterface $form_state) {
21     $form = parent::form($form, $form_state);
22
23     $font_display = $this->entity;
24     $form['label'] = [
25       '#type' => 'textfield',
26       '#title' => $this->t('Label'),
27       '#maxlength' => 255,
28       '#default_value' => $font_display->label(),
29       '#description' => $this->t("Label for the Font display."),
30       '#required' => TRUE,
31     ];
32
33     $form['id'] = [
34       '#type' => 'machine_name',
35       '#default_value' => $font_display->id(),
36       '#machine_name' => [
37         'exists' => '\Drupal\fontyourface\Entity\FontDisplay::load',
38       ],
39       '#disabled' => !$font_display->isNew(),
40     ];
41
42     /* You will need additional form elements for your custom properties. */
43
44     $fonts = Font::loadEnabledFonts();
45     if (empty($fonts)) {
46       drupal_set_message($this->t('Please select at least one font before picking a font style.'));
47       $form_state->setRedirect('entity.font.collection');
48     }
49     $available_fonts = [];
50     foreach ($fonts as $font) {
51       $available_fonts[$font->url->value] = $font->name->value;
52     }
53
54     $drupal_themes = \Drupal::service('theme_handler')->listInfo();
55     $themes = [];
56     foreach ($drupal_themes as $key => $theme) {
57       if (!empty($theme->info['hidden'])) {
58         continue;
59       }
60       $themes[$key] = $theme->info['name'];
61     }
62
63     $form['font_url'] = [
64       '#type' => 'select',
65       '#title' => $this->t('Font'),
66       '#description' => $this->t('Select the font to use as part of the font style'),
67       '#default_value' => $font_display->getFontUrl(),
68       '#options' => $available_fonts,
69       '#required' => TRUE,
70     ];
71
72     foreach ($fonts as $font) {
73       $element_id = 'font_display_usage_' . $font->Id();
74       $form[$element_id] = [
75         '#type' => 'container',
76         '#states' => [
77           'visible' => [
78             'select[name="font_url"]' => ['value' => $font->url->value],
79           ],
80         ],
81       ];
82       $form[$element_id]['usage'] = [
83         '#type' => 'fieldset',
84         '#collapsible' => FALSE,
85         '#title' => 'Usage',
86       ];
87       $form[$element_id]['usage']['instructions'] = [
88         '#type' => 'item',
89         '#markup' => 'If you wish to skip using the font display and add the css directly to your theme, copy/paste the following for the font into your theme css file:',
90       ];
91       $form[$element_id]['usage']['preview'] = [
92         '#type' => 'html_tag',
93         '#tag' => 'code',
94         '#attributes' => [
95           'style' => 'white-space: pre;',
96         ],
97         '#value' => fontyourface_font_css($font, NULL, "\n"),
98       ];
99     }
100
101     $form['fallback'] = [
102       '#type' => 'textfield',
103       '#title' => $this->t('Fallback fonts'),
104       '#description' => $this->t('Fallback fonts in case selected font fails to load.'),
105       '#default_value' => $font_display->getFallback(),
106     ];
107
108     $preset_selectors = $this->getPresetSelectors();
109     $form['preset_selectors'] = [
110       '#type' => 'select',
111       '#title' => $this->t('Preset Selectors'),
112       '#description' => $this->t('Use preset selectors to easily display your font.'),
113       '#options' => $preset_selectors,
114       '#default_value' => $this->getDefaultSelectorOption($font_display),
115       '#required' => TRUE,
116     ];
117
118     $form['selectors'] = [
119       '#type' => 'textarea',
120       '#title' => $this->t('Selectors'),
121       '#description' => $this->t('Selects the selected font will apply to. Note that all pages will have a "fontyourface" class on the body tag. You can use that to specify a font.'),
122       '#default_value' => $font_display->getSelectors(),
123       '#maxlength' => 300,
124       '#required' => FALSE,
125       '#states' => [
126         'visible' => [
127           'select[name="preset_selectors"]' => ['value' => 'other'],
128         ],
129       ],
130     ];
131
132     $form['theme'] = [
133       '#type' => 'select',
134       '#title' => $this->t('Theme'),
135       '#description' => $this->t('Select theme this display will work for.'),
136       '#default_value' => $font_display->getTheme(),
137       '#options' => $themes,
138       '#required' => TRUE,
139     ];
140
141     return $form;
142   }
143
144   /**
145    * {@inheritdoc}
146    */
147   public function validateForm(array &$form, FormStateInterface $form_state) {
148     parent::validateForm($form, $form_state);
149     $preset_selectors = $form_state->getValue('preset_selectors');
150     $selectors = $form_state->getValue('selectors');
151     if ($preset_selectors == 'other' && empty($selectors)) {
152       $form_state->setError($form['selectors'], $this->t("A selector is required if 'other' preset selector is selected"));
153     }
154   }
155
156   /**
157    * {@inheritdoc}
158    */
159   public function save(array $form, FormStateInterface $form_state) {
160     $font_display = $this->entity;
161     $preset_selectors = $form_state->getValue('preset_selectors');
162     $selectors = $form_state->getValue('selectors');
163     $font_display->setSelectors($preset_selectors);
164     if ($preset_selectors == 'other') {
165       $font_display->setSelectors($selectors);
166     }
167     $status = $font_display->save();
168
169     switch ($status) {
170       case SAVED_NEW:
171         drupal_set_message($this->t('Created the %label Font display.', [
172           '%label' => $font_display->label(),
173         ]));
174         break;
175
176       default:
177         drupal_set_message($this->t('Saved the %label Font display.', [
178           '%label' => $font_display->label(),
179         ]));
180     }
181     fontyourface_save_and_generate_font_display_css($font_display);
182     drupal_flush_all_caches();
183     $form_state->setRedirectUrl($font_display->urlInfo('collection'));
184   }
185
186   /**
187    * Get a list of preset selectors.
188    *
189    * @return array
190    *   List of key-value selectors for selecting css selector presets.
191    */
192   private function getPresetSelectors() {
193     return [
194       '' => '-- Select --',
195       '.fontyourface h1, .fontyourface h2, .fontyourface h3, .fontyourface h4, .fontyourface h5, .fontyourface h6' => 'All Headers (h1, h2, h3, h4, h5, h6)',
196       '.fontyourface h1' => 'h1',
197       '.fontyourface h2' => 'h2',
198       '.fontyourface h3' => 'h3',
199       '.fontyourface p, .fontyourface div' => 'standard text (p, div)',
200       '.fontyourface' => 'everything',
201       'other' => 'other',
202     ];
203   }
204
205   /**
206    * Return string that maps to selector.
207    *
208    * @param \Drupal\fontyourface\FontDisplayInterface $font_display
209    *   Current Font Display entity.
210    *
211    * @return string
212    *   String that maps to preset selector. 'Other' or empty string otherwise.
213    */
214   private function getDefaultSelectorOption(FontDisplayInterface $font_display) {
215     $preset_selectors = $this->getPresetSelectors();
216     $font_selector = $font_display->getSelectors();
217     if (!empty($preset_selectors[$font_selector])) {
218       return $font_selector;
219     }
220     elseif (!empty($font_selector)) {
221       return 'other';
222     }
223     return '';
224   }
225
226 }