You are here

function webform_protected_downloads_file_widget_after_build in Webform Protected Downloads 7

Implementation of hook_after_build().

1 string reference to 'webform_protected_downloads_file_widget_after_build'
webform_protected_downloads_form_alter in ./webform_protected_downloads.module
Implementation of hook_form_alter().

File

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

Code

function webform_protected_downloads_file_widget_after_build($form_element, &$form_state) {

  // get the protected files of the current node
  $files = $form_state['complete form']['#node']->wpd['protected_files'];

  // flag to see if the current field has protected files
  $field_has_protected_files = FALSE;

  // get the fields properties
  $field = field_widget_field($form_element, $form_state);
  $single_field_adjustments = array(
    t('The <em>removal</em> of this file has been disabled.'),
  );
  if (!empty($field['settings']['display_field'])) {
    $single_field_adjustments[] = t('The <em>display checkbox</em> for this file has been disabled.');
  }
  foreach ($files as $file) {
    if ($form_element['#field_name'] == $file->field && isset($form_element[$file->weight])) {

      // remove the remove button
      $form_element[$file->weight]['remove_button']['#access'] = FALSE;

      // disable the display checkbox, if available (only for file fields with
      // "Enable display field"-option)
      if (!empty($field['settings']['display_field'])) {
        $form_element[$file->weight]['display']['#attributes']['disabled'] = 'disabled';
      }
      if ($field['cardinality'] == 1) {
        $form_element[$file->weight]['#description'] .= ' ' . t('This file has been protected by Webform Protected Downloads. !adjustments If you want to remove this file unprotect it first on the <a href="@protected_downloads_page">Protected Downloads page</a>.', array(
          '@protected_downloads_page' => url('node/' . $form_state['complete form']['#node']->nid . '/protected-downloads'),
          '!adjustments' => implode(' ', $single_field_adjustments),
        ));
      }
      $field_has_protected_files = TRUE;
    }
  }

  // adjust the description for fields that allow multiple files
  if ($field_has_protected_files && $field['cardinality'] != 1) {
    $adjustments = array(
      t('The <em>removal</em> of those files has been disabled.'),
    );
    if (!empty($field['settings']['display_field'])) {
      $adjustments[] = t('The <em>display checkbox</em> for those files has been disabled.');
    }
    $form_element['#description'] .= ' ' . t('One or more of the following files have been protected using the Webform Protected Downloads module. !adjustments If you want to remove one of those files, unprotect it first on the <a href="@protected_downloads_page">Protected Downloads page</a>.', array(
      '@protected_downloads_page' => url('node/' . $form_state['complete form']['#node']->nid . '/protected-downloads'),
      '!adjustments' => implode(' ', $adjustments),
    ));
  }
  return $form_element;
}