Adding Custom Mobile Protocols to Drupal
With the proliferation of mobile apps and Web sites on mobile devices, developers are often asked to integrate the two. Mobile apps can register a custom protocol with the mobile OS so that a link on a Web page such as
<a href=”myapp://openapp”>Open in MyApp</a>
will open MyApp on the user’s phone. An early example of this was to have links on phone numbers actually call the number on mobile phones.
On a recent project, I was asked to include a “Back to App” link on the site’s menu. “No problem,” I thought, “I’ll just set that as the destination of that menu item.” However trying to add
myapp://openapp
as a menu item destination lead to a
Notice : Undefined index: path in menu_edit_item_validate() (line 399 of //modules/menu/menu.admin.inc ).
The path '' is either invalid or you do not have access to it.
A little digging lead me to the problem: Drupal has a limited list of protocols it considers 'valid'. Fortunately, those are accessed via
variable_get
. Unfortunately, there is no admin page where you can edit that list. And, more confusingly, Drupal uses two different default values for the list of protocols (includes/common.inc@1336 includes the
tel
protocol but modules/filter/filter.module@1398 does not). Finally, by default, Drupal doesn't set this variable in the database so the default value passed to
variable_get
is what gets used.
My solution was to add a
hook_update_N
to an existing (custom) module to add the protocol I needed.
/**
* Adds myapp:// protocol to the list of allowed protocols.
*/
function example_update_7001(&$sandbox) {
// From include/common.inc @ 1336
$protocols = variable_get('filter_allowed_protocols', array('ftp', 'http', 'https', 'irc', 'mailto', 'news', 'nntp', 'rtsp', 'sftp', 'ssh', 'tel', 'telnet', 'webcal'));
$protocols[] = 'myapp';
variable_set('filter_allowed_protocols', $protocols);
}
Not interested in writing an update hook? You can also set this directly in your
settings.php
file:
$conf['filter_allowed_protocols'] = array('ftp', 'http', ...);
I prefer the first option. If there is another module trying to add a protocol to the list, the second option would negate that. But you can't beat editing your settings.php for a quick-fix that avoids the need for a custom module.
As an alternative see
Post new comment