Using containers as #states enabled markup form elements

As part of the sprint we're holding in Paris right now to introduce new Commerce Guys to Drupal Commerce development, we devised a situation where we wanted a conditionally available message on the checkout form. We decided for a shipping scenario that we wanted to present a message to the user regarding shipping costs inline with the address form if the customer selected a shipping address outside of the free shipping country of the store.

Adding a random text message to a form is trivial. You can just add a markup element to the form, using a div tag to make sure it ends up in a fieldset if necessary:

<?php
$form
['message'] = array(
 
'#markup' => t('What a wonderful message!'),
);
?>

Additionally, adding a #states array that includes the instruction to hide a form element on the basis of a select list's value is trivial:

<?php
$form
['message'] = array(
 
'#markup' => t('What a wonderful message!'),
 
'#states' => array(
   
'invisible' => array(
     
':input[name="select_list_name"]' => array('value' => 'US'),
    ),
  ),
);
?>

Unless I botched my pseudo code, that #states array should instruct the Form API to include JavaScript to hide the element when the select list I specified has a value of US. Unfortunately, that code won't work. Shocked

The problem is that the JavaScript that hides the element depends on the element's ID to target the behavior, and a markup element by default gets no wrapping. This means you can't directly put a #states array on a markup element in general. You do have a few options to work around this, and I'll end with what we went with in our case... you can tell me if we're crazy. Sticking out tongue

  1. You could just hardcode a div wrapper that includes the ID and the form-wrapper class Drupal expects, but I can't say that I'd recommend it. Names change quite frequently during development and can easily be altered.
  2. You can also just put the markup element inside a container element and attach the #states array to the container. Containers are rendered as divs with proper IDs for targeting, so this works just fine.
  3. However, we wanted a compact solution, so what we did is turn the markup element into a container element and set its #children property to the message. This effectively makes the container element function as a markup element, but it actually wraps the markup in the appropriate div on output. Since #children is already set, this does mean that the container element cannot actually contain other elements, so there may be good reasons for you not to try this at home.

The gist of our solution was:

<?php
$form
['message'] = array(
 
'#type' => 'container',
 
'#children' => t('What a wonderful message!'),
 
'#states' => array(
   
'invisible' => array(
     
':input[name="select_list_name"]' => array('value' => 'US'),
    ),
  ),
);
?>

I suppose it would be nice if we didn't have to abuse the #children property name, but this seems like fair game to me unless it's a possibility to change the markup element to include the div and expected ID.

Maintaining submodules in a Git repository

I'm learning new Git tricks thanks to Damien and want to make sure I don't forget any of them, so I'm introducing a new Git tag here on the blog to start writing these down.

I'm working on a project with the team in Paris for the week, and the structure of the project is such that we maintain a main Git repository that pulls in Drupal and a variety of contributed modules using git submodules. To build the full project locally, I first clone our main repository and then issue git submodule init / update commands to get the proper versions of our submodules.

Now, I'm still learning the ropes with Git terminology and repository management, but from what I understand, a submodule folder points to the URL of the repository and a precise commit. It won't automatically track a branch, so when the remote repository has new commits or you make new commits in your submodule repositories, you have to tell the main repository something has changed.

First you would change directories into the submodule and either pull in the new commits or perhaps make your changes, commit them, and push them upstream. Then you would change back into the main repository directory and perform a git add on the submodule directory. Committing this will update the main repository to point to the new current commit in the submodule directory. Pushing this upstream will ensure that everyone else on the project can pull the change and use git submodule update to pull the new commits into their local submodule directories.

Pretty straightforward I suppose?

Facebook Messages

Much ado was made about Mark Zuckerberg's vision for Facebook messaging to function as a replacement for e-mail for the next generation. It's immediate. It's simple. It's natural. And of course it's integrated into a website where users already connect with a lot of the people they communicate with on a regular basis.

I was and still am skeptical. I rarely use Facebook messages, and when I do it's often for multi-party messaging. Occasionally I'll engage in one-on-one conversations, and it is handy to be able to quickly share images, links, etc. But Gmail is still my much more comfortable messaging home.

I did notice one psychological difference in the way I use the different services, though. When I send an e-mail, I always include at least a basic signature, "-Ryan", or my full blown contact information signature for work related e-mail. It actually feels a little rude to me not to. This is a little weird, because it's not like the people I communicate with need to see my name at the end of the e-mail to remember who sent it. However, on Facebook I have no such qualms. I rarely ever include my mini-signature.

Is it because it's more like a private forum? Is it because I expect my message to just be part of an ongoing conversation? Perhaps it's the way I relate to the two services based on their names... I would never send a letter without a signature via snail mail, so why would I send e-mail without one. Conversely, I would never add a signature to a text message, so why would I do so on a Facebook message?

Drupal Commerce in a git.d.o world

Because we were doing our Commerce module development on GitHub and exporting to CVS, we need to make some changes now that the Git migration on d.o is complete. Obviously the main Git repository for the project is now on d.o instead of GitHub. To clone the latest code, you can follow the helpful Git instructions included on Drupal project pages.

All of my D7 projects now include a 7.x-1.x branch that you should track to get the latest commits. I also updated the various dev releases of Commerce, Address Field, and my Commerce contribs to be snapshots of this branch, essentially deprecating master. I'm trying to decide the best thing to do about those pesky master branches and will likely just git rm everything in them and leave behind a README indexing the various HEADs in the repository.

Many people had forks of both the old Commerce repository and my development repository on GitHub. Additionally, some of you may have pending commits that haven't been pulled yet. If this is the case for you, please generate a patch instead of a new pull request and post it to the issue queue. As of this point I have no plans to rewrite the commit history of the new official repository to match the existing history on GitHub, meaning I cannot cleanly pull from your repositories.

(I have read Ben's blog post, and I'm sure there are some other finer aspects of Git that I'm just ignorant of to accommodate this... but I'm not sure it's worth the hassle when forks will need to be updated across the board anyways.)

This seems a little brute force, but I think it's for the best. The commit graph on d.o is now a nice tidy straight line, whereas the old graph on GitHub was a fractured mess that included several instances of duplicated commits thanks to botched merges and branching. We were still learning an appropriate Git workflow at the time and made several mistakes that not only dirtied up the history but also made it hard to sort out how to pull from various forks. (Accordingly, current contributor statistics on Ohloh are inaccurate anyways.)

This does mean some contributor credit in the commits will be lost, but I'll be updating various project pages to credit beta contributors for their commits. Regardless, I've been crediting contributors in commit messages all along, and merge commits show whose repository the merged commits came from. As of this point, I think the loss of a little meta information is worth the clean slate with the new repositories. Flame away in the comments if this is the stupidest idea you've ever heard... just want to make sure my plans are clear.

While we figure out how to handle pull requests in an environment that doesn't natively support them, in your issue please indicate your repository URL (whether it's a d.o sandbox of GitHub fork) and branch that I should pull from to compare. Bonus points for attaching a patch for quick review on site (or linking to the diff on the appropriate web interface). I'll add your repository as a remote so you don't have to post it every time. Additionally, I'd prefer it if you used a single branch per issue you're addressing with the branch name simply being the nid of the issue - makes it quite easy for me to branch and pull locally.

(I should mention that the good ol' patch process is perfectly fine if you don't want to maintain a public repository for me to pull from. You can just make your commits locally and diff 'em all in one fell swoop to create a patch for review.)

This is essentially what we've been doing on GitHub, so no surprises here for existing contributors. I'll leave the GitHub repos up for a while, but I'm updating their READMEs to point to the new repository on d.o. Once tested and approved, these guidelines will find their way into a handbook page for posterity's sake.

Pages