You are here

function filefield_paths_form_alter in File (Field) Paths 7

Same name and namespace in other branches
  1. 5 filefield_paths.module \filefield_paths_form_alter()
  2. 6 filefield_paths.module \filefield_paths_form_alter()

Implements hook_form_alter().

Parameters

$form:

File

./filefield_paths.module, line 40
Contains core functions for the File (Field) Paths module.

Code

function filefield_paths_form_alter(&$form) {

  // Force all File (Field) Paths uploads to go to the temporary file system
  // prior to being processed.
  if (isset($form['#entity']) && isset($form['#entity_type']) && isset($form['#bundle']) && $form['#type'] == 'form') {
    filefield_paths_temporary_upload_location($form);
  }
  $field_types = _filefield_paths_get_field_types();
  if (isset($form['#field']) && in_array($form['#field']['type'], array_keys($field_types))) {
    $entity_info = entity_get_info($form['#instance']['entity_type']);
    $settings = isset($form['#instance']['settings']['filefield_paths']) ? $form['#instance']['settings']['filefield_paths'] : array();
    $form['instance']['settings']['filefield_paths_enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable File (Field) Paths?'),
      '#default_value' => isset($form['#instance']['settings']['filefield_paths_enabled']) ? $form['#instance']['settings']['filefield_paths_enabled'] : TRUE,
      '#weight' => 2,
    );

    // Hide standard File directory field.
    $form['instance']['settings']['file_directory']['#states'] = array(
      'visible' => array(
        ':input[name="instance[settings][filefield_paths_enabled]"]' => array(
          'checked' => FALSE,
        ),
      ),
    );

    // File (Field) Paths fieldset element.
    $form['instance']['settings']['filefield_paths'] = array(
      '#type' => 'fieldset',
      '#title' => t('File (Field) Path settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 3,
      '#tree' => TRUE,
      '#states' => array(
        'visible' => array(
          ':input[name="instance[settings][filefield_paths_enabled]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );

    // Additional File (Field) Paths widget fields.
    $fields = module_invoke_all('filefield_paths_field_settings', $form['#field'], $form['#instance']);
    foreach ($fields as $name => $field) {

      // Attach widget fields.
      $form['instance']['settings']['filefield_paths'][$name] = array(
        '#type' => 'container',
      );

      // Attach widget field form elements.
      if (isset($field['form']) && is_array($field['form'])) {
        foreach (array_keys($field['form']) as $delta => $key) {
          $form['instance']['settings']['filefield_paths'][$name][$key] = $field['form'][$key];
          if (module_exists('token')) {
            $form['instance']['settings']['filefield_paths'][$name][$key]['#element_validate'][] = 'token_element_validate';
            $form['instance']['settings']['filefield_paths'][$name][$key]['#token_types'] = array(
              'file',
              $entity_info['token type'],
            );
          }

          // Fetch stored value from instance.
          if (isset($settings[$name][$key])) {
            $form['instance']['settings']['filefield_paths'][$name][$key]['#default_value'] = $settings[$name][$key];
          }
        }

        // Field options.
        $form['instance']['settings']['filefield_paths'][$name]['options'] = array(
          '#type' => 'fieldset',
          '#title' => t('@title options', array(
            '@title' => t($field['title']),
          )),
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
          '#weight' => 1,
          '#attributes' => array(
            'class' => array(
              "{$name} cleanup",
            ),
          ),
        );

        // Cleanup slashes (/).
        $form['instance']['settings']['filefield_paths'][$name]['options']['slashes'] = array(
          '#type' => 'checkbox',
          '#title' => t('Remove slashes (/) from tokens'),
          '#default_value' => isset($settings[$name]['options']['slashes']) ? $settings[$name]['options']['slashes'] : FALSE,
          '#description' => t('If checked, any slashes (/) in tokens will be removed from %title.', array(
            '%title' => t($field['title']),
          )),
        );

        // Cleanup field with Pathauto module.
        $form['instance']['settings']['filefield_paths'][$name]['options']['pathauto'] = array(
          '#type' => 'checkbox',
          '#title' => t('Cleanup using Pathauto'),
          '#default_value' => isset($settings[$name]['options']['pathauto']) && module_exists('pathauto') ? $settings[$name]['options']['pathauto'] : FALSE,
          '#description' => t('Cleanup %title using <a href="@pathauto">Pathauto settings</a>.', array(
            '%title' => t($field['title']),
            '@pathauto' => url('admin/config/search/path/settings'),
          )),
          '#disabled' => !module_exists('pathauto'),
        );

        // Transliterate field with Transliteration module.
        $form['instance']['settings']['filefield_paths'][$name]['options']['transliterate'] = array(
          '#type' => 'checkbox',
          '#title' => t('Transliterate'),
          '#default_value' => isset($settings[$name]['options']['transliterate']) && module_exists('transliteration') ? $settings[$name]['options']['transliterate'] : 0,
          '#description' => t('Provides one-way string transliteration (romanization) and cleans the %title during upload by replacing unwanted characters.', array(
            '%title' => t($field['title']),
          )),
          '#disabled' => !module_exists('transliteration'),
        );

        // Replacement patterns for field.
        if (module_exists('token')) {
          $form['instance']['settings']['filefield_paths']['token_tree'] = array(
            '#theme' => 'token_tree',
            '#token_types' => array(
              'file',
              $entity_info['token type'],
            ),
            '#dialog' => TRUE,
            '#weight' => 10,
          );
        }

        // Redirect
        $form['instance']['settings']['filefield_paths']['redirect'] = array(
          '#type' => 'checkbox',
          '#title' => t('Create Redirect'),
          '#description' => t('Create a redirect to the new location when a previously uploaded file is moved.'),
          '#default_value' => isset($settings['redirect']) ? $settings['redirect'] : FALSE,
          '#weight' => 11,
        );
        if (!module_exists('redirect')) {
          $form['instance']['settings']['filefield_paths']['redirect']['#disabled'] = TRUE;
          $form['instance']['settings']['filefield_paths']['redirect']['#description'] .= '<br />' . t('Requires the <a href="https://drupal.org/project/redirect" target="_blank">Redirect</a> module.');
        }

        // Retroactive updates.
        $form['instance']['settings']['filefield_paths']['retroactive_update'] = array(
          '#type' => 'checkbox',
          '#title' => t('Retroactive update'),
          '#description' => t('Move and rename previously uploaded files.') . '<div>' . t('<strong class="warning">Warning:</strong> This feature should only be used on developmental servers or with extreme caution.') . '</div>',
          '#weight' => 12,
        );

        // Active updating.
        $form['instance']['settings']['filefield_paths']['active_updating'] = array(
          '#type' => 'checkbox',
          '#title' => t('Active updating'),
          '#default_value' => isset($settings['active_updating']) ? $settings['active_updating'] : FALSE,
          '#description' => t('Actively move and rename previously uploaded files as required.') . '<div>' . t('<strong class="warning">Warning:</strong> This feature should only be used on developmental servers or with extreme caution.') . '</div>',
          '#weight' => 13,
        );
      }
    }
    $form['#submit'][] = 'filefield_paths_form_submit';
  }
}