3

I'm suprised that it is so hard to update an option value in a certain group using our API.

I have been writing scripts as follows:

  1. get option group id where option group name = X (save as OptionGroupIDX)
  2. get option value id where option value name = Y and option group id = OptionGroupIDX =(save as OptionValueIDY)
  3. update OptionValueIDY title to Z

I'd like to be able to write scripts that look more like:

  1. update the option value X in option group Y to value Z

The real world shell script I'm using at the minute is as follows:

OPTION_GROUP_ID=`drush cvapi OptionGroup.getvalue return=id name=from_email_address`
OPTION_VALUE_ID=`drush cvapi OptionValue.getvalue return=id option_group_id=$OPTION_GROUP_ID`
drush cvapi OptionValue.create id=$OPTION_VALUE_ID label='"Fixed" <info@fixed.org>'

Anyone able to help me get it closer to a one liner? Would be awesome if so because patterns like this seem to come up a fair amount when I'm working with the API. Would chaining help here?

2

In Civi 4.6+, you can use pseudoconstants instead of IDs in many places - including for option_group_id. Civi 4.7 expands the number of places you can use it.

So you can get this down to 2 lines by replacing the first two with:

OPTION_VALUE_ID=`drush cvapi OptionValue.getvalue return=id option_group_id=from_email_address`.

Here's the trick: If the API Explorer allows you to select the value from a select list, that means that pseudoconstant substitution is available on that field.

Unfortunately, I don't think you're going to get this down to one API call. create requires an ID; you're just not going to get the ID without a get call.

EDIT: It MAY be possible to get this down to one line! See "options.match" and "options.match-mandatory" on this page.

2

Looks like a case for API chaining. This or something like it should work:

drush cvapi OptionValue.getvalue return=id option_group_id=from_email_address \
    api.OptionValue.create={label:'"Fixed" <info@fixed.org>'}

Unfortunately, API chaining via drush does not appear to work. Thanks to @noah-miller for pointing that out. I tried chaining via cli.php and that doesn't appear to work either. It seems not to register the api.Entity.action parameter at all (which makes me optimistic it is not a difficult fix).

Jon's suggestion could be combined with what you already have to put it on one line, though the readability of this code is questionable:

drush cvapi OptionValue.create id=`drush cvapi OptionValue.getvalue \
    return=id option_group_id=from_email_address` label='"Fixed" <info@fixed.org>'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.