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