Difference between revisions of "MultiLang Fields Bazaar Package"

From EPrints Documentation
Jump to: navigation, search
Line 29: Line 29:
 
within eprints' home directory.
 
within eprints' home directory.
  
 +
The following sections explain each step in detail, where we are using '''ml_title''' as our example field.
  
== Edit workflow ==
+
== Introducing ml_title field in EPrints ==
 +
We create the file: '''~eprints/archives/<reponame>/cfg/cfg.d/zz_multilang_field.pl''' with the following content:
 +
 
 +
...
 +
#!perl
 +
#define local fields
 +
my $local_fields = [
 +
{
 +
    name => 'ml_title',
 +
    type => 'multilang',
 +
    multiple => 1,
 +
    fields => [ { sub_name => "text", type => "longtext", input_rows => 3, make_single_value_orderkey => 'EPrints::Extras::english_title_orderkey' } ],
 +
    input_add_boxes => 1,
 +
},
 +
 
 +
{
 +
    name => 'title',
 +
    type => 'virtualwithvalue',
 +
    virtual => 1,
 +
 
 +
    get_value => sub
 +
    {
 +
        my ($eprint) = @_;
 +
        if ($eprint->is_set('ml_title'))
 +
        {
 +
            my $lang = $eprint->repository->get_langid;
 +
            my $lang_set = 0;
 +
            my $vals = $eprint->get_value('ml_title');
 +
            my $title = '';
 +
            if (!$lang)
 +
            {
 +
                $lang_set = 1;
 +
            }
 +
            else
 +
            {
 +
                # set the default lang's text as title
 +
                foreach my $v1 (@{$vals})
 +
                {
 +
                    if ($v1->{lang} eq $lang)
 +
                    {
 +
                        $title = $v1->{text};
 +
                    }
 +
                }
 +
            }
 +
            # if the language is not set or I can't find an abstract in the
 +
            # user's language, get the first object's text as abstract
 +
            if ($lang_set or $title eq '')
 +
            {
 +
                $title = $vals->[0]->{text};
 +
            }
 +
            return $title;
 +
 
 +
        }
 +
        return undef;
 +
    },
 +
 
 +
    set_value => sub
 +
    {
 +
        my ($eprint, $value) = @_;
 +
        my $lang = 'en';
 +
        #only use this on imports, NOT if the value is already set
 +
        if ($eprint->is_set('ml_title'))
 +
        {
 +
            return;
 +
        }
 +
        if ($value)
 +
        {
 +
            $eprint->set_value('ml_title', [{lang=>$lang, text=>$value}]);
 +
        }
 +
    }
 +
},
 +
];
 +
 
 +
#create lookup hash of local field names
 +
my $local_fieldnames = {};
 +
 
 +
foreach my $f (@{$local_fields})
 +
{
 +
    $local_fieldnames->{$f->{name}} = 1;
 +
}
 +
 
 +
#merge in existing field configurations
 +
foreach my $f (@{$c->{fields}->{eprint}})
 +
{
 +
    if (!$local_fieldnames->{$f->{name}})
 +
    {
 +
    push @{$local_fields}, $f;
 +
    }
 +
}
 +
 
 +
 
 +
 
 +
#overwrite original array of configured fields
 +
$c->{fields}->{eprint} = $local_fields;
 +
...
 +
 
 +
== Editing the workflow ==
 
blahblah
 
blahblah

Revision as of 10:39, 6 June 2016


The MultiLang plugins introduces multiple language support in EPrints fields. The specific plugin replaces title and abstract fields with their multilanguage version (ml_title and ml_abstract respectively), but this wiki-page will explain how one can achieve this functionality in other EPrints fields as well.

Installation Prerequisites

None

Installation

Install through the EPrints Bazaar

How to use the plugin

Once the plugin has been installed, the user needs to edit the workflow to contain the multilingual fields' versions instead of the default ones. This procedure is explained in MultiLang Fields Bazaar Package::Edit workflow section.

How the plugin works

In order to introduce a new field in EPrints (such as the ml_title field), a few things need to take place:

  • The field's name and functionality needs to be introduced to the EPrints system via a configuration file located in ~eprints/archives/<reponame>/cfg/cfg.d/.
  • EPrints' database needs to be updated to include the new field. This is achieved when user eprints executes (from his home directory):
$ ./bin/epadmin update reponame
  • The appropriate phrases need to be added for each field and for each supported language.
  • The workflow needs to be edited in order to contain the new fields - and not include the original ones.
  • The repository needs to be reloaded, by running:
$ ./bin/epadmin reload reponame

within eprints' home directory.

The following sections explain each step in detail, where we are using ml_title as our example field.

Introducing ml_title field in EPrints

We create the file: ~eprints/archives/<reponame>/cfg/cfg.d/zz_multilang_field.pl with the following content:

...

  1. !perl
  2. define local fields

my $local_fields = [ {

   name => 'ml_title',
   type => 'multilang',
   multiple => 1,
   fields => [ { sub_name => "text", type => "longtext", input_rows => 3, make_single_value_orderkey => 'EPrints::Extras::english_title_orderkey' } ],
   input_add_boxes => 1,

},

{

   name => 'title',
   type => 'virtualwithvalue',
   virtual => 1,
   get_value => sub
   {
       my ($eprint) = @_;
       if ($eprint->is_set('ml_title'))
       {
           my $lang = $eprint->repository->get_langid;
           my $lang_set = 0;
           my $vals = $eprint->get_value('ml_title');
           my $title = ;
           if (!$lang)
           {
               $lang_set = 1;
           }
           else
           {
               # set the default lang's text as title
               foreach my $v1 (@{$vals})
               {
                   if ($v1->{lang} eq $lang)
                   {
                       $title = $v1->{text};
                   }
               }
           }
           # if the language is not set or I can't find an abstract in the 
           # user's language, get the first object's text as abstract
           if ($lang_set or $title eq )
           {
               $title = $vals->[0]->{text};
           }
           return $title;
       }
       return undef;
   },
   set_value => sub
   {
       my ($eprint, $value) = @_;
       my $lang = 'en';
       #only use this on imports, NOT if the value is already set
       if ($eprint->is_set('ml_title'))
       {
           return;
       }
       if ($value)
       {
           $eprint->set_value('ml_title', [{lang=>$lang, text=>$value}]);
       }
   }

}, ];

  1. create lookup hash of local field names

my $local_fieldnames = {};

foreach my $f (@{$local_fields}) {

   $local_fieldnames->{$f->{name}} = 1;

}

  1. merge in existing field configurations

foreach my $f (@{$c->{fields}->{eprint}}) {

   if (!$local_fieldnames->{$f->{name}})
   {
    push @{$local_fields}, $f;
   }

}


  1. overwrite original array of configured fields

$c->{fields}->{eprint} = $local_fields; ...

Editing the workflow

blahblah