You are here

function _emfield_emfield_field in Embedded Media Field 6.2

Same name and namespace in other branches
  1. 6.3 emfield.cck.inc \_emfield_emfield_field()
  2. 6.3 deprecated/emfield-deprecated.cck.inc \_emfield_emfield_field()
  3. 6 emfield.cck.inc \_emfield_emfield_field()

Implementation of hook_emfield_field(). This in turn is a callback for hook_field().

This private function is returned directly by emfield_emfield_field().

1 call to _emfield_emfield_field()
emfield_emfield_field in ./emfield.module
A helper function for hook_field(), called by the various sub-modules.

File

./emfield.cck.inc, line 47
Helper functions to implement our various cck-required functions, such as hook_field and hook_widget.

Code

function _emfield_emfield_field($op, &$node, $field, &$items, $teaser, $page, $module) {
  $return = array();
  switch ($op) {
    case 'validate':
      foreach ($items as $delta => $item) {
        $error_field = $field['multiple'] ? $field['field_name'] . '][' . $delta . '][embed' : $field['field_name'];
        _emfield_field_validate_id($field, $item, $error_field, $module, $delta);
      }
      break;
    case 'sanitize':

      // Get the field ready for Previews.
      if (!empty($node->preview)) {
        foreach ($items as $delta => $item) {
          $items[$delta] = _emfield_field_submit_id($field, $item, $module);
        }
      }
      break;
    case 'presave':
      foreach ($items as $delta => $item) {
        $items[$delta] = _emfield_field_submit_id($field, $item, $module);

        // Remove empty fields
        if (empty($item['embed'])) {

          // Remove thumbnail files
          if (module_exists('emthumb')) {

            // see http://drupal.org/node/393490#comment-1328368
            if (is_array($item) && !empty($item['emthumb']['emthumb']['emthumb']['emthumb']['file'])) {
              _emthumb_file_delete($item['emthumb']['emthumb']['emthumb']['emthumb']['file'], $field['field_name']);
            }
          }
          unset($items[$delta]);
        }
      }
      break;
    case 'load':

      // We need to unserialize the 'data' column manually.
      $field_name = $field['field_name'];
      foreach ($items as $delta => $item) {
        $data = (array) unserialize($items[$delta]['data']);
        $items[$delta]['data'] = $data;
        $node->{$field_name}[$delta]['data'] = $data;
      }
      $return[$field_name] = $items;
      break;
    case 'delete':
      break;
  }

  // Allow other modules to modify the data before storing.
  if (in_array($op, array(
    'insert',
    'update',
  ))) {
    foreach ($items as $delta => $item) {

      // Invoke all modules implementing hook_emfield_data_alter(&$data),
      // with the following parameters also sent.
      drupal_alter('emfield_data', $items[$delta]['data'], $module, $delta, $node, $field, $items);
    }
  }

  // Allow modules (such as emthumb) to alter our data.
  foreach (module_implements('emfield_field_extra') as $module) {
    $args = array(
      $op,
      &$node,
      $field,
      &$items,
      $teaser,
      $page,
      $module,
    );
    $ret = call_user_func_array($module . '_emfield_field_extra', $args);
    if (is_array($ret)) {
      $return = array_merge($return, $ret);
    }
  }
  if (in_array($op, array(
    'insert',
    'update',
  ))) {
    foreach ($items as $delta => $item) {

      // We need to manually serialize the 'data' array.
      if (is_array($items[$delta]['data'])) {
        $items[$delta]['data'] = serialize($items[$delta]['data']);
      }
    }
  }
  return $return;
}