0

I want to setup a View that will display all Title's within the company in a certain way.

History (not sure if necessary, but I imagine it can't hurt): I had a lot of duplicate Title's, because I grab the title from the user node, no distinct features seemed to help. I found a way of using hook__query_alter(QueryAlterableInterface $query) to make sure the titles were in order and they were not duplicated. If I display the View, as is, as a table, it looks like a normal list:

enter image description here

But I want the table to look like the following:

enter image description here

Can I do this without code? No idea how, If I can't, which is fine, my real question is below:

I can grab the view in template_views_pre_render(&$view), I noticed I can also snag some of it in the hook_preprocess_views_view(&$vars), and I can grab pieces in the template (...tpl.php) files like views-view-field-w/e.tpl.php and views-view--w/e.tpl.php. But as it stands I really don't know where to begin my algorithm and sifting through. In order to do this I imagine I build 3 arrays, array A, B, and C (eventually also D, E, F, etc.) and just spit them out in one of the template files to build rows for each alphabetical column in a table structure, but where do I build these arrays up, I hear preprocess hook is where it might be done? If so, how do I hand them over to the template files (stuff them back into the views array?). If I shouldn't be doing it that way, should I just build up my arrays and spit them out in the table format all in the template files?

1
  • Could you maybe format an example list in your question? I'm not really following what you are trying to do. There is a "group by field" setting in a view which you could use to group employees together if they have the same title. Commented Dec 5, 2015 at 23:48

1 Answer 1

0

In order to do this I handled everything on the views-view-unformatted-{view_name}.tpl.php level.

I am not sure if it is the most efficient way of doing all of the work, but it works like a charm. If anyone has any suggestions on how to optimize the code, or if I should be putting this work in the template.php file please let me know, but as it stands this answered and solved my problem.

/* Issue:         In order to accomodate the appropriate style requirements of 
 *               this view we need to override with this file and handle the $rows
 *               in the following format. The standard Table Format for Views 
 *               does not provide what we want (refer to Description to understand
 *               what we wanted).
* Description:   Overriding the views-view-unformatted...tpl.php file so we can 
 *               better control view page. This View displays
 *               all the Employee Titles. Which is fine, but we want it in a 
 *               particular format:
 *  (headers)                  A                 B                 C
 *  (titles)              App Specialist   Batch Processor   C.........
 *                        App Developer    B..........       C.........
 * 
 * 
 * ==========================================================================
*/
?>
<?php 
    /* 
     Lets grab and collate all of the Employee Titles and the the headers for 
     each set of Employee Titles (Example of a set = all titles that start 
     with 'A', or 'B', etc. 
    */

    // Employee Titles.
    $titles = array();
    // Employee Title Headers.
    $title_headers = array();
    // Keep track of the character we are on.
    $first_character = '';
    // Keep track of the last character we were on.
    $last_character = '';
    // Header count for the iteration process.
    $head_count = 0;
    // Title count for the iteration process.
    $title_count = 0;    

    foreach ($rows as $row) {      
      $employee_title = strip_tags($row); //strip
      $employee_title_trimmed = trim($employee_title); //trim
      $first_character = $employee_title_trimmed[0];

      // We don't have the character saved.
      if ($last_character != $first_character) {
        // Increase count.
        $head_count++;
        $title_count++;
        // Fill the headers array.
        $title_headers[$head_count] = $first_character; // add the letter
        $last_character = $first_character; // update the last character
      }
      // Fill the employee title's array
      $titles[$title_count][] = array($employee_title_trimmed => $row);  
    }  

    /* Time to display the header and title arrays. */
    // We only want 3 columns, so that's what this variable ensures. if we 
    // wanted 4 or 2 columns we just change this variable and it will take effect 
    // throughout.
    $columns_limit = 3;
    // counter for the employee title headers.
    $count_header = 1;
    // Max counter for the headers.
    $max_count_header = $count_header + $columns_limit;
    // Counter for the employee titles.
    $count_list = 1;
    // Max counter for titles.
    $max_count_list = $count_list + $columns_limit;


    print("<table>");
    while($count_header < sizeOf($titles)){ //never exceed size of titles
      print"<tr>"; // Header row starts.
      // Display 3 or less headers, depending on where we are in the array.
      for ($count_header; $count_header < $max_count_header; $count_header++){
        print "<th>";
        print $title_headers[$count_header]; // Display header.
        print "</th>";
      }
      print"</tr>"; // Header row stops
      print"<tr>"; // Employee title row starts
      for ($count_list; $count_list < $max_count_list; $count_list++) {
        print'<td id="title-list">';
        print"<ul name=\"$title_headers[$count_list]\">";
        foreach ($titles[$count_list] as $titles) {
          $title_value = array_values($titles);
          $title_entry = $title_value[0];
          print"<li id=\"blue-li\">";
          print $title_entry; // Display the employee title.
          print"</li>";
        }
        print"</ul>";
        print"</td>";
      }
      print"</tr>"; // Employee title row stops.
      $max_count_header = $count_header + $columns_limit; // Update Header's Max
      $max_count_list = $count_list + $columns_limit;     // Update Employee's Max
    }
    print("</table>");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.