You are here

function asset_widget in Asset 6

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

Implementation of hook_widget().

Attach a single form element to the form. It will be built out and validated in the callback(s) listed in hook_elements. We build it out in the callbacks rather than here in hook_widget so it can be plugged into any module that can provide it with valid $field information.

Content module will set the weight, field name and delta values for each form element. This is a change from earlier CCK versions where the widget managed its own multiple values.

If there are multiple values for this field, the content module will call this function as many times as needed.

Parameters

$form: the entire form array, $form['#node'] holds node information

$form_state: the form_state, $form_state['values'] holds the form values.

$field: the field array

$delta: the order of this item in the array of subelements (0, 1, 2, etc)

Return value

the form item for a single element for this field

File

inc/modules/asset_content.inc, line 145

Code

function asset_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  foreach ($items as $key => $item) {
    $a = asset_load(array(
      'aid' => $item['aid'],
    ));
    $items[$key]['value'] = $a->filename;
  }
  $field_name = $field['field_name'];

  //	$form = array($field_name => array('#tree' => TRUE));
  $form[$field['field_name']] = array(
    '#tree' => TRUE,
  );

  // If the field has multiple values
  if ($field['multiple']) {

    // Currently the fieldset is disabled because the asset javascript doesn't like it > name collision
    drupal_add_js(drupal_get_path('module', 'asset') . '/misc/asset_more.js');
    $delta = 0;

    // Render link fields for all the entered values
    foreach ($items as $data) {
      if (is_array($data) && $data['aid']) {
        _asset_widget_form($form[$field['field_name']][$delta], $field, $data, $delta);
        $delta++;
      }
    }

    // Render one additional new asset field
    foreach (range($delta, $delta) as $delta) {
      _asset_widget_form($form[$field['field_name']][$delta], $field, array(), $delta);
    }

    // Create a wrapper for additional fields
    $id = str_replace('_', '-', $field['field_name']) . '-wrapper';
    $form[$field['field_name']]['wrapper'] = array(
      '#type' => 'markup',
      '#value' => '<div id="' . $id . '" class="clear-block"></div>',
    );

    // Add 'More' Javascript Callback
    $form[$field['field_name']]['more-url'] = array(
      '#type' => 'hidden',
      '#value' => url('asset/widget/js/' . $field['type_name'] . '/' . $field['field_name'], array(
        'absolute' => true,
      )),
      '#attributes' => array(
        'class' => 'more-links',
      ),
      '#id' => str_replace('_', '-', $field['field_name']) . '-more-url',
    );

    // Add Current Field Count
    $form[$field['field_name']]['count'] = array(
      '#type' => 'hidden',
      '#value' => $delta,
      '#id' => str_replace('_', '-', $field['field_name']) . '-count',
    );

    // Add More Button
    $form[$field['field_name']]['more'] = array(
      '#type' => 'button',
      '#value' => t('More Assets'),
      '#name' => 'more',
      '#id' => str_replace('_', '-', $field['field_name']) . '-more',
    );
  }
  else {
    module_invoke('i18n', 'language_rtl') ? $rtl = "right" : ($rtl = "left");

    // Show the one and only value field
    $form[$field_name][0]['value'] = array(
      '#type' => 'textfield',
      '#title' => t($field['widget']['label']),
      //   '#description' => t($field['widget']['description']),
      '#default_value' => $items[0]['options'],
    );

    // Set the aid (hidden)
    $form[$field_name][0]['aid'] = array(
      '#type' => 'hidden',
      '#default_value' => $items[0]['aid'],
    );

    // Set the preview
    if (!empty($items[0]['value']) || isset($_POST[$field_name][0]['aid']) && is_numeric($_POST[$field_name][0]['aid'])) {

      // When in preview mode, $items[0]['value'] is not set, so we need to get the $_POST data
      $aid = !empty($items[0]['value']) ? $items[0]['aid'] : filter_xss($_POST[$field_name][0]['aid']);
      $is_image = array(
        'jpg',
        'jpeg',
        'png',
        'gif',
        'tif',
        'tiff',
        'bmp',
      );
      $a = asset_load(array(
        'aid' => $aid,
      ));

      // Preview the image
      if (in_array($a->extension, $is_image)) {
        if (variable_get('asset_imagecache', 0)) {
          $presets = _imagecache_get_presets();
          $preset = $presets[variable_get('asset_imagecache', 0)];
          $preview = theme('imagecache', $preset, str_replace("//", "/", $a->filepath));
        }
        else {
          $asset = array(
            'aid' => $aid,
            'format' => 'image',
            'formatter' => 'asset',
            'width' => '120',
            'height' => '80',
          );
          $preview = asset_render_macro($asset);
        }
        $form[$field_name][0]['preview'] = array(
          '#prefix' => '<div id="preview_' . $field_name . '_0" style="float:' . $rtl . '">',
          '#suffix' => '</div>',
          '#value' => $preview,
        );
      }
      else {

        // Try to auto-detect the format
        $form[$field_name][0]['preview'] = array(
          '#prefix' => '<div id="preview_' . $field_name . '_0" style="float:' . $rtl . '">',
          '#suffix' => '</div>',
          '#value' => asset_preview($aid),
        );
      }
    }
    else {
      $form[$field_name][0]['preview'] = array(
        '#value' => '<div id="preview_' . $field_name . '_0" style="float:' . $rtl . '"></div>',
      );
    }

    // Set the caption
    $l_text = '<img src="' . base_path() . drupal_get_path('module', 'asset') . '/misc/lullacons/doc-option-add.png' . '" alt="' . t("Insert asset") . '" title="' . t("Insert asset") . '"/>';
    $l = l($l_text, 'asset/wizard/textfield', array(
      'html' => true,
      'query' => 'textfield=' . $field_name . '[0][value]&aidfield=' . $field_name . '[0][aid]&delta=0',
      'attributes' => array(
        'class' => 'asset-textfield-link',
        'id' => 'asset-link-' . $field_name . '-0-value',
        'onclick' => "window.open(this.href, 'asset_textfield_link', 'width=614,height=400,scrollbars=yes,status=yes,resizable=yes,toolbar=no,menubar=no'); return false;",
      ),
    ));
    $form[$field_name][0]['caption'] = array(
      '#type' => 'textfield',
      '#maxlength' => '256',
      '#default_value' => $items[0]['caption'],
      '#description' => 'Enter the caption for this asset',
      '#prefix' => '<div style="margin-' . $rtl . ':130px"><table style="width:400px;margin-top:-15px;border-collapse:inherit"><tr><td style="padding:0px;margin:0px;text-align:center;padding-top:16px;width:10px" valign="top">' . $l . '</td><td>',
      '#suffix' => '</td></tr></table></div>',
    );

    // Set the copyright
    $l_text = '<img src="' . base_path() . drupal_get_path('module', 'asset') . '/misc/lullacons/doc-option-remove.png' . '" alt="' . t("Delete asset") . '" title="' . t("Delete asset") . '"/>';
    $l = l($l_text, '', array(
      'html' => true,
      'attributes' => array(
        'onclick' => "\n\t\t\t\t\t\t\$('#edit-" . str_replace("_", "-", $field_name) . "-0-value').val('');\n\t\t\t\t\t\t\$('#edit-" . str_replace("_", "-", $field_name) . "-0-aid').val('');\n\t\t\t\t\t\t\$('#edit-" . str_replace("_", "-", $field_name) . "-0-caption').val('');\n\t\t\t\t\t\t\$('#edit-" . str_replace("_", "-", $field_name) . "-0-copyright').val('');\n\t\t\t\t\t\t\$('#preview_" . $field_name . "_0').html('');\n\t\t\t\t\t\treturn false;",
      ),
    ));
    $form[$field_name][0]['copyright'] = array(
      '#type' => 'textfield',
      '#maxlength' => '256',
      '#default_value' => $items[0]['copyright'],
      '#description' => 'Enter the copyright for this asset',
      '#prefix' => '<div style="margin-' . $rtl . ':130px"><table style="width:400px;margin-top:-30px;border-collapse:inherit"><tr><td style="padding:0px;margin:0px;text-align:center;padding-top:16px;width:10px" valign="top">' . $l . '</td><td>',
      '#suffix' => '</td></tr></table></div>',
    );
    $form[$field_name][0]['#prefix'] = '<div class="clear-block">';
    $form[$field_name][0]['#suffix'] = '</div>';
  }
  if (isset($field['widget']['description']) && !empty($field['widget']['description'])) {
    $form[$field_name][0]['description'] = array(
      '#value' => '<div class="description">' . $field['widget']['description'] . '</div>',
    );
  }
  return $form;
}