You are here

function commentformsettings_form_alter in Node and Comments Form Settings 7.3

Same name and namespace in other branches
  1. 6.3 commentformsettings/commentformsettings.module \commentformsettings_form_alter()
  2. 6.2 commentformsettings/commentformsettings.module \commentformsettings_form_alter()
  3. 7.2 commentformsettings/commentformsettings.module \commentformsettings_form_alter()

Implements hook_form_alter().

File

commentformsettings/commentformsettings.module, line 75

Code

function commentformsettings_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'node_type_form') {
    $path = drupal_get_path("module", "commentformsettings") . '/includes/';
    $form['var_comments'] = array(
      '#type' => 'hidden',
      '#value' => $form['#node_type']->type,
    );
    $settings = commentformsettings_get_settings($form['#node_type']->type);

    // Only active the configuration options if the comments module is installed.
    if (module_exists("comment") && isset($form['comment'])) {
      include_once DRUPAL_ROOT . '/' . $path . 'settings_comments.inc';
      _commentformsettings_settings_form($form, $settings);
    }
    $form['#validate'][] = 'commentformsettings_settings_validate';

    // To save the values in an keyed array we need to define a custom submit callback.
    $form['#submit'][] = 'commentformsettings_settings_submit';
  }
  if (isset($form['#node'])) {
    if ($form_id == "comment_node_" . $form['#node']->type . "_form") {

      // Start off with the node visible from the form, it might be overridden
      // later.
      $node = $form['#node'];

      // print '<pre>' . htmlentities(print_r($form, 1)) . '</pre>';
      $path = drupal_get_path("module", "commentformsettings") . '/includes/';

      // on comment_form there's no $form['#node'] so we can't load the $node->type
      // we just take the argument
      // since we don't have node/nid when creating a comment on a separate form
      // we have to filter the two cases to check what argument to take and load
      // the node
      // TODO: find a better way to do this
      if (arg(0) == 'node' && is_numeric(arg(1))) {
        $node = node_load(arg(1));
      }
      if (arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2))) {
        $node = node_load(arg(2));
      }
      if (arg(0) == 'comment' && arg(1) == 'edit' && is_numeric(arg(2))) {
        $node = node_load(arg(2));
      }

      // User profile (When using content profile).
      if (module_exists('content_profile')) {
        if (arg(0) == 'user' && is_numeric(arg(1))) {
          $content = array();
          foreach (content_profile_get_types('names') as $type => $type_name) {
            $node = content_profile_load($type, arg(1));
            if ($node) {
              break;
            }
          }
        }
      }
      $settings = commentformsettings_get_settings($node->type);

      // Get all the elements defined in the function.
      $elements = commentformsettings_elements_default();

      // Loop thought the array to build the function.
      foreach ($elements as $key => $vals) {
        if (isset($settings[$key])) {

          // Ignore the elements in the variable. In this case, we only ignore cfs_pnc because
          // it is a cointainer with other values, an array or arrays
          $ignore = array(
            'cfs_pnc',
            'cfs_inputformat',
          );

          // If the $key is not in the array $ignore detect the functions.
          if (!in_array($key, $ignore)) {
            include_once DRUPAL_ROOT . '/' . $path . 'option_' . $key . '.inc';
            $function = '_option_' . $key;
            if (function_exists($function)) {
              $function($form, $form_state, $settings, $node);
            }
          }
        }
      }
    }
  }
}