You are here

function webform_protected_downloads_form_alter in Webform Protected Downloads 6

Same name and namespace in other branches
  1. 7 webform_protected_downloads.module \webform_protected_downloads_form_alter()

Implementation of hook_form_alter(). Doc says that $form_state is passed by reference, but that generates warnings: warning: Parameter 2 to webform_protected_downloads_form_alter() expected to be a reference, value given in /includes/common.inc on line 2883. Since we don't use that parameter here, we can safely set it to value instead of reference.

Parameters

array $form :

array $form_state :

string $form_id :

Return value

void

File

./webform_protected_downloads.module, line 823
This file contains hook declarations and functions for the Webform Protected Downloads module.

Code

function webform_protected_downloads_form_alter(&$form, $form_state, $form_id) {
  if (strpos($form_id, 'webform_client_form_') !== FALSE) {

    // trigger processing after the form has been submitted and handled by
    // webform
    $form['#submit'][] = 'webform_protected_downloads_process_submissions';
  }

  // update the file upload form, so that protected files can't be deleted,
  // listed or have the private option removed
  if (strpos($form_id, '_node_form') !== FALSE && webform_protected_downloads_node_is_webform($form['#node'])) {

    // add information about this node, so that we can load it while
    // processing the upload_js form
    $form['attachments']['wrapper']['wpd_node_nid'] = array(
      '#type' => 'hidden',
      '#value' => $form['#node']->nid,
    );
    if (count($form['attachments']['wrapper']['files'])) {
      webform_protected_downloads_adjust_upload_form($form['attachments']['wrapper']['files'], $form['#node']->nid);
      if (webform_protected_downloads_node_has_protected_files($form['#node']->nid)) {
        $form['attachments']['#description'] .= "<br />" . t('One ore more of the following files are protected and can not be listed or deleted. You can change this on the <a href="@protected_downloads_page">Protected Downloads page</a>.', array(
          '@protected_downloads_page' => url('node/' . $form['#node']->nid . '/protected-downloads'),
        ));
      }
    }
  }
  switch ($form_id) {

    // update the file upload form, so that protected files can't be deleted,
    // listed or have the private option removed
    case 'upload_js':
      if (count($form['files'])) {

        // try to load the node
        $node = node_load($form['#post']['wpd_node_nid']);

        // if we have a node to operate on we can proceed
        if ($node->nid) {
          webform_protected_downloads_adjust_upload_form($form['files'], $node->nid);
        }
      }
      break;

    // disable redirect options on the form configuration form if the redirect
    // to the downloads page has been activated for protected downloads
    case 'webform_configure_form':
      if (webform_protected_downloads_get_configuration($form['nid']['#value'], 'redirect')) {
        $element =& $form['submission']['redirection'];
        $element['redirect']['#attributes']['disabled'] = 'disabled';
        $element['redirect']['#value'] = $element['redirect']['#default_value'];
        $element['redirect_url']['#attributes']['disabled'] = 'disabled';
        $element['redirect_url']['#value'] = $element['redirect_url']['#default_value'];
        $element['#description'] .= '<br />' . t('This setting has been deactivated, because this webform is configured to redirect to the downloads page for the protected files. You can change this on the <a href="@protected_downloads_page">Protected Downloads page</a>.', array(
          '@protected_downloads_page' => url('node/' . $form['nid']['#value'] . '/protected-downloads'),
        ));
      }
      break;

    // disable the mandatory checkbox on the webform component overview, for
    // the component that is in use as mail for protected downloads
    case 'webform_components_form':
      if (webform_protected_downloads_node_has_protected_files($form['#node']->nid)) {
        foreach ($form['components'] as $cid => &$element) {
          if (webform_protected_downloads_get_configuration($form['#node']->nid, 'mail_field_cid', NULL) == $cid) {
            $element['mandatory']['#disabled'] = TRUE;
          }
        }
      }
      break;

    // disable the mandatory checkbox on the webform component edit form, if
    // the current component is the one used as mail for protected downloads
    case 'webform_component_edit_form':
      if (webform_protected_downloads_node_has_protected_files($form['nid']['#value'])) {
        if (webform_protected_downloads_get_configuration($form['nid']['#value'], 'mail_field_cid', NULL) == $form['cid']['#value']) {
          $form['validation']['mandatory']['#disabled'] = TRUE;
          $form['validation']['mandatory']['#default_value'] = 1;
          $form['validation']['mandatory']['#description'] .= '<br />' . t('This field has been disabled because one or more files attached to this node have been protected. This field is used as the mail address for the confirmation mail that users need to access the protected files.');
          $form['display']['disabled']['#default_value'] = 0;
          $form['display']['disabled']['#disabled'] = TRUE;
          $form['display']['disabled']['#description'] .= '<br />' . t('This field has been disabled because one or more files attached to this node have been protected. This component is used as the mail address for the confirmation mail that users need to access the protected files so it must be possible to edit it.');
        }
      }
      break;
  }

  // check if this is a webform form and set a message if there is a problem
  // with this webform
  if (strpos($form_id, 'webform_') !== FALSE) {
    if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) != 'webform-results') {
      $node = node_load(arg(1));
      if (webform_protected_downloads_node_is_webform($node) && $node->wpd_valid === FALSE) {
        _webform_protected_downloads_set_warning();
      }
    }
  }
}