Customize the Subscriptions terms list

Tags: 

A client recently requested instructions on how to customize the Subscriptions module's user subscriptions page to show more details instead of just the term title, e.g. show a description, image, etc. Thankfully this turns out to be pretty easy and just involves using hook_form_alter to customize the labels.

To use the following code just add it to a custom module and replace the "mymodule" bit with your module's name.

FYI this depends upon the patch from http://drupal.org/node/1522306 and is for Drupal 7.


/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Replace the term labels on the User Subscriptions page. This works by
 * building a full display of the term, thus the term's display should be
 * configured & themed as necessary.
 */
function mymodule_form_subscriptions_page_user_overview_alter(&$form, &$form_state, $form_id) {
  // Only proceed if the 'taxa' field is present, thus the Taxonomy
  // Subscriptions module is enabled.
  if (isset($form['taxa'])) {
    // Process each vocabulary.
    foreach ($form['taxa'] as $key => $data) {
      // Vocabularies will have a numeric $key and the $data will be an array.
      if (is_numeric($key) && is_array($data)) {
        // Change the label to a copy of the term rendered out fully.
        foreach ($data[0]['labels'] as $tid => $label) {
          // Load the term.
          $term = taxonomy_term_load($tid);

          // Build the display array for the term. This can be changed by adding
          // another attribute to the function to specify the view mode - see
          // http://api.drupal.org/taxonomy_term_view for full details.
          $term_array = taxonomy_term_view($term);

          // Render the final output & replace the label.
          $form['taxa'][$key][0]['labels'][$tid][-1]['#markup'] = render($term_array);
        }
      }
    }
  }
}

This results in the page listing taxonomy terms as follows:
The subscriptions list now shows the full taxonomy term display, not just the title.

How to reply

Care to add your own 2 cents? Let me know via Twitter or my contact page.