4

I would like to use drush to change one of the options stored in a large options field of a contrib module configuration table.

drush vset/vget, as far as I know, only work on the options that are stored in the variables table.

Is there a way to do this using drush on the variable directly, short of retrieving the whole field (using drush sql-query), modifying it (using unix tools), and updating it (again, using drush sql-query)? It needs to be automated, as this is a frequent task.

Or can anybody suggest a different approach that is more elegant?

  • 1
    There isn't - the closest command is field-update, which gets you the URL to the edit page. You could always write a custom command for it though – Clive Jun 10 '14 at 12:53
  • 1
    To save you a bit of bother working with the field API... $info = field_info_field('field_name'); $info['settings']['allowed_values'] = $new_array; field_update_field($info); will be pretty close to what you're after – Clive Jun 10 '14 at 15:02
6

Check to see if the contrib module has a modulename.drush.inc file in it. If it does, then the module provides Drush commands, and you can find out about them via drush help --filter=modulename.

If the command does not supply any Drush commands, the next thing to do is to open up the modulename.module and scan through the source code, and see if it has any API functions that perform the operation that you want to do. This can sometimes seem intimidating, but it's worth a shot! If you'd like to set the frobulation level of the module to 28, maybe, just maybe, you might find a line that reads function modulename_set_frobulation_level($value) .... If you do, then you can call it with Drush like this:

drush ev 'modulename_set_frobulation_level(28);'

If you do this a lot, you could make a Bash function for it, or, better yet, create a custom Drush command and contribute it back to the contrib module. Making a custom Drush function is really easy with the drushify command.

If you are unlucky, you will not find a nice set_frobulation_level function; sometimes, the code you want is only available in a form submit handler. This is distressingly common in contrib modules. Rather than try to figure out how to post a fake form submit, an even better option is to just factor out the code you'd like to call, and make the modulename_set_frobulation_level function yourself. Then, it's easy to create a Drush command for it, and you can contribute the whole shebang back to the contrib module when you are done, helping out the next person who travels this same road.

This might sound like a bit of work, but it's no harder than reverse-engineering the sql commands you'd need to run, and the results are much more satisfying.

  • Thanks, I just went through the contrib module's .drush.inc file. An update command is not available, but a list command, which is a good start. I'll probably go the route of adding an update command to the drush script (and contributing it back, of course). Will take my question now to the appropriate issue queue. – Ursula Jun 10 '14 at 14:56

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.