3 namespace Drupal\local_fonts\Form;
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\file\Entity\File;
10 * Class LocalFontConfigEntityForm.
12 * @package Drupal\local_fonts\Form
14 class LocalFontConfigEntityForm extends EntityForm {
19 public function form(array $form, FormStateInterface $form_state) {
20 $form = parent::form($form, $form_state);
22 $local_font_config_entity = $this->entity;
24 '#type' => 'textfield',
25 '#title' => $this->t('Label'),
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."),
33 '#type' => 'machine_name',
34 '#default_value' => $local_font_config_entity->id(),
36 'exists' => '\Drupal\local_fonts\Entity\LocalFontConfigEntity::load',
38 '#disabled' => !$local_font_config_entity->isNew(),
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.'),
51 $form['font_style'] = [
53 '#title' => $this->t('Font Style'),
55 'normal' => $this->t('Normal'),
56 'italic' => $this->t('Italics'),
58 '#default_value' => (isset($local_font_config_entity->font_style)) ? $local_font_config_entity->font_style : 'normal',
61 $form['font_weight'] = [
63 '#title' => $this->t('Font Weight'),
68 '400' => $this->t('400 (normal)'),
71 '700' => $this->t('700 (bold)'),
75 '#default_value' => (isset($local_font_config_entity->font_weight)) ? $local_font_config_entity->font_weight : '400',
78 $form['font_classification'] = [
79 '#type' => 'checkboxes',
80 '#title' => $this->t('Font Classification'),
81 '#description' => $this->t('This is mostly useful for filtering.'),
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'),
90 '#default_value' => (isset($local_font_config_entity->font_classification)) ? $local_font_config_entity->font_classification : [],
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.'),
98 '#upload_validators' => [
99 'file_validate_extensions' => ['woff'],
100 'file_validate_size' => [file_upload_max_size()],
101 'file_validate_name_length' => [],
111 public function validateForm(array &$form, FormStateInterface $form_state) {
112 $local_font_config_entity = $this->entity;
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'));
123 public function save(array $form, FormStateInterface $form_state) {
124 $values = $form_state->getValues();
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);
134 $status = $local_font_config_entity->save();
138 drupal_set_message($this->t('Created the %label Custom Font.', [
139 '%label' => $local_font_config_entity->label(),
144 drupal_set_message($this->t('Saved the %label Custom Font.', [
145 '%label' => $local_font_config_entity->label(),
148 $form_state->setRedirectUrl($local_font_config_entity->urlInfo('collection'));