1

I am trying to make my custom action for View Bulk Operations. My action works up to the point where I submit the form specified in assign_to_action_form. The watchdog statement in that function get's logged, but the ones in assign_to_action_submit and assign_to_action don't get logged. So somehow the rest of the action never gets executed. Any clues?

The code is below. Just to explain what the action will be doing: It is a bulk operation that assigns a User entity value to entityreference fields for all selected nodes in the view. It contains an action configuration for VBO specifying which field to fill when the action is executed (function assign_to_action_views_bulk_operations_form), and an action parameter (specified at each execution) specifying the actual User entity to set to the field (function assign_to_action_form).

function various_vbo_actions_action_info() {
    return array(
      'assign_to_action' => array(
        'type' => 'node',
        'label' => t('Assign to user'),
        'behavior' => array('changes_property'),
        'configurable' => FALSE,
        'vbo_configurable' => TRUE,
        'aggregate' => FALSE,
        'triggers' => array('any'),
      ),
    );
}

function assign_to_action_views_bulk_operations_form($options){
    /**
    VBO action configuration form (per view/field)
    */
    $options = array();
    $fields = field_read_fields(array('type'=>'entityreference'));
    foreach($fields as $field){
        if($field['settings']['target_type'] == 'user'){
            $options[$field['field_name']] = $field['field_name'];
        }
    }
    $form = array();
    $form['fieldname'] = array(
        '#type' => 'select',
        '#title' => t('Choose which field to fill.'),
        '#options' => $options,
        '#required' => TRUE,
        '#default_value' => !empty($options['fieldname']) ? $options['fieldname'] : '',
    );
    return $form;
}

function assign_to_action_form($settings,&$form_state){
    watchdog('views','Show form',array(),WATCHDOG_DEBUG);
    /**
        per action (click) configuration
    */
    #$form = array();
    $options = array();
    foreach(entity_load('user') as $user){
        $options[$user->uid] = $user->name;
    }
    $form['uid'] = array(
        '#type' => 'select',
        '#title' => t('Choose which user to assign it to.'),
        '#options' => $options,
        '#required' => TRUE,
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
    );
    return $form;
}

function assign_to_action_submit($form,$form_state){
    watchdog('views','Submit action',array(),WATCHDOG_DEBUG);
    $return = array();
    $return['uid'] = $form_state['values']['uid'];
    return $return;
}

function assign_to_action(&$node,$context){
    watchdog('views','Execute action',array(),WATCHDOG_DEBUG);
    #get settings fieldname
    $field = $context['settings']['fieldname'];
    #get user from context
    $user = user_load($context['settings']['uid']);
    #field_set_value($node,$field,$user);
    drupal_set_message("Got field ".$field." and user ".$user);
}

Your Answer

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

Browse other questions tagged or ask your own question.