%% Copyright 2020 Philip Kime
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% This work has the LPPL maintenance status `maintained'.
%%
%% The Current Maintainer of this work is Philip Kime.

\ProvidesFile{apa6.bbx}[2020/08/28\space v8.5\space APA 6th ed. biblatex references style]
\RequireBiber[3]
\RequireBibliographyStyle{standard}
\urlstyle{same} % APA examples all have URLs in same font as text

% Declare the language mapping suffix
\DeclareLanguageMappingSuffix{-apa6}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Automated capitalisation after colons
%  (https://blog.apastyle.org/apastyle/2011/06/capitalization-after-colons.html)
%  Copied and adapted from ieee.bbx

% {<process>} {*} {<text>}
\newcommand*{\bbx@colon@search}[3]{%
  \bbx@colon@search@aux#1#2\empty#3: \stop
}
\long\def\bbx@colon@search@aux#1#2#3: #4\stop{%
  \expandafter#1\expandafter#2\expandafter{#3}%
  \ifblank{#4}
    {}
    {%
      : %
      \bbx@colon@search@aux#1#2\empty#4\stop
    }%
}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Used to join citations/references to
%  extradate

\newcommand{\apashortdash}{-}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Allow variable max authors/editors limit

\newcommand{\maxprtauth}{7}
\DeclareBibliographyOption{apamaxprtauth}{%
  \ifstrempty{#1}
    {}
    {\renewcommand{\maxprtauth}{#1}}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.22) Force roman numerals into arabic
%            using etoolbox macros

\newcommand{\apanum}[1]{\ifrmnum{#1}{\rmntonum{#1}}{#1}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Apa defines no particular hanging indent but this makes it look like the
% examples in the style manual.

\setlength{\bibhang}{2.5em}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

% Date formats. Suppress end range of less specific fields
\newcommand*{\mkdaterangeapalong}[1]{%
  \begingroup
    \blx@metadateinfo{#1}%
    \iffieldundef{#1year}{}
      {\datecircaprint
       \ifstrequal{#1}{url}% URL dates are unlikely to be BCE ...
         {\printtext{%
             \mkbibdateapalongmdy{#1year}{#1month}{#1day}%
             \iffieldundef{#1endyear}%
               {}%
               {\iffieldequalstr{#1endyear}{}% open-ended range?
                 {\mbox{\bibdatedash}}
                 {\bibdatedash%
                  \iffieldsequal{#1year}{#1endyear}%
                    {\iffieldsequal{#1month}{#1endmonth}%
                      {\iffieldsequal{#1day}{#1endday}%
                        {}%
                        {\mkbibdateapalongmdy{}{}{#1endday}}}%
                      {\mkbibdateapalongmdy{}{#1endmonth}{#1endday}}}%
                    {\mkbibdateapalongmdy{#1endyear}{#1endmonth}{#1endday}}}}%
             \dateuncertainprint}}
         {\printtext{%
             \mkbibdateapalong{#1year}{#1month}{#1day}%
             \dateeraprint{#1year}%
             \iffieldundef{#1endyear}%
               {}%
               {\iffieldequalstr{#1endyear}{}% open-ended range?
                 {\mbox{\bibdatedash}}
                 {\bibdatedash%
                  \iffieldsequal{#1year}{#1endyear}%
                    {\iffieldsequal{#1month}{#1endmonth}%
                      {\iffieldsequal{#1day}{#1endday}%
                        {}%
                        {\mkbibdateapalong{}{}{#1endday}}}%
                      {\mkbibdateapalong{}{#1endmonth}{#1endday}}}%
                    {\mkbibdateapalong{#1endyear}{#1endmonth}{#1endday}%
                   \dateeraprint{#1endyear}}}%
               \enddateuncertainprint}}}}%
  \endgroup}

% Only for DATE as only \printdateextra is used
\newcommand*{\mkdaterangeapalongextra}[1]{%
  \begingroup
    \blx@metadateinfo{#1}%
    \iffieldundef{#1year}{}
      {\printtext{%
        \datecircaprint
        \mkbibdateapalongextra{#1year}{#1month}{#1day}%
        \dateeraprint{#1year}%
        \dateuncertainprint
        \iffieldundef{#1endyear}%
          {}%
          {\iffieldequalstr{#1endyear}{}% open-ended range?
            {\mbox{\bibdatedash}}
            {\bibdatedash%
             \iffieldsequal{#1year}{#1endyear}%
               {\iffieldsequal{#1month}{#1endmonth}%
                  {\iffieldsequal{#1day}{#1endday}%
                    {}%
                    {\mkbibdateapalongextra{}{}{#1endday}}}
                  {\mkbibdateapalongextra{}{#1endmonth}{#1endday}}}
               {\mkbibdateapalongextra{#1endyear}{#1endmonth}{#1endday}%
                \dateeraprint{#1endyear}}}%
           \enddateuncertainprint}}}%
  \endgroup}

\AtEndPreamble{%
  \renewcommand*{\datecircaprint}{%
    \ifdatecirca{\biblcstring{circa}\printdelim{datecircadelim}}{}}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 7.09 Example 62) ERIC
% eprint references

\DeclareFieldFormat{eprint:eric}{%
  \bibcpstring{retrieved}%
  \addspace
  \bibstring{from}\addspace%
  ERIC\addspace database\adddot\addspace%
  \mkbibparens{#1}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.12) Five author max before "et al" and a one author truncation policy
%            However, only after the first cite, see the labelname format
%            in .cbx. It's also overridden per-entry by uniquelist
% (APA 6.x)  Citation tracking is global
% (APA 6.14) Use initials to disambiguate shared surnames
% (APA 6.16) Use year postfix to disambiguate multiple items in same year
% (APA 6.10) Never reference anything not cited
% (APA 6.25) Author initials only
% (APA 6.25) Sorting is nyt but we need to account for PUBSTATE which comes
%            after all normal cites for the same author

\DeclareSortingTemplate{apa}{
  \sort{
    \field{presort}
  }
  \sort[final]{
    \field{sortkey}
  }
  \sort{
    \field{sortname}
    \field{author}
    \field{editor}
    \field{translator}
    \field{writer}
    \field{director}
    \field{producer}
    \field{execproducer}
    \field{sorttitle}
    \field{title}
  }
  \sort{
    \field{pubstate}
  }
  \sort{
    \field{sortyear}
    \field{year}
    \literal{-2000000000}% APA has "nodates" coming first
  }
  \sort{
    \field{sorttitle}
    \field{title}
  }
  \sort{
    \field{volume}
    \literal{0}
  }
}

% Don't fall back on other year fields if there is no year, use
% "nodate" string
\DeclareLabeldate{%
  \field{pubstate}
  \field{date}
  \field{year}%legacy - no EDTF support
  \literal{nodate}
}

% Due to APA strange requirements like truncation after first cite and
% ellipsis from 7th to n-1 in bib, there might be some really strange
% edge cases which can't be handled as this needs treating in the style
% after biber has finished. Very unlikely though.
\ExecuteBibliographyOptions{%
                            abbreviate=true,%
                            autocite=inline,%
                            backref=false,%
                            citetracker=true,%
                            date=apalong,%
                            dateabbrev=false,%
                            datecirca=true,%
                            dateera=christian,%
                            labeldate=apalong,%
                            dateuncertain=true,%
                            eventdate=comp,%
                            labeldateparts=true,%
                            maxbibnames=7,%
                            maxcitenames=2,%
                            minbibnames=6,%
                            mincitenames=1,%
                            mincrossrefs=999,%
                            origdate=apalong,%
                            pagetracker=true,%
                            sortcites=true,%
                            sorting=apa,%
                            uniquelist=minyear,%
                            uniquename=full,%
                            uniqueprimaryauthor=true,%
                            urldate=apalong,%
                            useprefix=true,%
                            usetranslator=true}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Enforce ignoring of PUBSTATE if there is a YEAR or DATE field
\DeclareStyleSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \step[fieldsource=year, final]
      \step[fieldset=pubstate, null]
    }
    \map{
      \step[fieldsource=date, final]
      \step[fieldset=pubstate, null]
   }
  }
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.29) Additional material sometimes goes in parens
%            after title. This bool used to track the
%            parens. It is no longer needed and just kept
%            for backwarwds compatibility.

\newbool{bbx:parens}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 7.06:47) Reviews are awkward - if no author, date
%               position changes so we need a flag to
%               track this

\newbool{bbx:noreviewauthor}
\AtEveryBibitem{\global\boolfalse{bbx:noreviewauthor}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.30) Sometimes "Vol" is inside the additional
%            material parens, sometimes not. This bool
%            tracks if it has been inserted yet.
%            Can't use \clearfield{volume} as some
%            later number format tests need to know
%            whether volume was defined.

\newbool{bbx:volseen}
\AtEveryBibitem{\global\boolfalse{bbx:volseen}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.27) This bool tracks if the title was put
%            in the author position.
%            Can't use \clearfield{title} as some
%            later punctuation tests need to know
%            whether title was defined.

\newbool{bbx:titleinauthpos}
\AtEveryBibitem{\global\boolfalse{bbx:titleinauthpos}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.27) Need a flag to say when the editor
%            is in author position because this
%            can effect where the year goes.

\newbool{bbx:editorinauthpos}
\AtEveryBibitem{\global\boolfalse{bbx:editorinauthpos}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.27) Flag to say whether the "in" has
%            been placed already in IN*
%            Reset every bibitem.

\newbool{bbx:in}
\AtEveryBibitem{\global\boolfalse{bbx:in}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set up some standard APA formats

\DeclareFieldFormat{volume}{\apanum{#1}}
\DeclareFieldFormat{chapter}{\bibcpstring{chapter}~\apanum{#1}}
\DeclareFieldFormat{volumes}{\bibcpstring{volumes}~#1}
\DeclareFieldFormat{addendum}{\mkbibparens{#1}}
\DeclareFieldFormat{part}{#1}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.25) Works by the same author have the full author
%            name, not an eliding dash. Author is always
%            last name first.
% (APA 6.27) Ellipsis in 7th position and then nothing until last
% (APA 7.10 Example 67) Unknown names and dates
%

% \printnames does different things depending on whether the format you are
% calling is the default for the name field or is a custom format:
%
%   * Default format (e.g. "labelname" format for printing labelname): no
%     start/stop max/minnames truncation is done first - the format is
%     supposed to do it.
%   * Custom format (e.g. "labelname" format for printing author):
%     max/minnames truncation is done before calling the format so we have
%     to do \printnames[format][-\value{listtotal}]{field} to ensure we get
%     all of the names in the list to work on

\DeclareNameAlias{default}{apaauthor}

\DeclareNameFormat{apaauthor}{%
  \ifthenelse{\value{listcount}=\maxprtauth\AND\value{listcount}<\value{listtotal}}
    {\addcomma\space\ldots\space}
    {\ifthenelse{\value{listcount}>\maxprtauth\AND\value{listcount}<\value{listtotal}}
      {}
      {\iffieldannotation{uncertain}
        {\mkbibbrackets{\usebibmacro{name:apa:family-given}%
                         {\namepartfamily}%
                         {\namepartgiven}%
                         {\namepartgiveni}%
                         {\namepartprefix}%
                         {\namepartsuffix}?}}
        {\usebibmacro{name:apa:family-given}%
          {\namepartfamily}%
          {\namepartgiven}%
          {\namepartgiveni}%
          {\namepartprefix}%
          {\namepartsuffix}}}}%
    \ifthenelse{\value{listcount}=\value{listtotal}}%
      {\ifmorenames{\printdelim{andothersdelim}\bibstring{andothers}}{}}{}}

\DeclareDelimFormat[bib,biblist]{nameyeardelim}{\newunitpunct}
\DeclareDelimAlias*[bib,biblist]{nonameyeardelim}{nameyeardelim}

\renewbibmacro*{author}{%
  \ifnameundef{author}
    {\usebibmacro{labeltitle}%
     \setunit{\printdelim{nonameyeardelim}}}
    {\printnames[apaauthor][-\value{listtotal}]{author}%
     \setunit*{\addspace}%
     \printfield{nameaddon}%
     \ifnameundef{with}
       {}
       {\setunit{\addspace}%
        \printtext[parens]{%
          \bibstring{with}%
          \setunit{\addspace}%
          \printnames[apaauthor][-\value{listtotal}]{with}}%
        \setunit{\addspace}}%
     \setunit{\printdelim{nameyeardelim}}}%
  \newblock
  \usebibmacro{labelyear+extradate}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.27) References section delimiters are ampersands, not " and "
% (APA 6.27) Use blank for long lists
% (APA 4.03) Serial comma for lists of three or more

\DeclareDelimFormat[bib,biblist]{finalnamedelim}{%
  \ifthenelse{\value{listcount}>\maxprtauth}
    {}
    {\ifthenelse{\value{liststop}>2}
       {\finalandcomma\addspace\&\space}
       {\addspace\&\space}}}

\DeclareDelimFormat[bib,biblist]{finalnamedelim:apa:family-given}{%
  \ifthenelse{\value{listcount}>\maxprtauth}
    {}
    {\finalandcomma\addspace\&\space}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.28) Use "nodate" bibstring for references with no year
%            Months and days go into parenthesis with year, if set
% (APA 6.29) Issue goes where month normally goes if it
%            is set.
% (APA 7.10) circa or uncertain dates in brackets

% Make sure endmonth gets an expansion too
\DeclareFieldFormat{endmonth}{\mkbibmonth{#1}}

\newbibmacro*{monthorissue}{%
  \iffieldundef{issue}
    {\iffieldundef{month}
      {}
      {\addcomma\space\printfield{month}%
      \iffieldundef{endmonth}{}{/\printfield{endmonth}}}}
    {\addcomma\space\printfield{issue}}}

\newbibmacro*{labelyear+extradate}{%
  \iffieldundef{labelyear}
    {\iffieldundef{origyear}
      {}
      {\printtext[apadate]{\printorigdate}}}%
    {\printtext[apadate]{\printlabeldateextra}}}

\DeclareFieldFormat{apadate}{%
  \ifboolexpr{ test {\ifdatecirca} or test {\ifdateuncertain} }
    {\mkbibbrackets{#1}}
    {\mkbibparens{#1}}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.29) General format of titles.
%            Ugly Chicago-style lowercasing of English titles

\DeclareFieldFormat{apacase}{\bbx@colon@search\MakeSentenceCase*{#1}}

\DeclareFieldFormat{issuetitle}{#1\isdot}
\DeclareFieldFormat{title}{\iffieldequalstr{titleisdescription}{true}{\mkbibbrackets{#1}}{\mkbibemph{#1}\isdot}}
\DeclareFieldFormat{origtitle}{\mkbibemph{\bbx@colon@search\MakeSentenceCase*{#1}}\isdot}
\DeclareFieldFormat[article]{title}{#1\isdot}
\DeclareFieldFormat[article]{origtitle}{\bbx@colon@search\MakeSentenceCase*{#1}\isdot}
\DeclareFieldFormat[inbook]{title}{#1\isdot}
\DeclareFieldFormat[inbook]{origtitle}{\bbx@colon@search\MakeSentenceCase*{#1}\isdot}
\DeclareFieldFormat[incollection]{title}{#1\isdot}
\DeclareFieldFormat[incollection]{origtitle}{\bbx@colon@search\MakeSentenceCase*{#1}\isdot}
\DeclareFieldFormat[inproceedings]{title}{#1\isdot}
\DeclareFieldFormat[inproceedings]{origtitle}{\bbx@colon@search\MakeSentenceCase*{#1}\isdot}
\DeclareFieldFormat[proceedings]{title}{#1\isdot}
\DeclareFieldFormat[proceedings]{origtitle}{\bbx@colon@search\MakeSentenceCase*{#1}\isdot}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 7.01 Example 10) Newspapers have prefix for pages.

\DeclareFieldFormat[newsarticle]{pages}{%
    \DeclareNumChars{.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}%
    \mkpageprefix{#1}%
    \DeclareNumChars{.}}

\DeclareFieldFormat[newsarticle]{newspaper}{\mkbibemph{#1}}
\DeclareFieldFormat[newsarticle]{entrysubtype}{\mkbibbrackets{#1}}
\DeclareFieldFormat[newsarticle]{title}{#1}
\DeclareFieldFormat[article]{pages}{#1}

\newbibmacro*{newspaper}{%
  \printfield{newspaper}
  \setunit{\addcomma\space}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.29)      Additional {main}title information in brackets (using)
%                 {MAIN}TITLEADDON field. Colon after maintitle.

\DeclareFieldFormat{titleaddon}{\mkbibbrackets{\bbx@colon@search\MakeSentenceCase*{#1}}}
\DeclareFieldFormat{maintitleaddon}{\mkbibbrackets{\bbx@colon@search\MakeSentenceCase*{#1}}}
\DeclareFieldFormat{booktitleaddon}{\mkbibbrackets{\bbx@colon@search\MakeSentenceCase*{#1}}}

\newbibmacro*{bookaddinfo}{%
  \ifthenelse{\iffieldundef{edition}\AND\iffieldundef{volumes}}
    {}
    {\printtext[parens]{%
       \printfield{edition}%
       \setunit*{\addcomma\addspace}%
       \printfield{volumes}}}}

\renewbibmacro*{title}{%
  \ifthenelse{\iffieldundef{title}\AND\iffieldundef{subtitle}}
    {}
    {\iffieldundef{origtitle}
      {\printtext[title]{%
        \printfield[apacase]{title}%
        \setunit{\subtitlepunct}%
        \printfield[apacase]{subtitle}}}
      {\printfield{origtitle}%
       \setunit{\addspace}%
       \printtext[brackets]{%
        \printfield[apacase]{title}%
        \setunit{\subtitlepunct}%
        \printfield[apacase]{subtitle}}}%
     \setunit{\addspace}%
     \printfield{titleaddon}%
     \iffieldequalstr{entrytype}{book}%
       {\setunit{\addspace}\usebibmacro{bookaddinfo}}%
       {}%
     \ifthenelse{%
       \ifnameundef{author}\AND%
       \(\ifnameundef{editor}\AND\NOT\boolean{bbx:editorinauthpos}\)\AND%
       \ifnameundef{producer}\AND%
       \ifnameundef{director}\AND%
       \ifnameundef{writer}}
        {\setunit{\printdelim{nonameyeardelim}}\newblock
         \usebibmacro{labelyear+extradate}}
        {}}}

\renewbibmacro*{maintitle}{%
  \ifthenelse{\iffieldundef{maintitle}\AND\iffieldundef{mainsubtitle}}
    {}
    {\printtext[maintitle]{%
       \printfield[apacase]{maintitle}%
       \setunit{\subtitlepunct}%
       \printfield[apacase]{mainsubtitle}}%
    \setunit{\addspace}}
  \printfield{maintitleaddon}%
  \addcolon}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.30) Format of volume and number for articles

\DeclareFieldFormat[article]{volume}{\mkbibemph{\apanum{#1}}}
\DeclareFieldFormat[article]{number}{\mkbibparens{\apanum{#1}}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.30) Commas between title and volume for articles

\renewbibmacro*{journal+issuetitle}{%
  \usebibmacro{journal}%
  \setunit*{\addcomma\addspace}%
  \printfield{volume}%
  \printfield{number}%
  \setunit{\addspace}\newblock
  \usebibmacro{issuename}%
  \newunit}

\newbibmacro*{issuename}{%
  \iffieldundef{issuetitle}
    {}
    {\printtext[issuetitle]{%
       \printfield[noformat]{issuetitle}%
       \setunit{\subtitlepunct}%
       \printfield[noformat]{issuesubtitle}}}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.29) Additional information for non-periodicals in
%            parenthesis after title. This is ugly but it's
%            hard to put in "optional parens" like this
%            around an unknown amount of characters.

\newbibmacro*{addinfo}{%
  \ifthenelse{\iffieldundef{edition}\AND%
              \iffieldundef{chapter}\AND%
              \iffieldundef{volumes}\AND%
              \iffieldundef{pages}\AND%
              \iffieldundef{number}\AND%
              \(\iffieldundef{volume}\OR\boolean{bbx:volseen}\)}
  {}
  {\printtext[parens]{%
     \printfield{edition}%
     \setunit*{\addcomma\addspace}%
     \printfield{chapter}%
     \setunit*{\addcomma\addspace}%
     \notbool{bbx:volseen}%
       {\iffieldundef{volume}
          {}
          {\global\booltrue{bbx:volseen}}%
        \printfield{volume}%
        \iffieldundef{part}
          {}
          {\printfield{part}}}
       {}%
     \setunit*{\addcomma\addspace}%
     \printfield{volumes}%
     \setunit*{\addcomma\addspace}%
     \printfield{pages}}}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.27) "In " precedes editor/trans list, no colon

\newbibmacro*{in}{%
  \ifbool{bbx:in}%
    {}%
    {\global\booltrue{bbx:in}%
     \bibcpstring{in}\setunit{\space}}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.27) Editors have first names first ...
% (APA 7.02 Example 27) ... unless there is no author

\DeclareNameFormat{apanames}{%
  \ifthenelse{\value{listcount}=\maxprtauth\AND\value{listcount}<\value{listtotal}}
    {\addcomma\space\ldots\space}
    {\ifthenelse{\value{listcount}>\maxprtauth\AND\value{listcount}<\value{listtotal}}
      {}
      {\usebibmacro{name:apa:given-family}%
        {\namepartfamily}%
        {\namepartgiven}%
        {\namepartgiveni}%
        {\namepartprefix}%
        {\namepartsuffix}}}%
    \ifthenelse{\value{listcount}=\value{listtotal}}%
      {\ifmorenames{\printdelim{andothersdelim}\bibstring{andothers}}{}}{}}

\renewbibmacro*{author/editor}{%
  \ifnameundef{author}
    {\ifnameundef{editor}
      {}
      {\usebibmacro{editorinauthpos}}}
    {\global\boolfalse{bbx:editorinauthpos}%
     \usebibmacro{author}}}

\newbibmacro*{editorinauthpos}{%
    \global\booltrue{bbx:editorinauthpos}%
    \printnames[apaauthor][-\value{listtotal}]{editor}%
    \setunit{\addspace}%
    \ifnameundef{editor}
      {}
      {\printtext[parens]{\usebibmacro{apaeditorstrg}{editor}}%
       % need to clear editor so we don't get an "In" clause later
       % But we also need to set a flag to say we did this so we
       % don't lose sight of the fact we once had an editor for
       % various year placement tests
       \clearname{editor}%
       \setunit{\printdelim{nameyeardelim}}%
       \usebibmacro{labelyear+extradate}%
       \newunit}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.27) Name format. Don't capitalise prefixes
% (APA 6.27) Full name disambiguation using brackets

% #1 = family name
% #2 = given name
% #3 = given name (initials)
% #4 = name prefix
% #5 = name suffix

\newbibmacro*{name:delim:apa:family-given}[1]{%
  \ifnumgreater{\value{listcount}}{\value{liststart}}
    {\ifboolexpr{
       test {\ifnumless{\value{listcount}}{\value{liststop}}}
       or
       test \ifmorenames
     }
       {\printdelim{multinamedelim}}
       {\printdelim{finalnamedelim:apa:family-given}}}
    {}}

\newbibmacro*{name:apa:family-given}[5]{%
  \ifuseprefix
    {\usebibmacro{name:delim:apa:family-given}{#4#1}%
     \usebibmacro{name:hook}{#4#1}%
     \ifdefvoid{#4}{}{%
       \mkbibnameprefix{#4}\isdot%
       \ifprefchar{}{\bibnamedelimc}}%
     \mkbibnamefamily{#1}\isdot%
     \ifdefvoid{#2}{}{\revsdnamepunct\bibnamedelimd\mkbibnamegiven{#3}\isdot%
                      \ifthenelse{\value{uniquename}>1}
                        {\bibnamedelimd\mkbibbrackets{#2}}
                        {}}%
     \ifdefvoid{#5}{}{\addcomma\bibnamedelimd\mkbibnamesuffix{#5}\isdot}}
    {\usebibmacro{name:delim:apa:family-given}{#1}%
     \usebibmacro{name:hook}{#1}%
     \mkbibnamefamily{#1}\isdot
     \ifboolexpe{%
       test {\ifdefvoid{#2}}
       and
       test {\ifdefvoid{#4}}}
       {}
       {\revsdnamepunct}%
     \ifdefvoid{#2}{}{\bibnamedelimd\mkbibnamegiven{#3}%
                      \ifthenelse{\value{uniquename}>1}
                        {\bibnamedelimd\mkbibbrackets{#2}}
                        {}}%
     \ifdefvoid{#4}{}{%
       \bibnamedelimc\mkbibnameprefix{#4}%
       \ifprefchar{}{\bibnamedelimc}}%
     \ifdefvoid{#5}{}{\addcomma\bibnamedelimd\mkbibnamesuffix{#5}\isdot}}}

\newbibmacro*{name:apa:given-family}[5]{%
  \usebibmacro{name:delim}{#2#4#1#5}%
  \usebibmacro{name:hook}{#2#4#1#5}%
  \ifdefvoid{#2}{}{\mkbibnamegiven{#3}\isdot%
                   \ifthenelse{\value{uniquename}>1}
                     {\bibnamedelimd\mkbibbrackets{#2}}
                     {}%
                   \bibnamedelimd}%
  \ifdefvoid{#4}{}{%
    \mkbibnameprefix{#4}\isdot
    \ifprefchar{}{\bibnamedelimc}}%
  \mkbibnamefamily{#1}\isdot%
  \ifdefvoid{#5}{}{\bibnamedelimd\mkbibnamesuffix{#5}\isdot}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.27) Editor string in parens after author list
% (APA 7.02 Example 21) Collapse editor and trans if same
% (APA 7.02 Example 26) Translator


% Separated out into book and in* macros because otherwise
% this makes one ugly, huge, unreadable beast.

\newbibmacro*{book:editor+trans}{%
  \ifthenelse{\ifnameundef{editor}\AND%
              \ifnameundef{editora}\AND%
              \ifnameundef{editorb}\AND%
              \ifnameundef{editorc}\AND%
              \ifnameundef{translator}}%
    {}%
    {\ifnamesequal{editor}{translator}%
       {\printtext[parens]{%
          \printnames[apanames][-\value{listtotal}]{editor}%
          \setunit*{\addcomma\addspace}%
          \usebibmacro{apaeditorstrg}{editor}%
          \setunit*{\addspace\&\space}%
          \bibcpstring{translator}}}
       {\printtext[parens]{%
          \ifnameundef{editor}%
            {}%
            {\printnames[apanames][-\value{listtotal}]{editor}%
             \setunit{\addcomma\addspace}%
             \usebibmacro{apaeditorstrg}{editor}%
             \clearname{editor}%
             \setunit{\adddot}%
             \setunit*{\addspace\&\space}}%
          \ifnameundef{translator}%
            {\setunit{}}%
            {\printnames[apanames][-\value{listtotal}]{translator}%
             \setunit{\addcomma\addspace}%
             \bibcpstring{translator}%
             \clearname{translator}%
             \setunit{\adddot}}}}}}

\newbibmacro*{editor+trans}{%
  \ifthenelse{\ifnameundef{editor}\AND%
              \ifnameundef{editora}\AND%
              \ifnameundef{editorb}\AND%
              \ifnameundef{editorc}\AND%
              \ifnameundef{translator}}%
    {\setunit{\adddot\addspace}}%
    {\ifnamesequal{editor}{translator}%
      {\usebibmacro{in}%
       \printnames[apanames][-\value{listtotal}]{editor}%
       \setunit{\addspace}
       \printtext[parens]{%
         \usebibmacro{apaeditorstrg}{editor}%
         \setunit*{\addspace\&\space}%
         \bibcpstring{translator}}}
      {\ifnameundef{translator}%
        {}%
        {\setunit{\addspace}%
         \printtext[parens]{\printnames[apanames][-\value{listtotal}]{translator}%
         \setunit{\addcomma\addspace}%
         \bibcpstring{translator}}%
         \clearname{translator}%
         \setunit{\adddot\addspace}}%
       \usebibmacro{in}%
       \usebibmacro{editorx}}%
     \setunit{\addcomma\addspace}}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.29) Special journal issues are periodicals

\DeclareFieldFormat[periodical]{pages}{#1}
\DeclareFieldFormat[periodical]{title}{#1\isdot}
\DeclareFieldFormat[periodical]{issuetitle}{\mkbibbrackets{#1}}
\DeclareFieldFormat[periodical]{volume}{\mkbibemph{\apanum{#1}}}
\DeclareFieldFormat[periodical]{number}{\mkbibparens{\apanum{#1}}}
\DeclareListFormat[periodical]{location}{\mkbibemph{#1}}

\renewbibmacro*{periodical}{%
  \iffieldundef{title}
    {}
    {\printtext[title]{%
       \printfield[apacase]{title}%
       \setunit{\subtitlepunct}%
       \printfield[apacase]{subtitle}}}}

\renewbibmacro*{title+issuetitle}{%
  \usebibmacro{periodical}%
  \setunit{\addspace}%
  \printfield{issuetitle}%
  \notbool{bbx:editorinauthpos}
    {\setunit{\printdelim{nonameyeardelim}}%
     \usebibmacro{labelyear+extradate}}
    {}}

\newbibmacro*{pnumber}{%
  \printfield{volume}%
  \printfield{number}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 7.02 Example 21) Original year

% Only give ORIGYEAR in references if both:
%   YEAR is also given
%   YEAR and ORIGYEAR are different

\newbibmacro*{origyear}{%
  \ifboolexpr{%
    not test {\iffieldundef{origyear}}
    not test {\iffieldundef{labelyear}}
    and not test {\iffieldsequal{labelyear}{origyear}}}
    {\printtext[parens]{%
       \bibcpstring{origyear}
       \setunit{\addnbspace}%
       \printorigdate}}
    {}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 7.02) "In " before booktitle, edited or not
% (APA 6.30) Non-periodical volume number followed by "."

\renewbibmacro*{maintitle+title}{%
  \iffieldsequal{maintitle}{title}
    {\clearfield{maintitle}%
     \clearfield{mainsubtitle}%
     \clearfield{maintitleaddon}}
    {\iffieldundef{maintitle}
      {}
      {\usebibmacro{maintitle}%
       \newunit\newblock
       \iffieldundef{volume}
         {}
         {\setunit{\global\booltrue{bbx:volseen}}%
          \printfield{volume}%
          \printfield{part}%
          \setunit{\adddot\space}}}}%
  \usebibmacro{title}%
  \newunit}

\renewbibmacro*{maintitle+booktitle}{%
  \ifthenelse{\iffieldundef{maintitle}\AND\iffieldundef{booktitle}}
    {}
    {\usebibmacro{in}%
     \iffieldundef{maintitle}
      {}
      {\usebibmacro{maintitle}%
       \newunit\newblock
       \iffieldundef{volume}
         {}
         {\setunit{\addspace\global\booltrue{bbx:volseen}}%
          \printfield{volume}%
          \printfield{part}%
          \setunit{\adddot\addspace}}}%
    \usebibmacro{booktitle}}}

\renewbibmacro*{booktitle}{%
  \ifthenelse{\iffieldundef{booktitle}\AND\iffieldundef{booksubtitle}}
    {}
    {\printtext[booktitle]{%
       \printfield[apacase]{booktitle}%
       \setunit{\subtitlepunct}%
       \printfield[apacase]{booksubtitle}}%
     \setunit{\addspace}}%
  \printfield{booktitleaddon}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.29) Colon separates title/subtitle
%            Skip if following terminating punctuation

\renewcommand*{\subtitlepunct}{\ifterm{}{\addcolon}\space}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.30) Format of volume depends on whether
%            there is a maintitle (what APA calls "series") or not.

\DeclareFieldFormat{volume}{\iffieldundef{maintitle}
                              {\bibcpstring{volume}~\apanum{#1}}
                              {\mkbibemph{\bibcpstring{volume}~\apanum{#1}}}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.30) Number for reports with no prefix if not
%            a numeral. Report number is optional and so must be
%            the parens.

\DeclareFieldFormat{number}{\iffieldundef{volume}
                             {\ifinteger{\thefield{number}}
                               {\addspace\mkbibemph{\bibcpstring{number}~\apanum{#1}}\adddot}
                               {\apanum{#1}}}
                             {\mkbibemph{\apanum{#1}}}}

\DeclareFieldFormat[report]{number}{\mkbibparens{\iffieldundef{type}
                                                  {}
                                                  {\printfield{type}\setunit{\addspace}}%
                                                  \bibcpstring{number}~\apanum{#1}}}

\newbibmacro*{apa:reportnum}{%
  \iffieldundef{number}%
    {}%
    {\printfield{number}}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 6.30) Location only shows first item

\DeclareListFormat{default}{%
  \usebibmacro{list:delim}{#1}%
  #1\isdot}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 7.02 Example 38) Proceedings emph number, location
%                       and location. Pages have no prefix

\DeclareListFormat[proceedings]{location}{\mkbibemph{#1}}
\DeclareFieldFormat[proceedings]{pages}{#1}
\DeclareFieldFormat[proceedings]{number}{\mkbibemph{\apanum{#1}}}
\DeclareFieldFormat[proceedings]{volume}{\mkbibemph{\apanum{#1}}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 7.02 Example 40--44) Theses titles and volume
%                           Titles are not emph if only an abstract

\DeclareFieldFormat[thesis]{title}{\mkbibemph{#1}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 7.05) Unpublished theses

\DeclareFieldFormat[unpublished]{title}{\mkbibemph{#1}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 7.06) Reviews

\newcommand*{\begrelateddelimreviewof}{\addspace}
\DeclareFieldFormat[review]{title}{#1}
\DeclareFieldFormat[review]{pages}{#1}
\DeclareFieldFormat[review]{volume}{\mkbibemph{\apanum{#1}}}
\DeclareFieldFormat[review]{number}{\mkbibparens{\apanum{#1}}}

\newbibmacro*{reviewauthor}{%
  \ifnameundef{author}
    {\booltrue{bbx:noreviewauthor}}
    {\usebibmacro{author}}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Media authors

\newbibmacro*{producer}{%
  \ifnameundef{producer}
    {}
    {\printnames[apaauthor][-\value{listtotal}]{producer}%
     \addspace
     \printtext[parens]{\bibcpstring{producer}}%
     \setunit*{\addcomma\addspace\&\addspace}}}

\newbibmacro*{director}{%
  \ifnameundef{director}
    {}
    {\printnames[apaauthor][-\value{listtotal}]{director}%
     \addspace
     \printtext[parens]{\bibcpstring{director}}%
     \setunit*{\addcomma\addspace\&\addspace}}}

\newbibmacro*{writer}{%
  \ifnameundef{writer}
    {}
    {\printnames[apaauthor][-\value{listtotal}]{writer}%
     \addspace
     \printtext[parens]{\bibcpstring{writer}}%
     \setunit*{\addcomma\addspace\&\addspace}}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%
% (APA 7.07) Misc and data

\DeclareFieldFormat[data]{title}{\mkbibemph{#1}}
\DeclareFieldFormat[data]{entrysubtype}{\mkbibbrackets{#1}}
\DeclareFieldFormat[misc]{entrysubtype}{\mkbibbrackets{#1}}
\DeclareFieldFormat[misc]{nameaddon}{\mkbibparens{#1}}
\DeclareFieldFormat[misc]{title}{\iffieldequalstr{titleisdescription}{true}{\mkbibbrackets{#1}}{{#1}\isdot}}

\newbibmacro*{datatitle}{%
  \iffieldundef{title}
    {\iffieldundef{entrysubtype}
      {}
      {\printfield{entrysubtype}}}
    {\iffieldundef{entrysubtype}
      {}
      {\usebibmacro{title}\addspace
       \printfield{entrysubtype}}}}

%
%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 7.07) Audiovisual

\DeclareFieldFormat[video]{title}{\iffieldundef{maintitle}{\mkbibemph{#1}}{#1}}
\DeclareFieldFormat[video]{maintitle}{\mkbibemph{#1}}
\DeclareFieldFormat[video]{entrysubtype}{\mkbibbrackets{#1}}
\DeclareFieldFormat[audio]{entrysubtype}{\mkbibbrackets{#1}}
\DeclareFieldFormat[music]{title}{#1}
\DeclareFieldFormat[music]{maintitle}{\mkbibemph{#1}}
\DeclareFieldFormat[music]{mainsubtitle}{\mkbibemph{#1}}
\DeclareFieldFormat[audio]{howpublished}{\mkbibparens{#1}}

\newbibmacro*{tvseries}{%
  \iffieldundef{maintitle}
    {}
    {\usebibmacro{in}%
     \ifnameundef{execproducer}
       {}
       {\printnames[apanames][-\value{listtotal}]{execproducer}%
         \addspace
         \printtext[parens]{\bibcpstring{execproducer}}}
     \setunit{\addcomma\addspace}%
     \usebibmacro{avmaintitle}%
     \newunit}}

\newbibmacro*{avmaintitle}{%
  \ifthenelse{\iffieldundef{maintitle}\AND\iffieldundef{mainsubtitle}}
    {}
    {\printtext[maintitle]{%
       \printfield[apacase]{maintitle}%
       \setunit{\subtitlepunct}%
       \printfield[apacase]{mainsubtitle}}%
    \setunit{\addspace}}
  \printfield{maintitleaddon}}

\newbibmacro*{album}{%
  \iffieldundef{maintitle}
    {}
    {\bibcpstring{on}%
     \setunit{\addspace}%
     \usebibmacro{avmaintitle}%
     \newunit}}


%
%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%
% (APA 7.10) Letter/Letters

\DeclareFieldFormat[letter]{title}{\mkbibbrackets{#1}}
\DeclareFieldFormat[letters]{title}{#1}
\DeclareFieldFormat[letter]{number}{\mkbibparens{#1}}
\DeclareFieldFormat[letters]{number}{\mkbibparens{#1}}

%
%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%
% (APA 6.31) URLs

\DeclareFieldFormat{url}{\url{#1}}
\DeclareFieldFormat{abstracturl}{\url{#1}}
\DeclareFieldFormat{urldate}{#1}

\renewbibmacro*{url+urldate}{%
  \ifthenelse{\(\iffieldundef{url}\AND\iffieldundef{abstracturl}\AND\iffieldundef{abstractloc}\)\OR\NOT\iffieldundef{doi}}
    {}
    {\ifthenelse{\iffieldundef{abstracturl}\AND\iffieldundef{abstractloc}}
       {}
       {\bibcpstring{abstract}%
        \setunit{\addspace}}%
     \iffieldequalstr{entrysubtype}{{DVD}}
       {\bibstring{available}}
       {\bibstring{retrieved}}%
     \setunit{\addspace}%
     \iffieldundef{urlyear}
       {}
       {\printurldate
        \setunit{\urldatecomma}}%
     \bibstring{from}%
     \setunit{\addspace}%
     \printfield{urldescription}%
     \setunit*{\addcolon\addspace}%
     \iffieldundef{url}{}{\printfield{url}\renewcommand*{\finentrypunct}{\relax}}%
     \iffieldundef{abstractloc}{}{\printfield{abstractloc}\renewcommand*{\finentrypunct}{\relax}}%
     \iffieldundef{abstracturl}{}{\printfield{abstracturl}\renewcommand*{\finentrypunct}{\relax}}}}

%
%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 7.11) Non-emph titles for online items
%            Subtype in brackets


\DeclareFieldFormat[online]{title}{#1}
\DeclareFieldFormat[online]{entrysubtype}{\mkbibbrackets{#1}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA A7.07) Emph titles for patents

\DeclareFieldFormat[patent]{title}{\mkbibemph{#1}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 7.08 Example 56) Software has version in title

\DeclareFieldFormat[software]{title}{#1}
\DeclareFieldFormat[software]{version}{\mkbibparens{\bibcpstring{version}~#1}}

\newbibmacro*{apa:softwaretitle}{%
  \printtext[title]{%
  \printfield[apacase]{title}%
  \setunit{\subtitlepunct}%
  \printfield[apacase]{subtitle}}%
  \setunit{\addspace}%
  \iffieldundef{version}%
     {}
     {\printfield{version}}
  \printfield{titleaddon}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Hacky bits to suppress final period in some cases.
% * If there is an ORIGYEAR field (APA 7.02:21)
% * Unless there is a RELATED and ADDENDUM
\newbibmacro*{apa:finpunct}{%
  \iffieldundef{origyear}
    {\ifboolexpr{
       test {\iffieldundef{addendum}}
       and
       test {\iffieldundef{related}}}
     {}
     {\renewcommand*{\finentrypunct}{\relax}}}
    {\renewcommand*{\finentrypunct}{\relax}}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Related entries

\DeclareFieldFormat{related:reprintfrom}{\mkbibparens{#1}}
\DeclareFieldFormat{related:reviewof}{\mkbibbrackets{#1}}

\renewbibmacro*{related:reprintfrom}[1]{%
  \entrydata*{#1}{%
    \printtext[title]{\printfield[apacase]{title}}%
    \setunit{\bibpagespunct}%
    \printfield{pages}%
    \setunit{\addcomma\addspace}%
    \bibstring{byauthor}\addspace
    \ifnameundef{editor}
      {}
      {\printnames[apanames][-\value{listtotal}]{editor}%
       \setunit{\addcomma\addspace}%
       \usebibmacro{apaeditorstrg}{editor}%
       \setunit{\addcomma\addspace}%
       \printlabeldateextra
       \setunit{\addcomma\addspace}}%
    \printnames[apanames][-\value{listtotal}]{author}%
    \setunit{\addcomma\addspace}%
    \usebibmacro{location+publisher}%
    \newunit\newblock
    \usebibmacro{related}}}

\newbibmacro*{related:reviewof}[1]{%
  \setunit{}% Sanitise this in case no author
  \entrydata*{#1}{%
    \printtext[title]{\printfield[apacase]{title}}%
    \setunit{\addspace}%
    \bibstring{byauthor}\addspace
    \printnames[apanames][-\value{listtotal}]{author}%
    \printnames[apanames][-\value{listtotal}]{producer}%
    \printnames[apanames][-\value{listtotal}]{director}%
    \setunit{\addcomma\addspace}%
    \printdateextra
    \newunit\newblock
    \usebibmacro{related}}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 7.x) General type layouts

\DeclareBibliographyDriver{article}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/editor}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \newunit\newblock
  \usebibmacro{journal+issuetitle}%
  \setunit{\bibpagespunct}%
  \printfield{pages}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{pageref}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{newsarticle}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/editor}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \newunit\newblock
  \usebibmacro{newspaper}%
  \setunit{\bibpagespunct}%
  \printfield{pages}%
  \newunit\newblock
  \printfield{entrysubtype}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{pageref}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{book}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/editor}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{maintitle+title}%
  \setunit{\addspace}\newblock
  \usebibmacro{book:editor+trans}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{location+publisher}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \usebibmacro{origyear}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{booklet}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/editor}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \newunit\newblock
  \printfield{howpublished}%
  \newunit\newblock
  \printfield{type}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{location+publisher}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{proceedings}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/editor}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \newunit\newblock
  \usebibmacro{booktitle}%
  \setunit{\addcomma\addspace}\newblock
  \usebibmacro{location+publisher}%
  \setunit{\addcomma\addspace}%
  \printfield{volume}%
  \setunit*{\adddot}%
  \printfield{number}%
  \setunit{\addcomma\addspace}
  \printfield{pages}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \printlist{organization}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{collection}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/editor}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \newunit\newblock
  \usebibmacro{location+publisher}%
  \setunit{\addcomma\addspace}%
  \printfield{number}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{inbook}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \ifthenelse{\NOT\iffieldundef{title}\OR\boolean{bbx:titleinauthpos}}{\newunit}{\setunit{\addspace}}\newblock
  \usebibmacro{editor+trans}%
  \newblock
  \usebibmacro{maintitle+booktitle}%
  \newblock
  \usebibmacro{addinfo}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{location+publisher}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \setunit*{\addspace}\newblock
  \usebibmacro{origyear}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{incollection}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \ifthenelse{\NOT\iffieldundef{title}\OR\boolean{bbx:titleinauthpos}}{\newunit}{\setunit{\addspace}}\newblock
  \usebibmacro{editor+trans}%
  \setunit*{\addcomma\addspace}\newblock
  \usebibmacro{maintitle+booktitle}%
  \newblock
  \usebibmacro{addinfo}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{location+publisher}%
  \newunit\newblock
  \usebibmacro{origyear}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{inproceedings}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \ifthenelse{\NOT\iffieldundef{title}\OR\boolean{bbx:titleinauthpos}}{\newunit}{\setunit{\addspace}}\newblock
  \usebibmacro{editor+trans}%
  \setunit*{\addcomma\addspace}\newblock
  \usebibmacro{maintitle+booktitle}%
  \iffieldundef{eventyear}{}{\setunit{\addcomma\addspace}}%
  \printeventdate
  \setunit*{\addspace}\newblock
  \usebibmacro{addinfo}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \printlist{organization}%
  \newunit
  \printfield[apacase]{eventtitle}%
  \newunit
  \printfield{venue}%
  \iffieldundef{pages}
    {\setunit{\addcomma\addspace}}
    {\newunit}%
  \newblock
  \usebibmacro{location+publisher}%
  \newunit\newblock
  \usebibmacro{origyear}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{manual}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/editor}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \newunit\newblock
  \printfield{edition}%
  \newunit\newblock
  \printfield{type}%
  \newunit
  \printfield{version}%
  \newunit
  \printfield{note}%
  \newunit\newblock
  \printlist{organization}%
  \newunit
  \usebibmacro{location+publisher}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{online}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \ifthenelse{\iffieldundef{title}\AND\boolean{bbx:titleinauthpos}}{\newunit}{\setunit{\addspace}}\newblock
  \printfield{entrysubtype}%
  \addperiod\addspace
  \iftoggle{bbx:doi}
    {\printfield{doi}%
     \iffieldundef{doi}{}{\renewcommand*{\finentrypunct}{\relax}}}
    {}%
  \newunit\newblock
  \iftoggle{bbx:eprint}
    {\usebibmacro{eprint}%
     \iffieldundef{eprint}{}{\renewcommand*{\finentrypunct}{\relax}}}
    {}%
  \newunit\newblock
  \usebibmacro{url+urldate}%
  \iffieldundef{url}{}{\renewcommand*{\finentrypunct}{\relax}}
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{patent}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \ifthenelse{\NOT\iffieldundef{title}\OR\boolean{bbx:titleinauthpos}}{\newunit}{\setunit{\addspace}}\newblock
  \setunit*{\addspace}%
  \printfield{number}%
  \newunit\newblock
  \usebibmacro{location+publisher}%
  \newunit\newblock
  \usebibmacro{byholder}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{periodical}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/editor}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title+issuetitle}%
  \setunit{\adddot\addspace}
  \usebibmacro{journal}%
  \setunit{\addcomma\addspace}
  \printlist{location}%
  \setunit{\addcomma\addspace}
  \usebibmacro{pnumber}%
  \setunit{\addcomma\addspace}
  \printfield{pages}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{report}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/editor}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \usebibmacro{apa:reportnum}%
  \newunit\newblock
  \printlist{institution}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{location+publisher}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{thesis}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \ifthenelse{\NOT\iffieldundef{title}\OR\boolean{bbx:titleinauthpos}}{\newunit}{\setunit{\addspace}}\newblock
  \usebibmacro{type+institution}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{review}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{reviewauthor}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \setunit{\printdelim{nonameyeardelim}}\newblock
  \ifbool{bbx:noreviewauthor}{\usebibmacro{labelyear+extradate}}{}%
  \newunit\newblock
  \usebibmacro{journal+issuetitle}%
  \newunit\newblock
  \printfield{note}%
  \setunit{\bibpagespunct}%
  \printfield{pages}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{misc}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \ifthenelse{\iffieldundef{title}\AND\boolean{bbx:titleinauthpos}}{\newunit}{\setunit{\addspace}}\newblock
  \printfield{entrysubtype}%
  \newunit\newblock
  \printfield{howpublished}%
  \newunit\newblock
  \printfield{type}%
  \newunit
  \printfield{version}%
  \newunit
  \printfield{note}%
  \newunit\newblock
  \printlist{organization}%
  \newunit\newblock
  \printlist{institution}%
  \setunit*{\addcomma\addspace}%
  \usebibmacro{location+publisher}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{data}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{datatitle}%
  \newunit\newblock
  \printfield{type}%
  \newunit
  \printfield{version}%
  \newunit
  \printfield{note}%
  \newunit\newblock
  \printlist{organization}%
  \newunit\newblock
  \usebibmacro{location+publisher}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{letter}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/editor}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \newunit\newblock
  \printlist{institution}%
  \setunit{\addspace}\newblock
  \printfield{number}%
  \newunit\newblock
  \printfield{note}%
  \setunit{\addcomma\addspace}\newblock
  \usebibmacro{location+publisher}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{letters}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/editor}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \newunit\newblock
  \printlist{institution}%
  \setunit{\addspace}\newblock
  \printfield{number}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{location+publisher}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{video}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{writer}%
  \usebibmacro{director}%
  \usebibmacro{producer}%
  \setunit{\printdelim{nameyeardelim}}\newblock
  \usebibmacro{labelyear+extradate}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \setunit{\addspace}\newblock
  \printfield{entrysubtype}%
  \newunit\newblock
  \usebibmacro{tvseries}%
  \newunit\newblock
  \usebibmacro{location+publisher}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{movie}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{writer}%
  \usebibmacro{director}%
  \usebibmacro{producer}%
  \setunit{\printdelim{nameyeardelim}}\newblock
  \usebibmacro{labelyear+extradate}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \setunit{\addspace}\newblock
  \printfield{entrysubtype}%
  \newunit\newblock
  \usebibmacro{tvseries}%
  \newunit\newblock
  \usebibmacro{location+publisher}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{audio}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{writer}%
  \usebibmacro{director}%
  \usebibmacro{producer}%
  \setunit{\printdelim{nameyeardelim}}\newblock
  \usebibmacro{labelyear+extradate}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \setunit{\addspace}\newblock
  \printfield{entrysubtype}%
  \newunit\newblock
  \usebibmacro{location+publisher}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{music}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \ifthenelse{\NOT\iffieldundef{title}\OR\boolean{bbx:titleinauthpos}}{\newunit}{\setunit{\addspace}}\newblock
  \usebibmacro{album}%
  \newunit\newblock
  \usebibmacro{location+publisher}%
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{software}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/editor}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{apa:softwaretitle}%
  \newunit\newblock
  \usebibmacro{location+publisher}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}%
  \usebibmacro{finentry}}

\DeclareBibliographyDriver{unpublished}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/editor}%
  \setunit{\printdelim{nametitledelim}}\newblock
  \usebibmacro{title}%
  \newunit\newblock
  \printfield{howpublished}%
  \iflistundef{institution}
    {\newunit}
    {\setunit{\addcomma\addspace}}%
  \newblock
  \printlist{institution}%
  \setunit*{\addcomma\addspace}\newblock
  \printfield{note}%
  \setunit*{\addcomma\addspace}\newblock
  \printlist{location}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \printfield{addendum}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{pageref}%
  \usebibmacro{apa:finpunct}
  \usebibmacro{finentry}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Modified, common macros

\newbibmacro*{editorx}{%
  \ifnameundef{editor}
    {}
    {\printnames[apanames][-\value{listtotal}]{editor}%
     \setunit{\addspace}%
     \printtext[parens]{\usebibmacro{apaeditorstrg}{editor}}
     \clearname{editor}%
     \newunit}%
  \ifnameundef{editora}
    {}
    {\printnames[apanames][-\value{listtotal}]{editora}%
     \setunit{\addspace}%
     \printtext[parens]{\usebibmacro{apaeditorstrg}{editora}}
     \clearname{editora}%
     \newunit}%
  \ifnameundef{editorb}
    {}
    {\printnames[apanames][-\value{listtotal}]{editorb}%
     \setunit{\addspace}%
     \printtext[parens]{\usebibmacro{apaeditorstrg}{editorb}}
     \clearname{editorb}%
     \newunit}%
  \ifnameundef{editorc}
    {}
    {\printnames[apanames][-\value{listtotal}]{editorc}%
     \setunit{\addspace}%
     \printtext[parens]{\usebibmacro{apaeditorstrg}{editorc}}
     \clearname{editorc}%
     \newunit}}

\newbibmacro*{apaeditorstrg}[1]{%
  \iffieldundef{#1type}
    {\ifthenelse{\value{#1}>1\OR\ifandothers{#1}}
       {\bibcpstring{editors}}
       {\bibcpstring{editor}}}
    {\ifthenelse{\value{#1}>1\OR\ifandothers{#1}}
         {\bibcpstring{type\thefield{#1type}s}}
         {\bibcpstring{type\thefield{#1type}}}}}

\renewbibmacro*{doi+eprint+url}{%
  \iftoggle{bbx:doi}
    {\printfield{doi}%
     \iffieldundef{doi}{}{\renewcommand*{\finentrypunct}{\relax}}}
    {}%
  \newunit\newblock
  \iftoggle{bbx:eprint}
    {\usebibmacro{eprint}%
     \iffieldundef{eprint}{}{\renewcommand*{\finentrypunct}{\relax}}}
    {}%
  \newunit\newblock
  \iftoggle{bbx:url}
    {\usebibmacro{url+urldate}%
     \iffieldundef{url}{}{\renewcommand*{\finentrypunct}{\relax}}}
    {}}


\DeclareFieldFormat{doi}{%
  doi\addcolon
  \ifhyperref
    {\href{https://doi.org/#1}{\nolinkurl{#1}}}
    {\nolinkurl{#1}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (APA 7.02) No LOCATION/PUBLISHER if there is a DOI
%            (Inconsistent in 2nd printing, see 7.04:39 which
%            is incorrect in 2nd printing)

\newbibmacro*{location+publisher}{%
  \iffieldundef{doi}
    {\printlist[default][1-1]{location}%
     \setunit*{\addcolon\space}%
     \printlist{publisher}%
     \newunit}
    {}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newbibmacro*{type+institution}{%
  \setunit{\addspace}%
  \ifboolexpr{ test {\iffieldundef{type}} and test {\iflistundef{institution}} }
    {}
    {\printtext[parens]{%
       \iflistundef{institution}
         {\setunit*{\addspace}}
         {\setunit*{\addcolon\space}}%
       \printfield{type}%
       \setunit*{\addcomma\space}%
       \printlist{institution}%
       \setunit*{\addcomma\space}%
       \printlist{location}}%
     \newunit}}

\newbibmacro*{labeltitle}{%
  \iffieldundef{label}
    {\printfield{title}%
     \clearfield{title}%
     \setunit{\addspace}%
     \printfield{entrysubtype}%
     \clearfield{entrysubtype}%
     \global\booltrue{bbx:titleinauthpos}}
    {\printfield{label}}}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

% Unchanged from authoryear-comp

\DeclareBibliographyOption{dashed}[true]{%
  \ifstrequal{#1}{true}
    {\ExecuteBibliographyOptions{pagetracker}%
     \renewbibmacro*{bbx:savehash}{\savefield{fullhash}{\bbx@lasthash}}}
    {\ExecuteBibliographyOptions{pagetracker=false}%
     \renewbibmacro*{bbx:savehash}{}}}

\DeclareFieldFormat{shorthandwidth}{#1}
\setlength{\bibitemsep}{0pt}

\DeclareNameAlias{author}{sortname}
\DeclareNameAlias{editor}{sortname}
\DeclareNameAlias{translator}{sortname}

\defbibenvironment{bibliography}
  {\list
     {}
     {\setlength{\leftmargin}{\bibhang}%
      \setlength{\itemindent}{-\leftmargin}%
      \setlength{\itemsep}{\bibitemsep}%
      \setlength{\parsep}{\bibparsep}}}
  {\endlist}
  {\item}

\defbibenvironment{shorthands}
  {\list
     {\printfield[shorthandwidth]{shorthand}}
     {\setlength{\labelwidth}{\shorthandwidth}%
      \setlength{\leftmargin}{\labelwidth}%
      \setlength{\labelsep}{\biblabelsep}%
      \addtolength{\leftmargin}{\labelsep}%
      \setlength{\itemsep}{\bibitemsep}%
      \setlength{\parsep}{\bibparsep}%
      \renewcommand*{\makelabel}[1]{##1\hss}}}
  {\endlist}
  {\item}

\InitializeBibliographyStyle{%
  \global\undef\bbx@lasthash}

\newbibmacro*{bbx:savehash}{%
  \savefield{fullhash}{\bbx@lasthash}}

\newbool{bbx@inset}
\DeclareBibliographyDriver{set}{%
  \booltrue{bbx@inset}%
  \entryset{}{}%
  \newunit\newblock
  \usebibmacro{setpageref}%
  \finentry}

\endinput
