4b8fed91c9652e2a3615d1ab3a130756d9ce91c1
[yaffs-website] / web / modules / contrib / fontyourface / modules / local_fonts / src / Form / LocalFontConfigEntityForm.php
1 <?php
2
3 namespace Drupal\local_fonts\Form;
4
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\file\Entity\File;
8
9 /**
10  * Class LocalFontConfigEntityForm.
11  *
12  * @package Drupal\local_fonts\Form
13  */
14 class LocalFontConfigEntityForm extends EntityForm {
15
16   /**
17    * {@inheritdoc}
18    */
19   public function form(array $form, FormStateInterface $form_state) {
20     $form = parent::form($form, $form_state);
21
22     $local_font_config_entity = $this->entity;
23     $form['label'] = [
24       '#type' => 'textfield',
25       '#title' => $this->t('Label'),
26       '#maxlength' => 255,
27       '#default_value' => $local_font_config_entity->label(),
28       '#description' => $this->t("Name of the Custom Font. Note that while Font Family is not necessarily unique, this name is."),
29       '#required' => TRUE,
30     ];
31
32     $form['id'] = [
33       '#type' => 'machine_name',
34       '#default_value' => $local_font_config_entity->id(),
35       '#machine_name' => [
36         'exists' => '\Drupal\local_fonts\Entity\LocalFontConfigEntity::load',
37       ],
38       '#disabled' => !$local_font_config_entity->isNew(),
39     ];
40
41     $form['font_family'] = [
42       '#type' => 'textfield',
43       '#title' => $this->t('Font family'),
44       '#default_value' => (isset($local_font_config_entity->font_family)) ? $local_font_config_entity->font_family : '',
45       '#description' => $this->t('The CSS Font Family. The @font-face name will be based on this.'),
46       '#size' => 60,
47       '#maxlength' => 128,
48       '#required' => TRUE,
49     ];
50
51     $form['font_style'] = [
52       '#type' => 'select',
53       '#title' => $this->t('Font Style'),
54       '#options' => [
55         'normal' => $this->t('Normal'),
56         'italic' => $this->t('Italics'),
57       ],
58       '#default_value' => (isset($local_font_config_entity->font_style)) ? $local_font_config_entity->font_style : 'normal',
59     ];
60
61     $form['font_weight'] = [
62       '#type' => 'select',
63       '#title' => $this->t('Font Weight'),
64       '#options' => [
65         '100' => '100',
66         '200' => '200',
67         '300' => '300',
68         '400' => $this->t('400 (normal)'),
69         '500' => '500',
70         '600' => '600',
71         '700' => $this->t('700 (bold)'),
72         '800' => '800',
73         '900' => '900',
74       ],
75       '#default_value' => (isset($local_font_config_entity->font_weight)) ? $local_font_config_entity->font_weight : '400',
76     ];
77
78     $form['font_classification'] = [
79       '#type' => 'checkboxes',
80       '#title' => $this->t('Font Classification'),
81       '#description' => $this->t('This is mostly useful for filtering.'),
82       '#options' => [
83         'serif' => $this->t('Serif'),
84         'sans-serif' => $this->t('Sans Serif'),
85         'slab-serif' => $this->t('Slab Serif'),
86         'handwriting' => $this->t('Handwriting'),
87         'decorative' => $this->t('Decorative'),
88         'monospace' => $this->t('Monospace'),
89       ],
90       '#default_value' => (isset($local_font_config_entity->font_classification)) ? $local_font_config_entity->font_classification : [],
91     ];
92
93     $form['font_file'] = [
94       '#type' => 'managed_file',
95       '#title' => $this->t('Font File'),
96       '#description' => $this->t('The font file must be in WOFF format since that is accepted by all modern browsers.'),
97       '#size' => 50,
98       '#upload_validators' => [
99         'file_validate_extensions' => ['woff'],
100         'file_validate_size' => [file_upload_max_size()],
101         'file_validate_name_length' => [],
102       ],
103     ];
104
105     return $form;
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function validateForm(array &$form, FormStateInterface $form_state) {
112     $local_font_config_entity = $this->entity;
113
114     $values = $form_state->getValues();
115     if (empty($values['font_file']) && empty($local_font_config_entity->getFontWoffData())) {
116       $form_state->setErrorByName('font_file', $this->t('WOFF file must be uploaded'));
117     }
118   }
119
120   /**
121    * {@inheritdoc}
122    */
123   public function save(array $form, FormStateInterface $form_state) {
124     $values = $form_state->getValues();
125
126     // Save Custom Font Config Entity.
127     $local_font_config_entity = $this->entity;
128     if (!empty($values['font_file'])) {
129       // Get contents of Font File.
130       $font_file = File::load($values['font_file'][0]);
131       $font_file_data = base64_encode(file_get_contents($font_file->getFileUri()));
132       $local_font_config_entity->setFontWoffData($font_file_data);
133     }
134     $status = $local_font_config_entity->save();
135
136     switch ($status) {
137       case SAVED_NEW:
138         drupal_set_message($this->t('Created the %label Custom Font.', [
139           '%label' => $local_font_config_entity->label(),
140         ]));
141         break;
142
143       default:
144         drupal_set_message($this->t('Saved the %label Custom Font.', [
145           '%label' => $local_font_config_entity->label(),
146         ]));
147     }
148     $form_state->setRedirectUrl($local_font_config_entity->urlInfo('collection'));
149   }
150
151 }