You are here

function imagefield_field_settings in ImageField 5.2

Same name and namespace in other branches
  1. 5 imagefield.module \imagefield_field_settings()

Implementation of hook_field_settings().

File

./imagefield.module, line 99
Defines an image field type. imagefield uses content.module to store the fid, and the drupal files table to store the actual file data.

Code

function imagefield_field_settings($op, $field) {
  switch ($op) {
    case 'callbacks':
      return array(
        'view' => CONTENT_CALLBACK_CUSTOM,
      );
    case 'form':
      $form = array();
      $form['default'] = array(
        '#type' => 'fieldset',
        '#title' => t('Default image'),
      );

      // Present a thumbnail of the current default image.
      $form['default']['use_default_image'] = array(
        '#type' => 'checkbox',
        '#title' => t('Use default image'),
        '#default_value' => $field['use_default_image'],
        '#description' => t('Check here if you want to use a image as default.'),
      );
      if (!empty($field['default_image'])) {
        $form['default']['default_image_thumbnail'] = array(
          '#type' => 'markup',
          '#value' => theme('imagefield_image', $field['default_image'], '', '', array(
            'width' => '150',
          ), false),
        );
      }
      $form['default']['default_image_upload'] = array(
        '#type' => 'file',
        '#title' => t('Upload image'),
        '#description' => t('Choose a image that will be used as default.'),
      );

      // We set this value on 'validate' so we can get cck to add it
      // as a standard field setting.
      $form['default_image'] = array(
        '#type' => 'value',
        '#value' => $field['default_image'],
      );
      return $form;
    case 'validate':

      // We save the upload here because we can't know the correct
      // file path until we save the file.
      // Check of we got an new upload.
      if (!($file = file_check_upload('default_image_upload'))) {
        break;
      }

      // figure steal the file extension and construct a filename for this
      // fields default image. This is standardized for default image handling
      // with private files.
      $ext = array_pop(explode('.', $file->filename));
      $filename = $field['field_name'] . '.' . $ext;

      // verify the destination exists and is writeable...
      $dst = 'imagefield_default_images/' . $filename;
      if (!imagefield_check_directory(dirname($dst))) {
        form_set_error('default_image', t("The default image could not be uploaded. The destination(%d) does not exist or is not writable by the webserver.", array(
          '%d' => dirname($dst),
        )));
        break;
      }

      // save the upload to its resting place.
      if (!($file = file_save_upload('default_image_upload', $dst, FILE_EXISTS_REPLACE))) {
        form_set_error('default_image', t("The default image could not be uploaded. Failed saving to destination(%d).", array(
          '%d' => $dst,
        )));
        break;
      }

      // set the value of the form_item so we can store this in the settings
      // from validate.
      form_set_value(array(
        '#parents' => array(
          'default_image',
        ),
      ), (array) $file);
      break;
    case 'save':
      return array(
        'default_image',
        'use_default_image',
      );
    case 'database columns':
      $columns = array(
        'fid' => array(
          'type' => 'int',
          'not null' => true,
          'default' => '0',
        ),
        'title' => array(
          'type' => 'varchar',
          'length' => 255,
          'not null' => true,
          'default' => "''",
          'sortable' => true,
        ),
        'alt' => array(
          'type' => 'varchar',
          'length' => 255,
          'not null' => true,
          'default' => "''",
          'sortable' => true,
        ),
      );
      return $columns;
    case 'filters':
      return array(
        'not null' => array(
          'operator' => array(
            '=' => t('Has Image'),
          ),
          'list' => 'views_handler_operator_yesno',
          'list-type' => 'select',
          'handler' => 'imagefield_views_handler_filter_is_not_null',
        ),
      );
  }
}