Answers.Body: <p>The simplest solution of all.  As mentioned here: <a href="https://civicrm.stackexchange.com/questions/5146/civicase-dashboard-reordering-case-types">Civicase Dashboard - reordering case types</a></p>
  
  <p>I changed the weights in the table to easily change the order.</p>
  
  <p>Why the heck didn't I think of that?</p>
  
  <p>It would be nice to be able to change the weight within the Case Type UI.</p>
  
  <p><strong>Update</strong>
  While the weight solution worked nicely for the select lists in Views exposed filters, it would not sort the results by case type name only by id.</p>
  
  <p>For example, a view listing case types came out like this:<br>
  Case Type C<br>
  Case Type E<br>
  Case Type A<br>
  Case Type D<br>
  Case Type B</p>
  
  <p>The issue appears to be that the civicrm_case_type table is never exposed to views.</p>
  
  <p>So I added this to civicrm.case.inc:</p>
  
  <pre><code>//----------------------------------------------------------------
    // CIVICRM Case Type Names are here, base tabling it up.
    //----------------------------------------------------------------
  
    $data['civicrm_case_type']['table']['group'] = t('CiviCRM Case Type Names');
  
    $data['civicrm_case_type']['table']['base'] = array(
      // Governs the whole mozilla
      'field' =&gt; 'id',
      'title' =&gt; t('CiviCRM Case Type Names'),
      'help' =&gt; t("View displays CiviCRM Case Type Names"),
    );
  
    // Explain how this table joins to others.
    $data['civicrm_case_type']['table']['join'] = array(
      // Directly links to case table.
      'civicrm_case' =&gt; array(
        'left_field' =&gt; 'case_type_id',
        'field' =&gt; 'id',
      ),
    );
  
    //CiviCRM Case Type - FIELDS
  
    //Case Type Title
    $data['civicrm_case_type']['title'] = array(
      'title' =&gt; t('Case Type Name'),
      'real field' =&gt; 'title',
      'help' =&gt; t('The Name of the Case Type'),
      'field' =&gt; array(
      'handler' =&gt; 'views_handler_field',
      'click sortable' =&gt; TRUE,
      ),
      'argument' =&gt; array(
       'handler' =&gt; 'views_handler_argument',
      ),
      'sort' =&gt; array(
      'handler' =&gt; 'views_handler_sort',
      ),
    );
  </code></pre>
  
  <p>Which joins the table and only exposes the title field to Views.</p>
  
  <p>After I applied this, the same view printed out:<br>
  Case Type A<br>
  Case Type B<br>
  Case Type C<br>
  Case Type D<br>
  Case Type E</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-08T14:22:12.500Z
Answers.LastActivityDate: 2015-09-26T23:35:31.020Z
Answers.LastEditDate: 2017-04-13T12:53:13.017Z
Answers.UserDisplayName: pmoz
Answers.Body: <p>Have you tried <a href="https://www.drupal.org/project/civicrm_entity" rel="nofollow">Civicrm Entities</a> in Drupal?  It should allow you to expose anything as a drupal entity but I haven't verified this case.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-23T20:59:27.180Z
Answers.Text: In my limited interaction with CiviCRM Entities, it did not seem very tightly integrated with CiviCase.  Perhaps I'll take another look, but I think this is one of those things that should be in core.
Answers.UserDisplayName: pmoz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-24T01:28:41.113Z
Answers.Text: IMO civicrm entities should be in core rather than views :) can't say specifically about cases although I may need to use it in an upcoming project so pls update here with any learnings or questions.
Answers.UserDisplayName: Sonicthoughts
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-24T16:33:15.947Z
Answers.Text: I agree and I will!
Answers.UserDisplayName: pmoz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-23T13:47:35.733Z
Answers.LastActivityDate: 2015-07-23T13:47:35.733Z
Answers.UserDisplayName: Sonicthoughts
Answers.Body: <p>I think this will require a patch to the CiviCRM Views code. If you can offer to do so, you could open a ticket in JIRA and supply the fix</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-13T22:32:33.017Z
Answers.Text: I'll poke around, but I fear it may be out of my league.
Answers.UserDisplayName: pmoz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-26T20:34:10.260Z
Answers.Text: Ticket [CRM-17285](https://issues.civicrm.org/jira/browse/CRM-17285) added and fix supplied.
Answers.UserDisplayName: pmoz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-13T20:25:19.507Z
Answers.LastActivityDate: 2015-07-13T20:25:19.507Z
Answers.UserDisplayName: petednz - fuzion
Answers.Body: <p>I had a similar problem on a non-civi drupal site (since this is a general drupal views problem). I worked around it with this inelegant junk in my custom module:</p>
  
  <p><code>function mymodule_views_pre_render($view) {
      if ($view-&gt;name == 'the_name_of_the_view') {
          foreach ($view-&gt;result as $i =&gt; $r) {
              // Might not need to loop - idea is to do your own custom sorting and put result into some $new_result variable
          }
          $view-&gt;result = $new_result;
      }
  }</code></p>
  
  <p>Note it's drupal 6. Might have to fiddle for drupal 7. You may want to use your favorite technique to examine $view->result to see what it looks like first.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-13T14:50:31.140Z
Answers.LastActivityDate: 2015-07-13T14:50:31.140Z
Answers.UserDisplayName: Demerit
Body: <p>We frequently need to sort Drupal Views by Case Type. </p>
  
  <p>It works, but sorts by id not the title field.</p>
  
  <p>I believe it has to do with the pseudoconstant references in civicrm.case.inc and civicrm.core.inc. </p>
  
  <p>I think that approach is fine except when it's time to sort and/or filter.</p>
  
  <p>In Drupal Views, all exposed filters are sorted by id.  So when a user tries to filter by Case Type they get an unordered list of Case Types that to their eyes are in a very random order and is more difficult to use.</p>
  
  <p>Same thing if I sort the overall view by case type.  Cases appear in non-alphabetical order and require a bit more concentration to digest them.</p>
  
  <p>Is there a way to sort these fields in alphabetical order?</p>
  
Tags: <civicase><drupal-views>
Title: How do I sort by Case Type Title instead of id in a Drupal View?
ThreadText: # How do I sort by Case Type Title instead of id in a Drupal View?
  
  (Asked by: username_0 on 2015-07-11)
  
  We frequently need to sort Drupal Views by Case Type.
  
  It works, but sorts by id not the title field.
  
  I believe it has to do with the pseudoconstant references in civicrm.case.inc and civicrm.core.inc.
  
  I think that approach is fine except when it's time to sort and/or filter.
  
  In Drupal Views, all exposed filters are sorted by id. So when a user tries to filter by Case Type they get an unordered list of Case Types that to their eyes are in a very random order and is more difficult to use.
  
  Same thing if I sort the overall view by case type. Cases appear in non-alphabetical order and require a bit more concentration to digest them.
  
  Is there a way to sort these fields in alphabetical order?
  
  **Tags:** civicase, drupal-views
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_0 on 2015-09-08. Score: 1 | Accepted answer
  
  The simplest solution of all.  As mentioned here: Civicase Dashboard - reordering case types
  I changed the weights in the table to easily change the order.
  Why the heck didn't I think of that?
  It would be nice to be able to change the weight within the Case Type UI.
  Update
  While the weight solution worked nicely for the select lists in Views exposed filters, it would not sort the results by case type name only by id.
  For example, a view listing case types came out like this:
  Case Type C
  Case Type E
  Case Type A
  Case Type D
  Case Type B
  The issue appears to be that the civicrm_case_type table is never exposed to views.
  So I added this to civicrm.case.inc:
  //----------------------------------------------------------------
    // CIVICRM Case Type Names are here, base tabling it up.
    //----------------------------------------------------------------
  
    $data['civicrm_case_type']['table']['group'] = t('CiviCRM Case Type Names');
  
    $data['civicrm_case_type']['table']['base'] = array(
      // Governs the whole mozilla
      'field' => 'id',
      'title' => t('CiviCRM Case Type Names'),
      'help' => t("View displays CiviCRM Case Type Names"),
    );
  
    // Explain how this table joins to others.
    $data['civicrm_case_type']['table']['join'] = array(
      // Directly links to case table.
      'civicrm_case' => array(
        'left_field' => 'case_type_id',
        'field' => 'id',
      ),
    );
  
    //CiviCRM Case Type - FIELDS
  
    //Case Type Title
    $data['civicrm_case_type']['title'] = array(
      'title' => t('Case Type Name'),
      'real field' => 'title',
      'help' => t('The Name of the Case Type'),
      'field' => array(
      'handler' => 'views_handler_field',
      'click sortable' => TRUE,
      ),
      'argument' => array(
       'handler' => 'views_handler_argument',
      ),
      'sort' => array(
      'handler' => 'views_handler_sort',
      ),
    );
  
  Which joins the table and only exposes the title field to Views.
  After I applied this, the same view printed out:
  Case Type A
  Case Type B
  Case Type C
  Case Type D
  Case Type E
  
  
  ---
  
  ### Answer by username_1 on 2015-07-23. Score: 2
  
  Have you tried [Civicrm Entities](https://www.drupal.org/project/civicrm_entity) in Drupal? It should allow you to expose anything as a drupal entity but I haven't verified this case.
  
  > **Comments:**
  > **username_0**: In my limited interaction with CiviCRM Entities, it did not seem very tightly integrated with CiviCase.  Perhaps I'll take another look, but I think this is one of those things that should be in core.  
  > **username_1**: IMO civicrm entities should be in core rather than views :) can't say specifically about cases although I may need to use it in an upcoming project so pls update here with any learnings or questions.  
  > **username_0**: I agree and I will!  
  
  ---
  
  ### Answer by username_2 on 2015-07-13. Score: 1
  
  I think this will require a patch to the CiviCRM Views code. If you can offer to do so, you could open a ticket in JIRA and supply the fix
  
  > **Comments:**
  > **username_0**: I'll poke around, but I fear it may be out of my league.  
  > **username_0**: Ticket [CRM-17285](https://issues.civicrm.org/jira/browse/CRM-17285) added and fix supplied.  
  
  ---
  
  ### Answer by username_3 on 2015-07-13. Score: 1
  
  I had a similar problem on a non-civi drupal site (since this is a general drupal views problem). I worked around it with this inelegant junk in my custom module:
  
  `function mymodule_views_pre_render($view) {
  if ($view->name == 'the_name_of_the_view') {
  foreach ($view->result as $i => $r) {
  // Might not need to loop - idea is to do your own custom sorting and put result into some $new_result variable
  }
  $view->result = $new_result;
  }
  }`
  
  Note it's drupal 6. Might have to fiddle for drupal 7. You may want to use your favorite technique to examine $view->result to see what it looks like first.
  
  
  ---
  
  