Skip to content

Instantly share code, notes, and snippets.

@jesusbagpuss
Created May 20, 2021 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesusbagpuss/2efc96ed05cf9121b925063a20677120 to your computer and use it in GitHub Desktop.
Save jesusbagpuss/2efc96ed05cf9121b925063a20677120 to your computer and use it in GitHub Desktop.
Custom EPScript additions - render specific contributor type
# Open block
{
# Write into EPrints::Script::Compiled
package EPrints::Script::Compiled;
use strict;
# Example usage in citation file:
#
# <epc:if test="$item.render_contributors_filtered('http://www.loc.gov/loc.terms/relators/THS',1)">
# <h2><epc:phrase ref="contributor_type_typename_http://www.loc.gov/loc.terms/relators/THS"/></h2>
# <epc:print expr="$item.render_contributors_filtered('http://www.loc.gov/loc.terms/relators/THS')"/>
# </epc:if>
#
# The first call indludes the 'test' parameter, which can be used to determine whether surrounding
# elements need to be displayed - in the above example the <h2>.
sub run_render_contributors_filtered
{
my( $self, $state, $eprint, $contributor_type, $test ) = @_;
if( !defined $eprint->[0] || ref($eprint->[0]) ne "EPrints::DataObj::EPrint" )
{
$self->runtime_error( "Can only call render_contributors_filtered() on eprint objects not ".
ref($eprint->[0]) );
}
if( !defined $contributor_type )
{
$self->runtime_error( "You need to specify a contributor type you want to render." );
}
my @filtered_contributors = grep{ defined $_->{type} && $_->{type} eq $contributor_type->[0] } @{ $eprint->[0]->value( "contributors" ) };
if( $test )
{
return [ 0, "BOOLEAN" ] if( scalar @filtered_contributors < 1 );
return [ 1, "BOOLEAN" ];
}
# NB this is based on EPrints::Extras::render_related_url
# It is over-complicated for this use-case, as we could just use
# $state->{session}->render_name( $row->{name} )
# but if other compound fields are being used with similar code
# being able to get to the 'render_value' for the sub fields is useful.
my $field = $eprint->[0]->dataset->field( "contributors" );
my $f = $field->get_property( "fields_cache" );
my $fmap = {};
foreach my $field_conf ( @{$f} )
{
my $fieldname = $field_conf->{name};
my $field = $field->{dataset}->get_field( $fieldname );
$fmap->{$field_conf->{sub_name}} = $field;
}
my $ul = $state->{session}->make_element( "ul" );
foreach my $row ( @filtered_contributors )
{
my $li = $state->{session}->make_element( "li" );
$li->appendChild( $fmap->{name}->render_single_value( $state->{session}, $row->{name} ) );
if( defined $row->{id} )
{
$li->appendChild( $state->{session}->make_text( " (" ) );
$li->appendChild( $state->{session}->make_text( $row->{id} ) );
$li->appendChild( $state->{session}->make_text( ")" ) );
}
$ul->appendChild( $li );
}
return [ $ul, "XHTML" ];
}
} #END BLOCK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment