Sort Reference view by last name of first Contributor

Created on 17 November 2020, over 3 years ago
Updated 4 January 2024, 6 months ago

Problem/Motivation

I would like to be able to sort a Reference view with the first Contributor of a Reference as sort criteria. If I add 'Contributor: Last name' as sort criteria to my Reference view it picks one of the Contributors of the Reference, but not necessarily the first one. Is it possible to add a 'Contributor: Last name of first author' sort criterium? Or, have I misconfigured my View?

Steps to reproduce

  • Add a View which shows 'Reference' of 'All' types sorted by: 'Unsorted'
  • Add some fields to display, at least 'Reference: Author'
  • Add Contributor as Relationship
  • Add 'Contributor: Last name' to Sort Criteria

The full configuration I use is attached (views.view.output_test.yml)

Thanks!

πŸ’¬ Support request
Status

Active

Component

Code

Created by

πŸ‡³πŸ‡±Netherlands gkaas

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡©πŸ‡ͺGermany johnnny83

    What is also weird (but a little bit off-topic): I have a content type "team member" and in each publication type (book, journal etc.) an entity reference field to team member, so you can connect every publication to a team member. Now I want to list on every page of a team member only some (not all) publications that are connected to them. That's why I created an entity reference field connected to a entity reference view that is listing the publications of the team member. So when I edit a team member page I can select which publications shall be shown. The problem is that the sorting by publication year is not always working. Many publications are in the right time order (and within a year also the sorting by last name is working), but some are not, so it looks like: 2022, 2014, 2021, 2021, 2021, 2020, 2020, 2018, 2017, 2017, 2016, 2014, 2021, 2018, 2018, 2011

  • πŸ‡«πŸ‡·France Toki Caen, Normandy

    Hello @antongp

    I added the mini-module defined above (#2) and indeed the sorting is good.
    Now I have a problem because my exposed filter on the author name only works on the first author since the condition imposes the delta to zero.
    Would it be possible to have some code to sort the view without adding a filter?
    I need some help to start this little code.
    I imagine we can start with $query->set('order',$first_author) where $first_author is the bibcite_reference__author where delta is 0? and then $query->set('order','asc')?
    I am not quite sure how to get the $first_author though.
    Thanks for any tip here.

  • πŸ‡«πŸ‡·France Toki Caen, Normandy

    I suppose the SQL query is this one :
    SELECT last_name FROM bibcite_contributor LEFT JOIN bibcite_reference__author ON bibcite_contributor.id=bibcite_reference__author.author_target_id WHERE bibcite_reference__author.delta=0
    Now it would be nice to get this information for each row of the View and use the result as sorting criteria.
    Anyone?
    This feature is critical for any end-user, unsorted bibliography is a bit useless, I'm afraid.
    Something else, when I use the contributor in a relationship to access last_name, I need to aggregate results.
    If not, each reference is repeated for each author.
    And finally, when displaying a contributor field to get the last_name, the result for each reference is chaotic, once it is the first author last name, once it is the third author last name, etc.
    I am more and more lost...

  • πŸ‡°πŸ‡ΏKazakhstan usdv

    @toki Hello,
    It seems this sorting it is a important feature you're right. I think I'll take a look at what we can do with this in next couple of days. Let me dig into that.

  • πŸ‡«πŸ‡·France Toki Caen, Normandy

    This is great news, many thanks, don't hesitate if you need some testing from a humble site builder.

  • Hi @Toki

    I can suppose next temporary solution. It contains code of hook from #2 πŸ’¬ Sort Reference view by last name of first Contributor Active it required to sort by first author in the Reference.

    Also needed to add relationship to Contributor. Add field, Last Name for example, to sort to the field list and exclude it from display.
    Then needed to open Format Settings and set this field checkbox Sortable and radio Default sort.

    It works for my instance for admin views bibcite_reference_admin which shows References in Table format.

  • πŸ‡«πŸ‡·France Toki Caen, Normandy

    Hi @AardWolf

    Thanks for testing alternatives here.
    Actually I have managed to get a view sorted by year and then secondly by first author last name, thanks to the code suggested by @antongp (I have started with that in comment #8).
    My issue here is to get rid of the condition on delta=0 because if I expose a filter on last name author I can't get people which are never first author (my client's request).
    So I was wondering how to build a custom sort based on this delta column in bibcite_reference_author in relation with last_name in bibcite_contributor.

  • πŸ‡«πŸ‡·France Toki Caen, Normandy

    And just to confirm what @johnnny83 wrote in his question on Drupal Answers, when adding a Contributor relationship, each reference is displayed as many times as it has authors. Even with Aggregation activated, the issue is still here. Using the short code above, as we filter by only the first author (delta = 0), the issue disappears but it sounds like a workaround to avoid a deeper problem?
    Let us know how we can help, at our humble level.

  • πŸ‡«πŸ‡·France Toki Caen, Normandy

    Within the previous bilbiography Drupal 7 module (Biblio), that kind of sorting was available in Views thanks to the bilbio_handler_sort_contributor_lastname.inc file.

    Does someone has any idea where to start to "convert" this code into Drupal 9/Symfony code?
    And sorting by the first rank author would be probably enough here, no need for the rank_options function.

    <?php
    
    /**
     *
     */
    class biblio_handler_sort_contributor_lastname extends views_handler_sort {
    
      /**
       *
       */
      public function option_definition() {
        $options = parent::option_definition();
    
        $options['rank'] = array('default' => 0);
        return $options;
      }
    
      /**
       *
       */
      public function admin_summary() {
        $order = parent::admin_summary();
        $rank = $this->rank_options();
        return $rank[$this->options['rank']] . ' ' . t('Author') . ', ' . $order;
      }
    
      /**
       *
       */
      public function options_form(&$form, &$form_state) {
        parent::options_form($form, $form_state);
        $form['op_val_start'] = array('#value' => '<div class="clearfix">');
        $form['rank'] = array(
          '#title' => t('Sort by which author?'),
          '#type' => 'select',
          '#options' => $this->rank_options(),
          '#default_value' => $this->options['rank'],
        );
        $form['op_val_end'] = array('#value' => '</div>');
    
      }
    
      /**
       *
       */
      public function rank_options() {
        return array('1st', '2nd', '3rd', '4th', '5th');
      }
    
      /**
       *
       */
      public function query() {
        parent::query();
        $this->query->add_where(0, "biblio_contributor.rank", $this->options['rank'], '=');
      }
    
    }
    
  • πŸ‡΅πŸ‡ΉPortugal lolgm

    For those who are having problems with placing the delta filter on fields, for example the author field shown by @antongp in comment #2 ( #3182978-2: Sort Reference view by last name of first Contributor β†’ ), you can see more about this problem in issue πŸ› Reference multivalue base fields are missing the "delta" views field Needs review .

Production build 0.69.0 2024