You are here

nodeformsettings.module in Node and Comments Form Settings 7.3

File

nodeformsettings.module
View source
<?php

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

  // If the variable is not initialized 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("nodeformsettings_" . $type);
    if (!isset($variable)) {
      variable_set("nodeformsettings_" . $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
 *
 * All keys MUST start with nfs_
 *
 * 1 => means Disabled (It Hides)
 * 0 => means Enabled (It shows)
 *
 * except when there are multiple options
 */
function nodeformsettings_elements_default() {
  return array(
    'nfs_author_information' => 0,
    'nfs_cancel' => array(
      'nfs_cancel_status' => 1,
      'nfs_cancel_behaviour' => 0,
    ),
    'nfs_comments' => 0,
    'nfs_hide_node_title' => 0,
    'nfs_inputformat' => 0,
    'nfs_path' => 0,
    'nfs_menu_settings' => 0,
    'nfs_publishingoptions' => 0,
    'nfs_revisionlog' => 0,
    'nfs_submit' => '',
    'nfs_title_create' => '',
    'nfs_title_edit' => '',
    'nfs_sitemap_display' => 0,
    'nfs_title_delete' => '',
    'nfs_desc_delete' => '',
    'nfs_promote_display' => 0,
    'nfs_sticky_display' => 0,
    'nfs_default_vertical_tab' => '-none-',
    'nfs_title_description' => '',
    'nfs_body_description' => '',
  );
}

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

/**
 * Implements hook_form_alter().
 */
function nodeformsettings_form_alter(&$form, $form_state, $form_id) {

  // When configuring the content type settings.
  if ($form_id == 'node_type_form') {

    // Get the path to the includes dir.
    $path = drupal_get_path("module", "nodeformsettings") . '/includes/';

    // Save the name of the variable for use in the submit callback.
    $form['var'] = array(
      '#type' => 'hidden',
      '#value' => $form['#node_type']->type,
    );

    // Get the default settings using variable_get and the current content type.
    $settings = nodeformsettings_get_settings($form['#node_type']->type);
    if (isset($settings)) {

      // Load the settings.
      include_once DRUPAL_ROOT . '/' . $path . 'settings_node.inc';

      // Get the form with the settings.
      _nodeformsettings_settings_form($form, $settings);
    }

    // To save the values in an keyed array we need to define a custom submit callback.
    $form['#submit'][] = 'nodeformsettings_settings_submit';
  }
}

/**
 * Implements hook_form_alter().
 */
function nodeformsettings_form_node_form_alter(&$form, $form_state, $form_id) {

  // On node form.
  if (!empty($form['#node_edit_form']) || !empty($form['#node_delete_form'])) {
    $node = $form['#node'];

    // Get the path to the includes dir.
    $path = drupal_get_path("module", "nodeformsettings") . '/includes/';

    // Get the settings.
    $settings = nodeformsettings_get_settings($node->type);

    // Get all the elements defined in the function.
    $elements = nodeformsettings_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. We do this because in this particular case this change is not being made
        // in a hook_form_alter, but a preprocess_page function, if we don't ignore it we'll get an error.
        // In case of nfs_inputformat, the change is applied using hook_element_info_alter().
        // If more changes are made out the hook_form_alter, then add those elements to this array.
        $ignore = array(
          "nfs_hide_node_title",
          "nfs_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);
          }
        }
      }
    }
  }
}

/**
 * Submit callback for the node form alter.
 *
 * @see nodeformsettings_form_alter()
 */
function nodeformsettings_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 nodeformsettings_contenttype.
  $name = 'nodeformsettings_' . $values['var'];

  // Get the elements from the function and loop to get only the keys, not the values.
  $elements = nodeformsettings_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'])) {

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

/**
 * Implements hook_preprocess_page().
 */
function nodeformsettings_preprocess_page(&$vars) {
  if (isset($vars['node'])) {
    $settings = nodeformsettings_get_settings($vars['node']->type);

    // Hide title on nodes if set to yes and not on edit page.
    if (!empty($vars['node']) && isset($settings['nfs_hide_node_title']) && $settings['nfs_hide_node_title'] == 1 && arg(2) != 'edit') {
      $vars['title'] = '';
    }
  }
}

/**
 * Remove all unsused variables.
 */
function nodeformsettings_purge($type = NULL) {
  $elements = nodeformsettings_elements_default();
  if (isset($type)) {
    variable_del('var_' . $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_' . $type);
      foreach ($elements as $k => $v) {
        variable_del($k . '_' . $type);
      }
    }
  }
}

/**
 * Implements hook_features_pipe_alter().
 *
 * For node component().
 */
function nodeformsettings_features_pipe_node_alter(&$pipe, $data, $export) {
  if (!empty($export['features']['node']) && module_exists('strongarm')) {
    $map = features_get_default_map('variable');
    $variables = array(
      'nodeformsettings_',
    );
    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 nodeformsettings_node_type_delete($info) {
  variable_del('nodeformsettings_' . $info->type);
}

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

/**
 * Implements hook_element_info_alter().
 */
function nodeformsettings_element_info_alter(&$type) {
  if (isset($type['text_format']['#process'])) {
    foreach ($type['text_format']['#process'] as &$callback) {
      if ($callback === 'filter_process_format') {
        $callback = 'nodeformsettings_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 nodeformsettings_process_format($element) {
  $element = filter_process_format($element);

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

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

    // Array of field names to restrict. Default body.
    $fields = array(
      'body',
    );

    // Get settings for this node type.
    $settings = nodeformsettings_get_settings($type);
    $setting = 'nfs_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;
    }
  }
  elseif (isset($element['#entity'], $element['#entity']->node_type) && module_exists('commentformsettings')) {
    $element = commentformsettings_process_format($element);
  }
  return $element;
}

/**
 * Function to handle delete form display.
 *
 * @param array $form
 * @param array $form_state
 * @param string $form_id
 */
function nodeformsettings_form_node_delete_confirm_alter(&$form, $form_state, $form_id) {
  $form['#node_delete_form'] = 'delete';
  nodeformsettings_form_node_form_alter($form, $form_state, $form_id);
}

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

  // Save the current form.
  nodeformsettings_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("nodeformsettings_" . $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("nodeformsettings_" . $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 nodeformsettings_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 = 'nodeformsettings_' . $type;
  variable_set($name, nodeformsettings_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
nodeformsettings_elements_default Define the element and a default value.
nodeformsettings_element_info_alter Implements hook_element_info_alter().
nodeformsettings_enable Implements hook_enable().
nodeformsettings_features_pipe_node_alter Implements hook_features_pipe_alter().
nodeformsettings_form_alter Implements hook_form_alter().
nodeformsettings_form_node_delete_confirm_alter Function to handle delete form display.
nodeformsettings_form_node_form_alter Implements hook_form_alter().
nodeformsettings_get_settings Return the settings for a given content type using variable_get.
nodeformsettings_node_type_delete Implements hook_node_type_delete().
nodeformsettings_node_type_update Implements hook_node_type_update().
nodeformsettings_preprocess_page Implements hook_preprocess_page().
nodeformsettings_process_format Custom callback function to process input format for all the fields.
nodeformsettings_purge Remove all unsused variables.
nodeformsettings_reset_default Function to handle submit for resetting to default values.
nodeformsettings_settings_submit Submit callback for the node form alter.
nodeformsettings_set_default_to_all Function to handle submit for setting default for all content types.