You are here

function _itoggle_ajax_toggle_field in iToggle 7.2

Same name and namespace in other branches
  1. 7 itoggle.module \_itoggle_ajax_toggle_field()

Change an entity field's value.

Parameters

string: The entity $type.

string: The entity $bundle.

string: The entity $field.

int $id: The entity'd id.

int $value: The new value of the entity field.

int $delta: The delta of the entity field.

Return value

boolean Whether we managed to change the field value.

See also

itoggle_ajax_callback()

1 call to _itoggle_ajax_toggle_field()
itoggle_ajax_callback in ./itoggle.pages.inc
Page callback. iToggle widget AJAX callback.

File

./itoggle.pages.inc, line 71
iToggle ajax functions.

Code

function _itoggle_ajax_toggle_field($type, $bundle, $field, $id, $value, $delta = 0) {
  $access = FALSE;

  // Check permissions.
  switch ($type) {
    case 'node':
      $node = node_load($id);
      $user = user_uid_optional_load();
      $access = user_access('administer nodes', $user) || user_access('bypass node access') || user_access("edit any {$bundle} content", $user) || $node->uid == $user->uid && user_access("edit own {$bundle} content", $user);
      break;
    case 'user':
      $access = user_access('administer users');
      break;
    case 'taxonomy_term':
    case 'taxomomy_vocabulary':
      $access = user_access('administer taxonomy');
      break;
    default:
      $info = itoggle_get_entity_info();
      $entity = entity_load_single($type, $id);

      // Use Entity API if available.
      if (function_exists('entity_access')) {
        $access = entity_access('edit', $type, $entity);
      }
      else {
        if (isset($info[$type]['access callback'])) {
          $access = call_user_func($info[$type]['access callback'], 'edit', $entity, NULL, $type);
        }
        else {
          $access = user_access('administer site configuration');
        }
      }
  }
  if ($access) {
    if ($entity = entity_load_single($type, $id)) {

      //      $lang = $entity->language;
      $lang = LANGUAGE_NONE;
      if (is_array($entity->{$field}[$lang][$delta])) {
        $entity->{$field}[$lang][$delta]['value'] = $value;
      }
      else {
        $entity->{$field}[$lang][$delta] = $value;
      }
      try {
        $return = entity_save($type, $entity) !== FALSE;
      } catch (Exception $e) {

        // Fail silently.
        $return = FALSE;
      }
      return $return !== FALSE;
    }
  }
  return FALSE;
}