Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / metatag / metatag_mobile / metatag_mobile.module
1 <?php
2
3 /**
4  * @file
5  * Contains metatag_mobile.module..
6  */
7
8 use Drupal\Core\Routing\RouteMatchInterface;
9
10 /**
11  * Implements hook_help().
12  */
13 function metatag_mobile_help($route_name, RouteMatchInterface $route_match) {
14   switch ($route_name) {
15     // Main module help for the metatag_mobile module.
16     case 'help.page.metatag_mobile':
17       $output = '';
18       $output .= '<h3>' . t('About') . '</h3>';
19       $output .= '<p>' . t('Provides support for meta tags used to control the mobile browser experience.') . '</p>';
20       return $output;
21
22     default:
23   }
24 }
25
26 /**
27  * Implements hook_theme().
28  */
29 function metatag_mobile_theme() {
30   $info['metatag_mobile_android_app'] = [
31     'render element' => 'element',
32   ];
33   $info['metatag_mobile_ios_app'] = [
34     'render element' => 'element',
35   ];
36
37   return $info;
38 }
39
40 /**
41  * Implements hook_page_attachments_alter().
42  */
43 function metatag_mobile_page_attachments_alter(array &$attachments) {
44   if (isset($attachments['#attached']['html_head'])) {
45     // A list of core tags that will be replaced, in the format:
46     // core tag => Metatag-supplied tag
47     // This assumes that the module's meta tags are output *before* the core
48     // tags, if this changes then We're Going To Have A Bad Time.
49     $dupes = [
50       'MobileOptimized' => 'mobileoptimized',
51       'HandheldFriendly' => 'handheldfriendly',
52       'viewport' => 'viewport',
53     ];
54
55     // Keep track of when the Metatag-supplied meta tags are found, so if the
56     // core tag is also found it can be removed.
57     $found = [];
58
59     foreach ($dupes as $core_tag => $meta_tag) {
60       foreach ($attachments['#attached']['html_head'] as $key => $item) {
61         if (isset($item[1])) {
62           // The Metatag values are output before core's, so skip the first item
63           // found so it can be picked up as the dupe; this is important for the
64           // "viewport" meta tag where both core and Metatag use the same name.
65           if ($item[1] == $meta_tag && !isset($found[$meta_tag])) {
66             $found[$meta_tag] = $key;
67           }
68           elseif ($item[1] == $core_tag && isset($found[$meta_tag])) {
69             // @todo This ought to work, but doesn't?
70             // $attachments['#attached']['html_head'][$key]['#access'] = FALSE;
71             unset($attachments['#attached']['html_head'][$key]);
72           }
73         }
74       }
75     }
76   }
77 }
78
79 /**
80  * Theme callback for an Android app link meta tag.
81  *
82  * The format is (all on oneline):
83  * <link rel="alternate" href="android-app://com.example.Example
84  * /sitesection/sitepage/thispage" />
85  */
86 function theme_metatag_mobile_android_app($variables) {
87   // Pass everything through to the normal 'link' tag theme.
88   $variables['element']['#name'] = 'alternative';
89   $variables['element']['#value'] = 'android-app://' . $variables['element']['#value'];
90
91   return theme('metatag_link_rel', $variables);
92 }
93
94 /**
95  * Theme callback for an iOS app link meta tag.
96  *
97  * The format is:
98  * <link rel="alternate" href="ios-app://123456/example/hello-screen" />
99  */
100 function theme_metatag_mobile_ios_app($variables) {
101   // Pass everything through to the normal 'link' tag theme.
102   $variables['element']['#name'] = 'alternative';
103   $variables['element']['#value'] = 'ios-app://' . $variables['element']['#value'];
104
105   return theme('metatag_link_rel', $variables);
106 }