You are here

function emthumb_emfield_field_extra in Embedded Media Field 6

Same name and namespace in other branches
  1. 5 contrib/emthumb/emthumb.module \emthumb_emfield_field_extra()
  2. 6.3 contrib/emthumb/emthumb.module \emthumb_emfield_field_extra()
  3. 6.2 contrib/emthumb/emthumb.module \emthumb_emfield_field_extra()

Implements hook_emfield_field_extra(). This is called on field operations to allow us to act on emthumbs.

File

contrib/emthumb/emthumb.module, line 12
Allows for custom thumbnail overrides to Embedded Media Field.

Code

function emthumb_emfield_field_extra($op, &$node, $field, &$items, $teaser, $page, $module) {
  switch ($op) {
    case 'load':

      // Called after content.module loads default data.
      $output = array();
      if (count($items)) {
        $values = array();
        foreach ($items as $delta => $file) {

          // For some weird reason this is_array() test is required to
          // prevent php from segfaulting apache
          $existing_data = array();
          if (isset($file['data']) && isset($file['data']['emthumb']) && is_array($file['data']['emthumb'])) {
            $existing_data = $file['data']['emthumb'];
          }

          // Merge new file with existing data so we include alt tag data.
          $items[$delta]['data']['emthumb'] = array_merge($existing_data, _emthumb_file_load($file['data']['emthumb']['fid']));
        }
        return array(
          $field['field_name'] => $items,
        );
      }
      break;
    case 'rss item':

      // Different from load (and others) as it can be, and is, called within
      // each $field individually.
      $output = array();
      if (count($items)) {
        $values = array();
        foreach ($items as $delta => $file) {
          $thumbnail = _emthumb_file_load($file['data']['emthumb']['fid']);
          if (isset($thumbnail['filepath'])) {
            $options = array(
              'absolute' => TRUE,
            );
            $thumbnail['filepath'] = url($thumbnail['filepath'], $options);
            $output[$delta]['thumbnail'] = $thumbnail;
          }
        }
      }
      return $output;
    case 'validate':

      //TODO Implement this validate function
      break;
    case 'insert':
    case 'update':

      // Called before content.module defaults.
      foreach ($items as $delta => $item) {
        if (!$items[$delta]['data']['emthumb'] && $items[$delta]['emthumb']['emthumb']['emthumb']['emthumb']) {

          // This will delete the file if the flag is on
          _emthumb_file_update($node, $items[$delta]['emthumb']['emthumb']['emthumb']['emthumb']['file'], $field, $items[$delta]['emthumb']['emthumb']['emthumb']['emthumb']['flags']['delete']);
          $items[$delta]['data']['emthumb'] = $items[$delta]['emthumb']['emthumb']['emthumb']['emthumb']['file'];
        }
        if (empty($items[$delta]['data']['emthumb']) && $field['widget']['emthumb_store_local_thumbnail']) {

          // was: variable_get('emthumb_store_local_thumbnail', TRUE)) {
          // Fetch remote thumb because we don't have a custom one.
          $items[$delta]['data']['emthumb'] = emthumb_fetch_remote_thumbnail($items[$delta], $field);
        }

        // Add alt text and alt title if necessary.
        if (!empty($items[$delta]['emthumb']['emthumb']['emthumb']['emthumb']['emthumb_alt'])) {
          $items[$delta]['data']['emthumb']['emthumb_alt'] = $items[$delta]['emthumb']['emthumb']['emthumb']['emthumb']['emthumb_alt'];
        }
        if (!empty($items[$delta]['emthumb']['emthumb']['emthumb']['emthumb']['emthumb_title'])) {
          $items[$delta]['data']['emthumb']['emthumb_title'] = $items[$delta]['emthumb']['emthumb']['emthumb']['emthumb']['emthumb_title'];
        }
        if (isset($items[$delta]['emthumb'])) {

          // We're saving in the data property so delete emthumb
          unset($items[$delta]['emthumb']);
        }
      }

      // Compact deltas.
      $items = array_values($items);
      break;
    case 'delete':
      foreach ($items as $delta => $item) {
        _emthumb_file_delete($item['data']['emthumb'], $field['field_name']);
      }
      break;
  }
}