Drupal7: Altering Behavior of Views Row Selector

In using Views Row Selector I wanted my row selector to behave differently than the module creator implemented:

  • Collect a different value per selected row than the default NID value. Unfortunately this value is hard-coded into the modules views field handler configuration code.
  • Show the checked, or selected, row(s) by visibly highlighting them in the UI.

To perform these tasks we have to make use of a Views API function and also make use of jQuery.

Altering the selected value(s)

  • I added a hidden field to my View a student_id and an employee_id; as a user in my system may have both. The field is a string encoded as “sSTUDENT_ID::eEMPLOYEE_ID“.
  • I then used hook_views_data_alter to inform Drupal that my version of a field handler class should be used as opposed to the default provided by the community module.
    /**
     * Implements hook_views_data_alter().
     */
    function ucsb_identity_searcher_views_data_alter(&$data) {
      $data['views']['views_row_selector'] = array(
        'title' => t('Row Selector Identity'),
        'help' => t('Provide a checkbox to select the rows'),
        'field' => array(
          'handler' => 'ucsb_identity_searcher_field_handler_row_selector',
          'click sortable' => FALSE,
        ),
      );
    }
  • Being lazy, my copy of the Class merely changes the hidden value field to store my custom IDs value. I copied the whole class code as opposed to overriding just the specific method. Ideally you only override what you need, but for me this copy/paste is simply easier. My added code is simply:
    class ucsb_identity_searcher_field_handler_row_selector extends views_handler_field {
    
      // .... Lots of code here.
    
      // For terseness:  sNUMBER:eNUMBER
      //
      // If any is missing supply the missing 'e' or 's'.
      function _row_selector_get_value(&$row_index, &$row) {
        #dpm($this, __FUNCTION__);
        if ($this->query instanceof ldap_views_plugin_query_ldap) {
          $results = NULL;
          if (isset($row['ldap_employeeNumber'])) {
            $results[$row_index][] = 'e'.$row['ldap_employeeNumber'];
          }
          if (isset($row['ldap_StudentPerm'])) {
            $results[$row_index][] = 's'.$row['ldap_StudentPerm'];
          }
          return $results[$row_index] = implode(':',$results[$row_index]);
        } else {
          return $results[$row_index] = $row->nid;
        }
      }
    
      // ... More code here. See the views_row_selector field handler class.
    }

So the above code alters what Views Row Selector passes along in its hidden field. We do not hack or change the contributed module, we simply override and enforce our own business logic using API function calls.

Highlighting Selected Table Rows

To make the display highlight the selected row(s) we add the following JS code to a site-wide included javascript file:

(function($) {
  $(document).ready(function() {
      // This targets our specific url page and results table.
      $('body.page-identity-search form table tr')
        .filter(':has(:checkbox:checked)')
        .addClass('selected')
        .end()
      .click(function(event) {
        $(this).toggleClass('selected');
        if (event.target.type !== 'checkbox') {
          $(':checkbox', this).attr('checked', function() {
            return !this.checked;
          });
        }
      });

    });
}(jQuery));

The end result of these 2 tweaks look like the following:

Selected row in search result.

Selected row in search result.

Woo Hoo!

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , , , , | Posted under Drupal | RSS 2.0

Author Spotlight

David Gurba

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *


5 + 8 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>