3 namespace Drupal\fontyourface\Form;
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\fontyourface\FontDisplayInterface;
8 use Drupal\fontyourface\Entity\Font;
11 * Class FontDisplayForm.
13 * @package Drupal\fontyourface\Form
15 class FontDisplayForm extends EntityForm {
20 public function form(array $form, FormStateInterface $form_state) {
21 $form = parent::form($form, $form_state);
23 $font_display = $this->entity;
25 '#type' => 'textfield',
26 '#title' => $this->t('Label'),
28 '#default_value' => $font_display->label(),
29 '#description' => $this->t("Label for the Font display."),
34 '#type' => 'machine_name',
35 '#default_value' => $font_display->id(),
37 'exists' => '\Drupal\fontyourface\Entity\FontDisplay::load',
39 '#disabled' => !$font_display->isNew(),
42 /* You will need additional form elements for your custom properties. */
44 $fonts = Font::loadEnabledFonts();
46 drupal_set_message($this->t('Please select at least one font before picking a font style.'));
47 $form_state->setRedirect('entity.font.collection');
49 $available_fonts = [];
50 foreach ($fonts as $font) {
51 $available_fonts[$font->url->value] = $font->name->value;
54 $drupal_themes = \Drupal::service('theme_handler')->listInfo();
56 foreach ($drupal_themes as $key => $theme) {
57 if (!empty($theme->info['hidden'])) {
60 $themes[$key] = $theme->info['name'];
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,
72 foreach ($fonts as $font) {
73 $element_id = 'font_display_usage_' . $font->Id();
74 $form[$element_id] = [
75 '#type' => 'container',
78 'select[name="font_url"]' => ['value' => $font->url->value],
82 $form[$element_id]['usage'] = [
83 '#type' => 'fieldset',
84 '#collapsible' => FALSE,
87 $form[$element_id]['usage']['instructions'] = [
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:',
91 $form[$element_id]['usage']['preview'] = [
92 '#type' => 'html_tag',
95 'style' => 'white-space: pre;',
97 '#value' => fontyourface_font_css($font, NULL, "\n"),
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(),
108 $preset_selectors = $this->getPresetSelectors();
109 $form['preset_selectors'] = [
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),
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(),
124 '#required' => FALSE,
127 'select[name="preset_selectors"]' => ['value' => 'other'],
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,
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"));
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);
167 $status = $font_display->save();
171 drupal_set_message($this->t('Created the %label Font display.', [
172 '%label' => $font_display->label(),
177 drupal_set_message($this->t('Saved the %label Font display.', [
178 '%label' => $font_display->label(),
181 fontyourface_save_and_generate_font_display_css($font_display);
182 drupal_flush_all_caches();
183 $form_state->setRedirectUrl($font_display->urlInfo('collection'));
187 * Get a list of preset selectors.
190 * List of key-value selectors for selecting css selector presets.
192 private function getPresetSelectors() {
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',
206 * Return string that maps to selector.
208 * @param \Drupal\fontyourface\FontDisplayInterface $font_display
209 * Current Font Display entity.
212 * String that maps to preset selector. 'Other' or empty string otherwise.
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;
220 elseif (!empty($font_selector)) {