You are here

function asset_widget in Asset 5.2

Same name and namespace in other branches
  1. 5 modules/asset_content.inc \asset_widget()
  2. 6 modules/asset_content.inc \asset_widget()
  3. 6 inc/modules/asset_content.inc \asset_widget()

Implementation of hook_widget().

File

modules/asset_content.inc, line 212

Code

function asset_widget($op, &$node, $field, &$items) {
  switch ($op) {
    case 'form':
      $form = array();
      $return_type = 'id';
      $key = 'aid';
      drupal_add_js(drupal_get_path('module', 'asset_wizard') . '/js/asset_wizard_cck.js');
      $form[$field['field_name']] = array(
        '#tree' => TRUE,
        '#type' => 'fieldset',
        '#title' => $field['widget']['label'],
        '#description' => $field['widget']['description'],
        '#weight' => $field['widget']['weight'],
        '#attributes' => array(
          'class' => $field['multiple'] ? 'asset-field-multiple' : 'asset-field',
        ),
      );

      // Directory List Widget
      if ($field['widget']['type'] == 'directory') {
        $directory_options = asset_directory_options();

        // Multiple Directory List Widget
        if ($field['multiple']) {
          $delta = 0;
          foreach ($items as $data) {
            if ($data['aid']) {
              $form[$field['field_name']][$delta]['aid'] = array(
                '#type' => 'select',
                '#default_value' => $data['aid'],
                '#options' => $directory_options,
              );
              $delta++;
            }
          }
          foreach (range($delta, $delta + 10) as $delta) {
            $form[$field['field_name']][$delta]['aid'] = array(
              '#type' => 'select',
              '#default_value' => NULL,
              '#options' => $directory_options,
            );
          }
        }
        else {
          $delta = 0;
          $data = $items[$delta];
          $form[$field['field_name']][$delta]['aid'] = array(
            '#type' => 'select',
            '#default_value' => $data['aid'],
            '#options' => $directory_options,
          );
        }
      }
      else {

        // Multiple Asset Wizard Widget
        if ($field['multiple']) {
          $delta = 0;
          foreach ($items as $data) {
            if ($data['aid']) {
              $form[$field['field_name']][$delta]['aid'] = array(
                '#type' => 'assetfield',
                '#maxlength' => 255,
                '#default_value' => $data['aid'],
                '#asset_return_type' => $return_type,
                '#asset_config' => $field['field_name'],
              );
              $delta++;
            }
          }
          foreach (range($delta, $delta + 10) as $delta) {
            $form[$field['field_name']][$delta]['aid'] = array(
              '#type' => 'assetfield',
              '#maxlength' => 255,
              '#default_value' => NULL,
              '#asset_return_type' => $return_type,
              '#asset_config' => $field['field_name'],
            );
          }
        }
        else {
          $delta = 0;
          $data = $items[$delta];
          $form[$field['field_name']][$delta]['aid'] = array(
            '#type' => 'assetfield',
            '#default_value' => $data['aid'],
            '#attributes' => array(
              'class' => 'asset-cck-' . $return_type,
            ),
            '#asset_return_type' => $return_type,
            '#asset_config' => $field['field_name'],
          );
        }
      }
      return $form;
    case 'validate':
      if ($field['required']) {
        if (!count($items)) {
          form_set_error($field['field_name'], $field['widget']['label'] . ' is required.');
        }
      }
      return;
    case 'process form values':

      // Don't save empty fields except the first value
      foreach ($items as $delta => $item) {
        if ($item['aid'] == '' && $delta > 0) {
          unset($items[$delta]);
        }
      }
      break;
  }
}