You are here

function _cmis_sync_drupal_node_field_value in CMIS API 7.2

Same name and namespace in other branches
  1. 6.4 cmis_sync/cmis_sync.drupal.inc \_cmis_sync_drupal_node_field_value()
  2. 6.3 cmis_sync/cmis_sync.drupal.inc \_cmis_sync_drupal_node_field_value()
  3. 7 cmis_sync/cmis_sync.drupal.inc \_cmis_sync_drupal_node_field_value()

Utility that gets/sets node field value. Only supports regular, text and filefield fields types.

2 calls to _cmis_sync_drupal_node_field_value()
_cmis_sync_cmis_drupal_prepare in cmis_sync/cmis_sync.cmis.inc
Maps a cmis_object to a drupal node.
_cmis_sync_drupal_cmis_prepare in cmis_sync/cmis_sync.drupal.inc
Maps a drupal node to a cmis_object

File

cmis_sync/cmis_sync.drupal.inc, line 197

Code

function _cmis_sync_drupal_node_field_value(&$node, $field_name, $field_value = NULL, $context = array()) {
  if (isset($node)) {
    $fields = field_info_instances('node', $node->type);
    $value = NULL;
    if (array_key_exists($field_name, $fields)) {
      if ($field_name != NULL) {
        $content_field = $node->{$field_name};
      }
      switch ($fields[$field_name]['widget']['type']) {
        case 'image_image':
        case 'file_generic':
          if ($field_value == NULL) {

            // Get the file data
            $items = field_get_items('node', $node, $field_name);
            $file = file_load($items[0]['fid']);
            $value = file_get_contents($file->uri);
          }
          else {
            $items = field_get_items('node', $node, $field_name);
            if (is_array($items)) {

              // update existing file
              $file = file_load($items[0]['fid']);
              if (!empty($file->uri)) {
                file_put_contents($file->uri, $field_value);
              }
            }
            else {
              $field_info = field_info_field($field_name);
              $instance_info = field_info_instance('node', $field_name, $node->type);
              $path = $field_info['settings']['uri_scheme'] . '://';
              if (strlen($instance_info['settings']['file_directory']) > 0) {

                // Set up and create path if it is not already set up
                $path .= $instance_info['settings']['file_directory'] . '/';
                if (!is_dir($path)) {
                  drupal_mkdir($path, NULL, TRUE);
                }
              }

              // add an image
              $filename = $path . uniqid('cmis_sync_');
              $file = file_save_data($field_value, $filename);
              if ($file) {
                $file->display = 1;
                $node->{$field_name}[LANGUAGE_NONE][0] = (array) $file;
              }
            }
          }
          break;
        case 'text':
        case 'text_textarea_with_summary':
          if ($field_value == NULL) {
            $value = $content_field['und'][0]['value'];
          }
          else {
            $node->{$field_name} = array();
            $node->{$field_name}['und'][0]['value'] = $field_value;
            $node->{$field_name}['und'][0]['format'] = 'filtered_html';
          }
          break;
        case 'date':
        case 'datestamp':
        case 'datetime':
        case 'number_decimal':
        case 'number_float':
        case 'number_integer':
          if ($field_value == NULL) {
            $value = $content_field['und'][0]['value'];
          }
          else {
            $node->{$field_name} = array();
            $node->{$field_name}['und'][0]['value'] = $field_value;
          }
          break;
        default:
          throw new CMISException(t('Unsupported sync drupal content field type [@field_type] for field [@field_name]. Please review your configuration.', array(
            '@field_name' => $field_name,
            '@field_type' => $fields[$field_name]['widget']['type'],
          )));
          break;
      }
    }
    else {

      // regular node field value
      if ($field_value == NULL) {
        $value = $node->{$field_name};
      }
      else {
        $node->{$field_name} = $field_value;
      }
    }
    return $value;
  }
  else {
    return NULL;
  }
}