You are here

function nodeaccess_uuid_entity_uuid_save in Nodeaccess 7

Implements hook_entity_uuid_save().

File

nodeaccess_uuid/nodeaccess_uuid.module, line 36
Adds UUID functionality to the nodeaccess module.

Code

function nodeaccess_uuid_entity_uuid_save($entity, $entity_type) {
  if ($entity_type !== 'node' || empty($entity->nodeaccess)) {
    return;
  }
  $nid = entity_get_id_by_uuid($entity_type, array(
    $entity->uuid,
  ));
  if (empty($nid)) {
    return;
  }
  $values['nid'] = reset($nid);

  // Store the roles and users so that we don't have to continually load them.
  $roles = drupal_static(__FUNCTION__ . '_roles', array());
  $users = drupal_static(__FUNCTION__ . '_users', array());
  foreach ($entity->nodeaccess as $nodeaccess) {
    switch ($nodeaccess['realm']) {
      case 'nodeaccess_rid':
        $role_name = $nodeaccess['role_name'];
        if (empty($roles[$role_name])) {
          $role = user_role_load_by_name($role_name);
          if (!empty($role)) {
            $roles[$role_name] = $role->rid;
          }
          elseif (variable_get('nodeaccess_uuid_create_missing_roles', TRUE)) {

            // Lets create the role.
            $role = new stdClass();
            $role->name = $role_name;
            user_role_save($role);
            $roles[$role_name] = $role->rid;
          }
          else {
            break;
          }
        }
        $nodeaccess['gid'] = $roles[$role_name];
        $values['rid'][$nodeaccess['gid']] = $nodeaccess;
        break;
      case 'nodeaccess_uid':
        if (empty($users[$nodeaccess['user_uuid']])) {
          $user = entity_get_id_by_uuid('user', array(
            $nodeaccess['user_uuid'],
          ));
          if (empty($user[$nodeaccess['user_uuid']])) {
            break;
          }
          $users[$nodeaccess['user_uuid']] = $user[$nodeaccess['user_uuid']];
        }
        $nodeaccess['gid'] = $users[$nodeaccess['user_uuid']];
        $values['uid'][$nodeaccess['gid']] = $nodeaccess;
        break;
    }
  }
  $form_state = array(
    'values' => $values,
  );
  _nodeaccess_grants_form_submit(array(), $form_state);
}