Hi,

I was wondering if there is a way to programatically create a configuration for MM? For the sake of Brightcove module, I would like to create a default config that will move files from a filefield to their API and I would like to do that automatically.

Thank you very much,
Jakub

Comments

arthurf’s picture

Yes, you can use the import/export functionality in 6.1x - just write your config and run it through the import submit function. Not ideal, but it works. In 6.2x there will be support for configurations in code.

kobnim’s picture

Arthur,

I am trying to programmatically import a configuration, using 6.x-1.x, and have been struggling with this for quite a while.

When you say "just write your config and run it through the import submit function" ... could you please explain? Which "import submit function" are you referring to?

I have tried calling these functions to convert my configuration to a form:

$form = media_mover_api_configuration_base_form($configuration); 
$form[] = media_mover_api_build_add_config($configuration);

And I see this function, which submits the form:
function media_mover_api_add_config_form_submit($form, &$form_state)

But this is where I am stuck. I do not know how to generate $form_state.

Am I on the right track? Is there a way to generate $form_state? Is there a better approach?
Thanks very much.

kobnim’s picture

I am now able to programmatically import a configuration, in 6.x-1.x. This is the missing step, which fills $form_state['values'] with the default values from $form:

    function mymodule_get_form_state($form){
      $values = array();
      _mymodule_form_get_values($form, &$values);
      $form_state = array();
      $form_state['values'] = $values;
      return ($form_state);
    }
    
    function _mymodule_form_get_values($element, &$values) {
      foreach (element_children($element) as $key) {
        if (isset($element[$key]) && $element[$key]) {
          if (isset($element[$key]['#default_value']) && $element[$key]['#default_value']) {
    	    $values[$key] = $element[$key]['#default_value'];
          } else {
            // Recurse through all children elements.
            _mymodule_form_get_values($element[$key], &$values);
          }
        }
      }
    }
arthurf’s picture

So the note that I made is somewhat vague- when you import your code it dumps you onto the configuration add form- I did this so that there would not be name space collisions. In 6.x-2.x branch this is handled better by ctools and since the configurations have machine names it's much easier to do this right.