Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / modules / contrib / fontyourface / modules / local_fonts / local_fonts.module
1 <?php
2
3 /**
4  * @file
5  * Local Fonts module file.
6  */
7
8 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\local_fonts\Entity\LocalFontConfigEntity;
10
11 /**
12  * Implements hook_fontyourface_api().
13  */
14 function local_fonts_fontyourface_api() {
15   return [
16     'version' => '3',
17     'name' => 'Custom Fonts',
18   ];
19 }
20
21 /**
22  * Implements hook_page_attachments().
23  */
24 function local_fonts_page_attachments(&$page) {
25   $enabled_fonts = &drupal_static('fontyourface_fonts', []);
26   foreach ($enabled_fonts as $font) {
27     if ($font->pid->value == 'local_fonts') {
28       $metadata = $font->getMetadata();
29       $font_id = $metadata['id'];
30       $directory = file_build_uri('fontyourface/local_fonts/' . $font_id);
31       $page['#attached']['html_head'][] = [
32         [
33           '#type' => 'html_tag',
34           '#tag' => 'link',
35           '#attributes' => [
36             'rel' => 'stylesheet',
37             'href' => file_create_url($directory . '/font.css'),
38             'media' => 'all',
39           ],
40         ], 'local-fonts-' . $font_id,
41       ];
42     }
43   }
44 }
45
46 /**
47  * Implements hook_entity_presave().
48  */
49 function local_fonts_entity_presave(EntityInterface $entity) {
50   if ($entity instanceof LocalFontConfigEntity) {
51     // Save and generate necessary font files.
52     local_fonts_save_and_generate_css($entity);
53
54     // Save Font in FYF DB storage.
55     $font_data = new \stdClass();
56     $font_data->name = $entity->label();
57     $font_data->url = 'local_fonts://' . $entity->id();
58     $font_data->provider = 'local_fonts';
59     $font_data->css_family = $entity->font_family;
60     $font_data->css_weight = $entity->font_weight;
61     $font_data->css_style = $entity->font_style;
62     $font_data->classification = array_filter($entity->font_classification);
63     $font_data->language = [
64       'English',
65     ];
66     $font_data->metadata = [
67       'id' => $entity->id(),
68     ];
69     fontyourface_save_font($font_data);
70   }
71 }
72
73 /**
74  * Implements hook_entity_delete().
75  */
76 function local_fonts_entity_delete(EntityInterface $entity) {
77   if ($entity instanceof LocalFontConfigEntity) {
78     $font_id = 'local_fonts://' . $entity->id();
79     $fids = \Drupal::entityQuery('font')
80       ->condition('url', $font_id)
81       ->range(0, 50)
82       ->execute();
83     if (!empty($fids)) {
84       $storage_handler = \Drupal::entityManager()->getStorage('font');
85       $fonts = $storage_handler->loadMultiple(array_keys($fids));
86       $storage_handler->delete($fonts);
87     }
88   }
89 }
90
91 /**
92  * Saves and generates font file based on font config entity data.
93  *
94  * @param Drupal\local_fonts\Entity\LocalFontConfigEntity $font_entity
95  *   Custom config font entity.
96  *
97  * @return bool
98  *   TRUE if files save successfully. Throw any errors otherwise.
99  */
100 function local_fonts_save_and_generate_css(LocalFontConfigEntity $font_entity) {
101   $directory = file_build_uri('fontyourface/local_fonts/' . $font_entity->id());
102   $css_file = $directory . '/font.css';
103   $font_file = $directory . '/font.woff';
104   file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
105
106   $css_file_data = "@font-face {\n";
107   $css_file_data .= "font-family: '" . $font_entity->font_family . "';\n";
108   $css_file_data .= "font-weight: " . $font_entity->font_weight . ";\n";
109   $css_file_data .= "font-style: " . $font_entity->font_style . ";\n";
110   $css_file_data .= "src: url('font.woff') format('woff');\n";
111   $css_file_data .= "}\n";
112
113   file_unmanaged_save_data(base64_decode($font_entity->getFontWoffData()), $font_file, FILE_EXISTS_REPLACE);
114   file_unmanaged_save_data($css_file_data, $css_file, FILE_EXISTS_REPLACE);
115
116   return TRUE;
117 }