3 namespace Drupal\fontyourface\Form;
6 use Drupal\Core\Entity\EntityForm;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\fontyourface\FontDisplayInterface;
9 use Drupal\fontyourface\Entity\Font;
12 * Class FontDisplayForm.
14 * @package Drupal\fontyourface\Form
16 class FontDisplayForm extends EntityForm {
21 public function form(array $form, FormStateInterface $form_state) {
22 $form = parent::form($form, $form_state);
24 $font_display = $this->entity;
26 '#type' => 'textfield',
27 '#title' => $this->t('Label'),
29 '#default_value' => $font_display->label(),
30 '#description' => $this->t("Label for the Font display."),
35 '#type' => 'machine_name',
36 '#default_value' => $font_display->id(),
38 'exists' => '\Drupal\fontyourface\Entity\FontDisplay::load',
40 '#disabled' => !$font_display->isNew(),
43 /* You will need additional form elements for your custom properties. */
45 $fonts = Font::loadActivatedFonts();
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();
52 $available_fonts = [];
53 foreach ($fonts as $font) {
54 $available_fonts[$font->url->value] = $font->name->value;
57 $drupal_themes = \Drupal::service('theme_handler')->listInfo();
59 foreach ($drupal_themes as $key => $theme) {
60 if (!empty($theme->info['hidden'])) {
63 $themes[$key] = $theme->info['name'];
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,
75 foreach ($fonts as $font) {
76 $element_id = 'font_display_usage_' . $font->Id();
77 $form[$element_id] = [
78 '#type' => 'container',
81 'select[name="font_url"]' => ['value' => $font->url->value],
85 $form[$element_id]['usage'] = [
86 '#type' => 'fieldset',
87 '#collapsible' => FALSE,
90 $form[$element_id]['usage']['instructions'] = [
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:',
94 $form[$element_id]['usage']['preview'] = [
95 '#type' => 'html_tag',
98 'style' => 'white-space: pre;',
100 '#value' => fontyourface_font_css($font, NULL, "\n"),
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(),
111 $preset_selectors = $this->getPresetSelectors();
112 $form['preset_selectors'] = [
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),
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(),
127 '#required' => FALSE,
130 'select[name="preset_selectors"]' => ['value' => 'other'],
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,
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"));
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);
170 $status = $font_display->save();
174 drupal_set_message($this->t('Created the %label Font display.', [
175 '%label' => $font_display->label(),
180 drupal_set_message($this->t('Saved the %label Font display.', [
181 '%label' => $font_display->label(),
184 fontyourface_save_and_generate_font_display_css($font_display);
185 drupal_flush_all_caches();
186 $form_state->setRedirectUrl($font_display->urlInfo('collection'));
190 * Get a list of preset selectors.
193 * List of key-value selectors for selecting css selector presets.
195 private function getPresetSelectors() {
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',
209 * Return string that maps to selector.
211 * @param \Drupal\fontyourface\FontDisplayInterface $font_display
212 * Current Font Display entity.
215 * String that maps to preset selector. 'Other' or empty string otherwise.
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;
223 elseif (!empty($font_selector)) {