You are here

function workbench_access_prepare_field_save in Workbench Access 7

Prepares a node for saving in the event we did not come from a form.

1 call to workbench_access_prepare_field_save()
workbench_access_node_presave in ./workbench_access.module
Implements hook_node_presave().

File

./workbench_access.module, line 727
Workbench Access module file.

Code

function workbench_access_prepare_field_save($node) {

  // If using a default form element, or the form data is already present, then
  // we do not need this step.
  if (variable_get('workbench_access_custom_form', 1)) {
    return;
  }
  $tree = workbench_access_get_active_tree();
  $scheme = $tree['access_scheme'];
  $node->workbench_access_language = $scheme['translatable'];
  $node->workbench_access_column = $scheme['storage_column'];

  // Find the fields to watch for data.
  if (empty($node->workbench_access_fields) && !empty($scheme['form_field'])) {
    $node->workbench_access_fields[] = $scheme['form_field'];
  }
  elseif (isset($node->type)) {
    $fields = workbench_access_get_assigned_fields($node->type);
    foreach ($fields as $field => $info) {
      $node->workbench_access_fields[] = $field;
    }
  }
  if (!isset($node->workbench_access_fields)) {
    return;
  }

  // Make sure that the required fields are present, even if they're empty.
  foreach ($node->workbench_access_fields as $field) {
    if (!isset($node->{$field})) {
      if ($field == 'menu') {
        menu_node_prepare($node);
        if (!empty($node->menu)) {

          // menu_node_prepare() expects FAPI to mark the link enabled.
          // If it's not marked enabled, when we execute node_save(),
          // menu_node_save() would delete it. Avoid this:
          $node->menu['enabled'] = empty($node->menu['mlid']) ? 0 : 1;
        }
      }
      else {
        $node->{$field} = array();
      }
    }
  }
}