Jump to content
Wiki Home
EPrints Home
Bazaar
Files
Main menu
Main menu
move to sidebar
hide
Navigation
Front Page
EPrints 3 Manual
EPrints Training Course
EPrints Glossary
Installing EPrints
How-to Configure EPrints
EPrints 3.2+ API
EPrints 3 Reference
EPrints Plugins
Wiki
All categories
Recent changes
Main Page
Help
EPrints Documentation
Search
Search
Appearance
Log in
Request account
Personal tools
Log in
Request account
Pages for logged out editors
learn more
Contributions
Talk
Editing
Using the new API
(section)
Page
Discussion
British English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=Doing a search and mapping a list (Harder)= Taking our existing plug-in we now want to make our input box a search term "Observations" and print out a list of EPrints. To do this we need to modify our action_process_input method and the render method from before. ==Searching== First things first we need a place to store a list of eprints rather than just one. Let's declare (or change the existing) global variable eprints: our $eprints = undef; Next lets change the action_process_input method to perform a search, to do this find the line which reads the "input_field" parameter and change this: my $input_value = $repository->param( "input_field" ); As this no longer represents an EPrint ID we'll rename the parameter. You can delete all the lines after this in the action_process_input method as we are going to replace them all. Next lets get the eprint dataset so we can search it: my $eprint_dataset = $repository->dataset( "eprint" ); Now we are going to search on TITLE so lets define the filter for the search. Here we are doing a fuzzy search (the match => EX parameter is not used). my @filters = ( { meta_fields => [qw( title )], value => $input_value }, ); Now lets do the search and assign the output to our list of eprints: $eprints = $eprint_dataset->search( filters => \@filters, custom_order=>"-datestamp", limit => 10, ); Notice that we are setting an order (based upon age) for the eprints to be listed in. Also we are limiting the number returned to 10. Finally lets do some error handling if the list is empty and nothing was found. This is just a little more detailed than before. if (!defined $eprints or $eprints->count < 1) { $self->{processor}->add_message( "error", $self->{repository}->make_text("No EPrints found to match search: $input_value") ); } ==Rendering the output== Lastly to display all the items in our list we need to change the render method. To do this find the place where did the rendering before (it was inside an "if" statement). To perform a bit more error checking lets firstly change the "if" statement slightly: if (defined $eprints and $eprints->count > 0) This will check there is actually something in the list as well as it being defined. Next we need to iterate of the items in the list and print their citations. This can be done using the following map function: $eprints->map( sub { my( undef, undef, $eprint ) = @_; $frag->appendChild($eprint->render_citation("default")); $frag->appendChild($repository->make_element("br")); }); Here we can see that each item in the list is an eprint and we can simply render a citation for each followed by a newline. ==Extension exercise== * Add a new parameter to allow the user to set a custom limit to the number of results which come back as a result of a search. * Tidy up your style * Define all the missing phrases
Summary:
Please note that all contributions to EPrints Documentation may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
EPrints Documentation:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)