3 namespace Drupal\fontyourface\Form;
5 use Drupal\Core\Form\ConfigFormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\fontyourface\Entity\Font;
8 use Drupal\Core\StringTranslation\TranslatableMarkup;
11 * Class FontSettingsForm.
13 * @package Drupal\fontyourface\Form
15 * @ingroup fontyourface
17 class FontSettingsForm extends ConfigFormBase {
22 protected function getEditableConfigNames() {
23 return ['fontyourface.settings'];
27 * Returns a unique string identifying the form.
30 * The unique string identifying the form.
32 public function getFormId() {
33 return 'Font_settings';
37 * Defines the settings form for Font entities.
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.
45 * Form definition array.
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'),
57 '#type' => 'fieldset',
59 '#collapsible' => FALSE,
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);
66 foreach (\Drupal::moduleHandler()->getImplementations('fontyourface_import') as $module_name) {
67 $form['imports']['import_' . $module_name] = [
69 '#value' => $this->t('Import from @module', ['@module' => $module_name]),
71 'style' => 'margin: 10px;',
74 '#suffix' => '</div>',
78 $form['imports']['import'] = [
80 '#value' => $this->t('Import all fonts'),
83 return parent::buildForm($form, $form_state);;
87 * Form submission handler.
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.
94 public function submitForm(array &$form, FormStateInterface $form_state) {
95 $values = $form_state->getValues();
96 $op = (string) $values['op'];
99 'title' => $this->t('Importing...'),
101 'finished' => '\Drupal\fontyourface\Form\FontSettingsForm::importFinished',
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',
113 if (!empty($batch['operations'])) {
117 if ($op == $this->t('Save configuration')) {
118 $config = $this->config('fontyourface.settings')
119 ->set('load_all_enabled_fonts', $values['load_all_enabled_fonts'])
121 parent::submitForm($form, $form_state);
124 // Resave enabled fonts.
125 $fonts = Font::loadEnabledFonts();
126 foreach ($fonts as $font) {
132 * Imports fonts from provider. Batch operation handler.
134 * @param string $module
135 * Module name that is providing fonts.
136 * @param array $context
137 * Context batch array.
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;
149 * Imports fonts from provider. Batch completion handler.
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.
158 public static function importFinished($success, array $results, array $operations) {
159 drupal_set_message(new TranslatableMarkup('Finished importing fonts.'));