You are here

function filefield_paths_form_alter in File (Field) Paths 6

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

Implements hook_form_alter().

File

./filefield_paths.module, line 53
Contains core functions for the FileField Paths module.

Code

function filefield_paths_form_alter(&$form, $form_state, $form_id) {
  $ffp = array();

  // Invoke hook_filefield_paths_form_alter().
  foreach (module_implements('filefield_paths_form_alter') as $module) {
    $function = "{$module}_filefield_paths_form_alter";
    $function($form, $ffp);
  }

  // If supporting module enabled, show FileField Paths settings form.
  if (count($ffp) > 0) {
    $fields = module_invoke_all('filefield_paths_field_settings');
    foreach ($ffp as $field_name => $field_data) {
      $result = db_fetch_object(db_query("SELECT * FROM {filefield_paths} WHERE type = '%s' AND field = '%s'", $field_data['type'], $field_name));
      if (!empty($result)) {
        foreach ($fields as &$field) {
          $field['settings'] = unserialize($result->{$field}['sql']);
        }
        unset($field);
      }
      $count = 0;
      foreach ($fields as $name => $field) {
        $count++;
        if (isset($field['form']) && is_array($field['form'])) {
          $keys = array_keys($field['form']);
          for ($i = 1; $i < count($field['form']); $i++) {
            $field['form'][$keys[$i]]['#weight'] = ($count - 1) * 3 + 2 + $i;
          }
          unset($keys);
          $field_data['form_path'] = array_merge_recursive($field_data['form_path'], $field['form']);
        }
        $field_data['form_path']['#tree'] = TRUE;
        $field_data['form_path'][$name]['#weight'] = ($count - 1) * 3;

        // Set defualt value for patterns.
        if (isset($field['settings']['value'])) {
          $field_data['form_path'][$name]['#default_value'] = $field['settings']['value'];
          if (isset($field['data'])) {
            foreach ($field['data'] as $key => $value) {
              $field_data['form_path'][$value]['#default_value'] = $field['settings'][$key];
            }
          }
        }
        $field_data['form_path']["{$name}_cleanup"] = array(
          '#type' => 'fieldset',
          '#title' => t('!title cleanup settings', array(
            '!title' => $field['title'],
          )),
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
          '#weight' => ($count - 1) * 3 + 1,
          '#attributes' => array(
            'class' => array(
              "{$name} cleanup",
            ),
          ),
        );

        // Cleanup field with Pathauto module.
        $field_data['form_path']["{$name}_cleanup"]["{$name}_pathauto"] = array(
          '#type' => 'checkbox',
          '#title' => t('Cleanup using Pathauto') . '.',
          '#default_value' => isset($field['settings']['pathauto']) ? $field['settings']['pathauto'] : 0,
          '#description' => t('Cleanup !title using !url', array(
            '!title' => $field['title'],
            '!url' => l(t('Pathauto settings'), 'admin/build/path/pathauto'),
          )),
        );
        if (!module_exists('pathauto')) {
          $field_data['form_path']["{$name}_cleanup"]["{$name}_pathauto"]['#disabled'] = TRUE;
          $field_data['form_path']["{$name}_cleanup"]["{$name}_pathauto"]['#default_value'] = 0;
        }

        // Convert field to lower case.
        $field_data['form_path']["{$name}_cleanup"]["{$name}_tolower"] = array(
          '#type' => 'checkbox',
          '#title' => t('Convert to lower case') . '.',
          '#default_value' => isset($field['settings']['tolower']) ? $field['settings']['tolower'] : 0,
          '#description' => t('Convert !title to lower case', array(
            '!title' => $field['title'],
          )) . '.',
        );

        // Transliterate field with Transliteration module.
        $field_data['form_path']["{$name}_cleanup"]["{$name}_transliterate"] = array(
          '#type' => 'checkbox',
          '#title' => t('Transliterate') . '.',
          '#default_value' => isset($field['settings']['transliterate']) ? $field['settings']['transliterate'] : 0,
          '#description' => t('Transliterate !title', array(
            '!title' => $field['title'],
          )) . '.',
        );
        if (!module_exists('transliteration')) {
          $field_data['form_path']["{$name}_cleanup"]["{$name}_transliterate"]['#disabled'] = TRUE;
          $field_data['form_path']["{$name}_cleanup"]["{$name}_transliterate"]['#default_value'] = 0;
        }

        // Replacement patterns for field.
        $field_data['form_path']["{$name}_tokens"] = array(
          '#type' => 'fieldset',
          '#title' => t('!title replacement patterns', array(
            '!title' => $field['title'],
          )),
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
          '#description' => theme('filefield_paths_token_help'),
          '#weight' => ($count - 1) * 3 + 2,
          '#attributes' => array(
            'class' => $name . ' tokens',
          ),
        );
      }

      // Retroactive updates.
      $field_data['form_path']['retroactive_update'] = array(
        '#type' => 'checkbox',
        '#title' => t('Retroactive update'),
        '#description' => t('Move and rename previously uploaded files') . '.' . '<br /> <strong style="color: #FF0000;">' . t('Warning') . ':</strong> ' . t('This feature should only be used on developmental servers or with extreme caution') . '.',
        '#weight' => 10,
      );

      // Active updating.
      $field_data['form_path']['active_updating'] = array(
        '#type' => 'checkbox',
        '#title' => t('Active updating'),
        '#default_value' => variable_get("ffp_{$field_data['type']}_{$field_name}", 0),
        '#description' => t('Actively move and rename previously uploaded files as required') . '.' . '<br /> <strong style="color: #FF0000;">' . t('Warning') . ':</strong> ' . t('This feature should only be used on developmental servers or with extreme caution') . '.',
        '#weight' => 11,
      );
      if (!in_array('filefield_paths_form_submit', $form['#submit'])) {
        $form['#submit'][] = 'filefield_paths_form_submit';
      }
    }
  }
}