You are here

function workbench_access_node_presave in Workbench Access 7

Implements hook_node_presave().

1 call to workbench_access_node_presave()
workbench_access_node_insert in ./workbench_access.module
Implements hook_node_insert().

File

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

Code

function workbench_access_node_presave($node) {

  // We must have a node id to continue. @see workbench_access_node_insert().
  if (empty($node->nid)) {
    return;
  }

  // hook_node_operations() does not retuen a full node.
  // See https://www.drupal.org/node/2895338.
  if (!isset($node->type)) {
    $query = db_query('SELECT type FROM {node} WHERE nid = :nid', array(
      ':nid' => $node->nid,
    ));
    $node->type = $query
      ->fetchField();
  }

  // Only run this on enabled node types.
  if (!isset($node->type) || !variable_get('workbench_access_node_type_' . $node->type, 1)) {
    return;
  }

  // Did we use the default field form?
  workbench_access_prepare_field_save($node);
  if (isset($node->workbench_access_fields)) {
    $node->workbench_access = array();
    foreach ($node->workbench_access_fields as $field) {
      if (!is_array($node->{$field})) {
        $node->workbench_access[] = $node->{$field};
      }
      else {
        if (!empty($node->workbench_access_language)) {
          $lang = field_language('node', $node, $field);
          $data = isset($node->{$field}[$lang]) ? $node->{$field}[$lang] : array();
        }
        else {
          $data = array(
            $node->{$field},
          );
        }
        foreach ($data as $value) {
          if (isset($value[$node->workbench_access_column])) {

            // Handle disabled menu links.
            if (!isset($value['enabled']) || !empty($value['enabled'])) {
              $node->workbench_access[] = $value[$node->workbench_access_column];
            }
          }
        }
      }
    }
  }

  // Ensure that the data format is consistent and that items that cannot
  // be changed by this editor are preserved.
  if (isset($node->workbench_access) && isset($node->workbench_access_fixed)) {
    if (!is_array($node->workbench_access)) {
      $node->workbench_access = array(
        $node->workbench_access,
      );
    }
    if (!is_array($node->workbench_access_fixed)) {
      $node->workbench_access_fixed = array(
        $node->workbench_access_fixed,
      );
    }
    $node->workbench_access += $node->workbench_access_fixed;
  }

  // Due to form handling, it is possible that we have duplicate entries.
  if (isset($node->workbench_access) && is_array($node->workbench_access)) {
    $node->workbench_access = array_unique($node->workbench_access);
  }
}