You are here

function image_attach_form_alter in Image 6

Same name and namespace in other branches
  1. 5.2 contrib/image_attach/image_attach.module \image_attach_form_alter()
  2. 5 contrib/image_attach/image_attach.module \image_attach_form_alter()

Implementation of hook_form_alter().

File

contrib/image_attach/image_attach.module, line 185
image_attach.module

Code

function image_attach_form_alter(&$form, $form_state, $form_id) {

  // Node edit form.
  if (isset($form['type']['#value']) && $form['type']['#value'] != 'image') {
    $type = $form['type']['#value'];

    // If enabled adjust the form.
    if ($form_id == $type . '_node_form' && variable_get('image_attach_' . $type, 0)) {
      $node = $form['#node'];
      _image_check_settings();
      $value = !empty($node->new_image) ? '#value' : '#default_value';
      $form['#attributes'] = array(
        "enctype" => "multipart/form-data",
      );

      // Add a custom submit handler so we can handle image creation on-the-fly
      $form['#validate'][] = 'image_attach_validate';

      // Add a custom submit handler so we can clean up the selection box items.
      $form['#submit'][] = 'image_attach_node_form_submit';

      // Check permissions and settings
      $may_attach = user_access('attach images');
      $may_attach_existing = variable_get('image_attach_existing', 1);
      $may_upload = user_access('create images');
      $has_existing_images = !empty($node->iids);
      $maximum_images = variable_get('image_attach_maximum_' . $type, 0);

      // Display the image attach form only if user can attach images, AND
      // it is allowed to attach existing images or the user is allowed to create new images
      if ($may_attach && ($may_attach_existing || $may_upload)) {
        $form['image_attach'] = array(
          '#type' => 'fieldset',
          '#title' => t('Attached images'),
          '#collapsible' => TRUE,
          '#collapsed' => empty($node->iids),
        );
        if ($maximum_images) {
          $form['image_attach']['#description'] = format_plural($maximum_images, 'You may attach 1 image.', 'You may attach up to @count images.');
        }
        if ($has_existing_images) {
          $form['image_attach']['image_thumbnail']['#theme'] = 'image_attach_form_thumbnails';
          $form['image_attach']['image_thumbnail']['#tree'] = TRUE;
          $weight = 0;
          foreach ($node->iids as $iid) {
            $image = node_load($iid);
            $form['image_attach']['image_thumbnail'][$iid] = array(
              'id' => array(
                '#type' => 'hidden',
                '#value' => $iid,
              ),
              'image' => array(
                '#type' => 'markup',
                '#value' => image_display($image, 'thumbnail'),
              ),
              'delete' => array(
                '#type' => 'checkbox',
              ),
              'title' => array(
                '#type' => 'markup',
                '#value' => check_plain($image->title),
              ),
              'weight' => array(
                '#type' => 'weight',
                '#title' => t('Weight'),
                '#delta' => count($node->iids),
                '#default_value' => isset($form_state['values']['image_thumbnail'][$iid]) ? $form_state['values']['image_thumbnail'][$iid]['weight'] : $weight++,
              ),
            );
          }
        }

        // Only show selection box of image nodes if the user may attach some,
        // or if there are existings ones that may be removed.
        if ($may_attach_existing || $has_existing_images) {
          $form['image_attach']['iids'] = array(
            '#type' => 'select',
            $value => empty($node->iids) ? NULL : $node->iids,
            '#multiple' => TRUE,
            '#size' => 6,
          );

          // User may attach already existing images: show a selection box containing all images.
          if ($may_attach_existing) {
            $form['image_attach']['iids']['#title'] = t('Existing images');
            $form['image_attach']['iids']['#options'] = _image_attach_get_image_nodes();
            if ($may_upload) {
              $form['image_attach']['iids']['#description'] = t('Choose existing images if you do not upload a new one.');
            }
            else {
              $form['image_attach']['iids']['#description'] = t('Choose existing images to attach.');
            }
          }
          else {
            $form['image_attach']['iids']['#title'] = t('Attached images');
            $form['image_attach']['iids']['#options'] = _image_attach_get_image_nodes($node->iids);
            $form['image_attach']['iids']['#description'] = t('You can remove a previously attached image by unselecting it.');
          }
        }

        // User may create images, add upload form elements.
        if ($may_upload) {
          $form['image_attach']['image'] = array(
            '#type' => 'file',
            '#size' => 40,
            '#title' => t('Upload image'),
          );
          $form['image_attach']['image_title'] = array(
            '#type' => 'textfield',
            '#title' => t('Image title'),
            $value => '',
            '#description' => t('The title the image will be shown with. Leave blank to use the filename.'),
          );

          // Provide an additional submit button, which adds an image and redirects
          // the user to the node edit form.
          $form['image_attach']['image_attach_multiple'] = array(
            '#type' => 'submit',
            '#value' => t('Upload'),
            '#validate' => array(
              'image_attach_validate',
            ),
            '#submit' => array(
              'image_attach_image_add_submit',
            ),
          );
        }
      }
    }
  }
}