You are here

nodeformsettings.module in Node and Comments Form Settings 7.2

File

nodeformsettings.module
View source
<?php

/**
 * Implements hook_enable().
 */
function nodeformsettings_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('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_preview' => 0,
    'nfs_publishingoptions' => 0,
    'nfs_revisionlog' => 0,
    'nfs_submit' => t('Submit'),
    'nfs_title_create' => t('Create !node_type'),
    'nfs_title_edit' => t('!node_title'),
  );
}

/**
 * Define all elements that need validation.
 */
function nodeformsettings_elements_validate() {
  return array(
    'nfs_title_create',
    'nfs_title_edit',
  );
}

/**
 * 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);
    }

    // Validation callback.
    $form['#validate'][] = 'nodeformsettings_settings_validate';

    // To save the values in an keyed array we need to define a custom submit callback.
    $form['#submit'][] = 'nodeformsettings_settings_submit';
  }
}
function nodeformsettings_form_node_form_alter(&$form, $form_state, $form_id) {

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

    // print '<pre>' . htmlentities(print_r($form, 1)) . '</pre>';
    $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);
          }
        }
      }
    }
  }
}

/**
 * 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 nodeformsettings_settings_validate($form, &$form_state) {
  $path = drupal_get_path('module', 'nodeformsettings') . '/includes/';

  // Get all the elements defined in the function.
  $elements = nodeformsettings_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 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'];

  // dprint_r($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']);
  }
}

/**
 * From http://drupal.org/node/426482 .
 */
function nodeformsettings_preprocess_page(&$vars) {
  if (isset($vars['node'])) {
    $settings = nodeformsettings_get_settings($vars['node']->type);
    if (isset($settings) && !empty($vars['node']) && isset($settings['nfs_hide_node_title']) && $settings['nfs_hide_node_title'] == 1) {
      $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($data) && module_exists('strongarm')) {
    $variables = array(
      'nodeformsettings_',
    );
    foreach ($data as $node_type) {
      foreach ($variables as $variable_name) {
        $pipe['variable'][] = "{$variable_name}_{$node_type}";
      }
    }
  }
}

/**
 * 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_node_type().
 */
function nodeformsettings_node_type_OLD($op, $info) {
}

/**
 * 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;
}

Functions

Namesort descending Description
nodeformsettings_elements_default Define the element and a default value.
nodeformsettings_elements_validate Define all elements that need validation.
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_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_OLD Implements hook_node_type().
nodeformsettings_node_type_update Implements hook_node_type_update().
nodeformsettings_preprocess_page From http://drupal.org/node/426482 .
nodeformsettings_process_format Custom callback function to process input format for all the fields.
nodeformsettings_purge Remove all unsused variables.
nodeformsettings_settings_submit Submit callback for the node form alter.
nodeformsettings_settings_validate We use this function to validate.