Tiny Modal

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
littleviking001's picture

I recently convinced the rest of my team that big, bulky version control systems are unnecessary, and that the only code repository any team should need is Twitter.com. So here's a module I wrote to prove that any Drupal module worth its snuff can fit into a 140-character post. The module takes any links with the 'm' class and turns them into a kind of modal dialog. (Note that the .module file is the one that's 140 characters, the .info file doesn't count.)

Some notes:

  • Since the code isn't wrapped in any functions, it's called as soon as it's included (at the end of bootstrap, just before hook_init gets called. So it's not much different from making a hook_init).
  • <script defer> only works in newer browsers, but it saves the need for a $(document).ready(function() {}) call.
  • The <m> tag is patently invalid, and I think IE will ignore it entirely, but it works in Firefox and that's good enough for me.
  • When I call href inside the onClick event of the links, the scope is the link, and it becomes an implied this.href value.
  • On the PHP side, die(menu_execute_active_handler()) causes the PHP script to stop and to print out menu_execute_active_handler() (which is Drupal's way of rendering page content - most Drupal pages are pasically theme('page', menu_execute_active_handler())).
  • And finally, $_GET['m']&&die(...) is basically shorthand for if ($_GET['m']) {die(...)}. When PHP encounters an AND, it will check the conditions one at a time and stop checking if it finds one that is false. So if $_GET['m'] is missing (NULL or FALSE), then it will stop the comparison and not die.

It would probably be a waste to attach files, seeing as the code is so small, so I'll just post the code here:

m.info

; $Id$
name = Tiny Modal
description = "Change any link into a modal dialog by adding the 'm' class"
core = 6.x

m.module

<m></m><script defer>$(".m").attr('onClick','return!$("m").load(href+"?m=1")')</script><?$_GET[m]&&die(menu_execute_active_handler());#dr140

Comments

I'm of the same mindset - I

Garrett Albright's picture

I'm of the same mindset - I often have to harangue my fellow programmers for making their code files too verbose. After all, it's only common sense that shorter code files will take far less time for the PHP parser to load from disk and parse. Legibility be damned - we're writing code for computers. If I wanted to write something for humans to read, I'd be a novelist.

But writing code which can fit into a tweet takes this idea to a whole new level. The future of computing is doing everything in the cloud. Thus, I propose that you not even store the code in a code file - just tweet it, then use the Twitter API to fetch that tweet and exec() it whenever a page is generated.

We are on the threshold of Web 3.0.

That is an excellent idea.

littleviking001's picture

I like the way you think.

New module

littleviking001's picture

This one allows Drupal sites to have q in the URL query. Normally, $_GET['q'] is reserved for the URL path, and attempting to visit a URL with q in the query will create strangeness and give you a 404. For example, visiting http://drupal.org/about?q=whatever actual directs you to http://drupal.org/whatever. This module maps the second q to _q, so you're essentially visiting http://drupal.org/about?_q=whatever. Visitors never know the difference, and site admins get the URLs they want.
getq.info

; $Id$
name = Get q
description = "Allow q in the URL query i.e. example.com/url/path?q=search+terms"
core = 6.x

getq.module
<?php $_GET['_q']=$_GET['q'];$_GET['q']=preg_replace('!/(.*?)\?.*!','\1',request_uri());#dr140

But this totally breaks if I

Garrett Albright's picture

But this totally breaks if I want to do something like example.com/url/path?q=search+terms&_q=this+is+ridiculous. I suggest this amendment to reduce the likelihood of overwriting a currently-set $_GET value:

<?php
$_GET
[sha1(rand())]=$_GET['q'];$_GET['q']=preg_replace('!/(.*?)\?.*!','\1',request_uri());#dr140
?>

To retrieve the original $_GET['q'] value later, you would use:

<?php
while((isset($rand)?$rand++:$rand=0)&&!($original_q=$_GET[sha1($rand)])){}#dr140
?>

Drupal for Evil

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: