You are here

commentformsettings.module in Node and Comments Form Settings 7.3

File

commentformsettings/commentformsettings.module
View source
<?php

/**
 * @file
 */

/**
 * Implements hook_enable().
 */
function commentformsettings_enable() {

  // If the variable is not initializated then errors shows up,
  // I don't like this since it seems is like playing with fire but
  // basically, for every content type, if the variable nodeformsettings_$type
  // does not exists, then create it with an empty array.
  // This should respect any content type where the variable has been set by the
  // user with custom values.
  $types = node_type_get_types();
  foreach ($types as $type => $content_type) {
    $variable = variable_get("commentformsettings_" . $type);
    if (!isset($variable)) {
      variable_set("commentformsettings_" . $type, array());
    }
  }
}

/**
 * Define the element and a default value.
 *
 * If you're making a patch define here the name of the element and the default
 * value, try to keep the array sorted by alphabetical order for readability
 * purposes.
 *
 * 1 => usually means Disabled
 * 0 => usually means Enabled
 */
function commentformsettings_elements_default() {
  return array(
    'cfs_anonymousname' => 0,
    'cfs_anonymousmail' => 0,
    'cfs_anonymoushomepage' => 0,
    'cfs_author' => 0,
    'cfs_comment_cancel' => 1,
    'cfs_inputformat' => 0,
    'cfs_newadmin' => 1,
    'cfs_submit' => t('Submit'),
    'cfs_title' => 0,
  );
}

/**
 * Define all elements that need validation.
 */
function commentformsettings_elements_validate() {
  return array(
    'cfs_comment_cancel',
    'cfs_newadmin',
  );
}

/**
 * Return the settings for a given content type using variable_get.
 *
 * param @type
 * content type machine name
 *
 * return array()
 */
function commentformsettings_get_settings($type) {
  return variable_get('commentformsettings_' . $type, '');
}

/**
 * Implements hook_form_alter().
 */
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);
            }
          }
        }
      }
    }
  }
}

/**
 * We use this function to validate.
 *
 * The reason why we don't use '#element_validate' in each form field
 * is because with this we have option to $form_state and with #element_validate
 * we only have access to the element that calls que validation function.
 */
function commentformsettings_settings_validate($form, &$form_state) {
  $path = drupal_get_path("module", "commentformsettings") . '/includes/';

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

  // Loop thought the array to build the function.
  foreach ($elements as $key) {
    include_once DRUPAL_ROOT . '/' . $path . 'validate_' . $key . '.inc';
    $function = '_validate_' . $key;
    if (function_exists($function)) {
      $function($form, $form_state);
    }
  }
}

/**
 * Submit callback for the node form alter.
 *
 * @see commentformsettings_form_alter()
 */
function commentformsettings_settings_submit($form, &$form_state) {

  // Get the values sent from the form and save them in $values.
  $values = $form_state['values'];

  // Save the value of $values['var'] in $name. This variable will
  // be used to define the name in variable_set($name, $values)
  // This will be something like commentformsettings_contenttype.
  $name = 'commentformsettings_' . $values['var_comments'];

  // Get the elements from the function and loop to get only the keys, not the values.
  $elements = commentformsettings_elements_default();
  foreach ($elements as $k => $v) {

    // Build the $ignore array with only the keys ($k).
    $ignore[] = $k;
  }

  // Add to the $ignore array the $name.
  $ignore[] = $name;

  // Loop thought the array of $values to unset everything but our values in $ignore.
  foreach ($values as $key => $value) {

    // If the key IS NOT in the $ignore array, then unset that value.
    if (!in_array($key, $ignore)) {
      unset($values[$key]);
    }
    else {

      // Build the $data array wich we will send to the variable_set function.
      $data[$key] = $value;
    }
  }
  variable_set($name, $data);
  if (isset($values['var_comments'])) {

    // Purge all variables created by hook_node_type.
    commentformsettings_purge($values['var_comments']);
  }
}

/**
 * @todo Please document this function.
 *
 * @see http://drupal.org/node/1354
 */
function commentformsettings_purge($type = NULL) {
  $elements = commentformsettings_elements_default();
  if (isset($type)) {
    variable_del('var_comments_' . $type);
    foreach ($elements as $k => $v) {
      variable_del($k . '_' . $type);
    }
  }
  else {

    // Remove all from all content types.
    foreach (node_type_get_names() as $type => $type_name) {
      variable_del('var_comments_' . $type);
      foreach ($elements as $k => $v) {
        variable_del($k . '_' . $type);
      }
    }
  }
}

/**
 * Implements hook_features_pipe_alter().
 *
 * For node component().
 */
function commentformsettings_features_pipe_node_alter(&$pipe, $data, $export) {
  if (!empty($export['features']['node']) && module_exists('strongarm')) {
    $map = features_get_default_map('variable');
    $variables = array(
      'commentformsettings_',
    );
    foreach ($export['features']['node'] as $node_type) {
      foreach ($variables as $variable_name) {
        $variable_name = "{$variable_name}_{$node_type}";
        if (isset($map[$variable_name]) && $map[$variable_name] != $module_name) {
          $export['dependencies'][$map[$variable_name]] = $map[$variable_name];
        }
        else {
          $pipe['variable'][] = $variable_name;
        }
      }
    }
  }
}

/**
 * Implements hook_node_type_delete().
 */
function commentformsettings_node_type_delete($info) {
  variable_del('commentformsettings_' . $info->type);
}

/**
 * Implements hook_node_type_update().
 */
function commentformsettings_node_type_update($info) {
  if (!empty($info->old_type) && $info->old_type != $info->type) {
    $setting = variable_get('commentformsettings_' . $info->old_type, '');
    variable_del('commentformsettings_' . $info->old_type);
    variable_set('commentformsettings_' . $info->type, $setting);
  }
}

/**
 * Implements hook_element_info_alter().
 */
function commentformsettings_element_info_alter(&$type) {

  // If nodeformsettings is not enabled, use this callback directly.
  // If not, the callback is processed from nodeformsettings module.
  if (!module_exists('nodeformsettings')) {
    if (isset($type['text_format']['#process'])) {
      foreach ($type['text_format']['#process'] as &$callback) {
        if ($callback === 'filter_process_format') {
          $callback = 'commentformsettings_process_format';
        }
      }
    }
  }
}

/**
 * Custom callback function to process input format for all the fields.
 *
 * @param Array $element
 *   The input element for which the format is to be processed.
 *
 * @return Array
 *  Return updated element with necessary changes.
 */
function commentformsettings_process_format($element) {

  // If the filter was not already processed in nodeformsettings module,
  // then process it here.
  if (!module_exists('nodeformsettings')) {
    $element = filter_process_format($element);
  }

  // Make sure we have an entity and a type here.
  if (isset($element['#entity'], $element['#entity']->node_type)) {

    // For comment, the '->node_type' field is available.
    $type = $element['#entity']->node_type;

    // For comments, the type will be 'comment_node_<content type>'
    // Field of comment is 'comment_body'.
    if (strpos($type, 'comment_node') !== FALSE) {
      $fields = array(
        'comment_body',
      );

      // Get settings for this node type.
      $settings = commentformsettings_get_settings(str_replace('comment_node_', '', $type));
      $setting = 'cfs_inputformat';

      // Hide the 'text format' pane below certain text area fields.
      if (isset($settings[$setting], $element['#field_name']) && count($settings) && $settings[$setting] == 1 && in_array($element['#field_name'], $fields)) {

        // Set access to FALSE.
        $element['format']['#access'] = FALSE;
      }
    }
  }
  return $element;
}

/**
 * Function to handle submit for setting default for all content types.
 *
 * @param Array $form
 * @param Array $form_state
 */
function commentformsettings_set_default_to_all($form, &$form_state) {

  // Save the current form.
  commentformsettings_settings_submit($form, $form_state);

  // Get current content type.
  $type = $form_state['values']['type'];
  $current_type_title = $form_state['values']['name'];

  // Get values from current content types settings.
  $variable = variable_get("commentformsettings_" . $type);

  // Get all the types.
  $types = node_type_get_types();

  // For each type, set the current content type values.
  foreach ($types as $type => $content_type) {
    variable_set("commentformsettings_" . $type, $variable);
  }
  drupal_set_message(t('The content type !type has been updated.', array(
    '!type' => '<em>' . $current_type_title . '</em>',
  )));
  drupal_goto('admin/structure/types');
}

/**
 * Function to handle submit for resetting to default values.
 *
 * @param Array $form
 * @param Array $form_state
 */
function commentformsettings_reset_default($form, &$form_state) {

  // Get current content type.
  $type = $form_state['values']['type'];
  $current_type_title = $form_state['values']['name'];

  // Set default values for this type.
  $name = 'commentformsettings_' . $type;
  variable_set($name, commentformsettings_elements_default());
  drupal_set_message(t('The content type !type has been updated.', array(
    '!type' => '<em>' . $current_type_title . '</em>',
  )));
  drupal_goto('admin/structure/types');
}

Functions

Namesort descending Description
commentformsettings_elements_default Define the element and a default value.
commentformsettings_elements_validate Define all elements that need validation.
commentformsettings_element_info_alter Implements hook_element_info_alter().
commentformsettings_enable Implements hook_enable().
commentformsettings_features_pipe_node_alter Implements hook_features_pipe_alter().
commentformsettings_form_alter Implements hook_form_alter().
commentformsettings_get_settings Return the settings for a given content type using variable_get.
commentformsettings_node_type_delete Implements hook_node_type_delete().
commentformsettings_node_type_update Implements hook_node_type_update().
commentformsettings_process_format Custom callback function to process input format for all the fields.
commentformsettings_purge @todo Please document this function.
commentformsettings_reset_default Function to handle submit for resetting to default values.
commentformsettings_settings_submit Submit callback for the node form alter.
commentformsettings_settings_validate We use this function to validate.
commentformsettings_set_default_to_all Function to handle submit for setting default for all content types.