973de0bc6cf6d0e3604655911c1bbff0efdea002
[yaffs-website] / web / modules / contrib / fontyourface / src / Form / FontSettingsForm.php
1 <?php
2
3 namespace Drupal\fontyourface\Form;
4
5 use Drupal\Core\Form\ConfigFormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\fontyourface\Entity\Font;
8 use Drupal\Core\StringTranslation\TranslatableMarkup;
9
10 /**
11  * Class FontSettingsForm.
12  *
13  * @package Drupal\fontyourface\Form
14  *
15  * @ingroup fontyourface
16  */
17 class FontSettingsForm extends ConfigFormBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   protected function getEditableConfigNames() {
23     return ['fontyourface.settings'];
24   }
25
26   /**
27    * Returns a unique string identifying the form.
28    *
29    * @return string
30    *   The unique string identifying the form.
31    */
32   public function getFormId() {
33     return 'Font_settings';
34   }
35
36   /**
37    * Defines the settings form for Font entities.
38    *
39    * @param array $form
40    *   An associative array containing the structure of the form.
41    * @param \Drupal\Core\Form\FormStateInterface $form_state
42    *   The current state of the form.
43    *
44    * @return array
45    *   Form definition array.
46    */
47   public function buildForm(array $form, FormStateInterface $form_state) {
48     $config = $this->config('fontyourface.settings');
49     $form['Font_settings']['#markup'] = 'Settings form for @font-your-face. Support modules can use this form for settings or to import fonts.';
50     $form['load_all_enabled_fonts'] = [
51       '#type' => 'checkbox',
52       '#title' => $this->t('Load all enabled fonts'),
53       '#default_value' => (int) $config->get('load_all_enabled_fonts'),
54       '#description' => $this->t('This will load all fonts that have been enabled regardless of theme. Warning: this may add considerable download weight to your pages depending on the number of enabled fonts'),
55     ];
56     $form['imports'] = [
57       '#type' => 'fieldset',
58       '#title' => 'Import',
59       '#collapsible' => FALSE,
60     ];
61     // Set the module weight. There is some general Drupal funk around module weights.
62     module_set_weight('fontyourface', 1);
63     foreach (\Drupal::moduleHandler()->getImplementations('fontyourface_api') as $module_name) {
64       module_set_weight($module_name, 10);
65     }
66     foreach (\Drupal::moduleHandler()->getImplementations('fontyourface_import') as $module_name) {
67       $form['imports']['import_' . $module_name] = [
68         '#type' => 'submit',
69         '#value' => $this->t('Import from @module', ['@module' => $module_name]),
70         '#attributes' => [
71           'style' => 'margin: 10px;',
72         ],
73         '#prefix' => '<div>',
74         '#suffix' => '</div>',
75       ];
76     }
77
78     $form['imports']['import'] = [
79       '#type' => 'submit',
80       '#value' => $this->t('Import all fonts'),
81       '#weight' => 10,
82     ];
83     return parent::buildForm($form, $form_state);;
84   }
85
86   /**
87    * Form submission handler.
88    *
89    * @param array $form
90    *   An associative array containing the structure of the form.
91    * @param \Drupal\Core\Form\FormStateInterface $form_state
92    *   The current state of the form.
93    */
94   public function submitForm(array &$form, FormStateInterface $form_state) {
95     $values = $form_state->getValues();
96     $op = (string) $values['op'];
97
98     $batch = [
99       'title' => $this->t('Importing...'),
100       'operations' => [],
101       'finished' => '\Drupal\fontyourface\Form\FontSettingsForm::importFinished',
102     ];
103     foreach (\Drupal::moduleHandler()->getImplementations('fontyourface_import') as $module_name) {
104       if ($op == $this->t('Import all fonts') || $op == $this->t('Import from @module', ['@module' => $module_name])) {
105         $batch['operations'][] = [
106           '\Drupal\fontyourface\Form\FontSettingsForm::importFromProvider',
107           [
108             $module_name,
109           ],
110         ];
111       }
112     }
113     if (!empty($batch['operations'])) {
114       batch_set($batch);
115     }
116
117     if ($op == $this->t('Save configuration')) {
118       $config = $this->config('fontyourface.settings')
119         ->set('load_all_enabled_fonts', $values['load_all_enabled_fonts'])
120         ->save();
121       parent::submitForm($form, $form_state);
122     }
123
124     // Resave enabled fonts.
125     $fonts = Font::loadEnabledFonts();
126     foreach ($fonts as $font) {
127       $font->enable();
128     }
129   }
130
131   /**
132    * Imports fonts from provider. Batch operation handler.
133    *
134    * @param string $module
135    *   Module name that is providing fonts.
136    * @param array $context
137    *   Context batch array.
138    */
139   public static function importFromProvider($module, array &$context) {
140     $context['message'] = new TranslatableMarkup('Importing from @module', ['@module' => $module]);
141     $module_handler = \Drupal::moduleHandler();
142     $new_context = $module_handler->invoke($module, 'fontyourface_import', [$context]);
143     if (!empty($new_context)) {
144       $context = $new_context;
145     }
146   }
147
148   /**
149    * Imports fonts from provider. Batch completion handler.
150    *
151    * @param bool $success
152    *   Boolean if operations were successful.
153    * @param array $results
154    *   Results of batch operations.
155    * @param array $operations
156    *   List of batch operations run.
157    */
158   public static function importFinished($success, array $results, array $operations) {
159     drupal_set_message(new TranslatableMarkup('Finished importing fonts.'));
160   }
161
162 }