You are here

promote_disable.module in Promote Disable 7

Same filename and directory in other branches
  1. 8 promote_disable.module

Primarily Drupal hooks and helper functions. This is the main module file for Promote Disable.

The development of this module is sponsored by Eon Creative Ltd. http://www.eoncreative.com/

File

promote_disable.module
View source
<?php

/**
 * @file
 * Primarily Drupal hooks and helper functions.
 * This is the main module file for Promote Disable.
 *
 * The development of this module is sponsored by
 * Eon Creative Ltd. http://www.eoncreative.com/
 */

/**
 * Implements hook_menu().
 */
function promote_disable_menu() {
  $items = array();
  $items['admin/config/content/promote_disable'] = array(
    'title' => 'Promote disable',
    'description' => 'Configuration for Promote disable module.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'promote_disable_form',
    ),
    'access arguments' => array(
      'administer content types',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Page callback: Promote Disable settings.
 *
 * @see promote_disable_menu()
 */
function promote_disable_form($form, &$form_state) {
  $node_types = _promote_disable_node_types();
  $form['promote_to_front_page'] = array(
    '#type' => 'fieldset',
    '#title' => t('Promote to front page'),
  );
  $form['promote_to_front_page']['promote_disable_node_types'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#size' => count($node_types),
    '#title' => t('Content types'),
    '#default_value' => variable_get('promote_disable_node_types', array()),
    '#options' => $node_types,
    '#description' => t('Select the content types on which you would like to disable the "Promoted to front page" option.'),
  );
  $form['sticky'] = array(
    '#type' => 'fieldset',
    '#title' => t('Make content sticky'),
  );
  $form['sticky']['promote_disable_sticky_node_types'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#size' => count($node_types),
    '#title' => t('Content types'),
    '#default_value' => variable_get('promote_disable_sticky_node_types', array()),
    '#options' => $node_types,
    '#description' => t('Select the content types on which you would like to disable the "Sticky option.'),
  );
  $form['promote_disabled_node_admin_content'] = array(
    '#type' => 'checkbox',
    '#default_value' => variable_get('promote_disabled_node_admin_content', FALSE),
    '#title' => t('Remove options.'),
    '#description' => t('Remove the "Promote selected content to the front page" and "Demote selected content from the front page" options from the !content_overview_page.', array(
      '!content_overview_page' => l(t('content overview page'), 'admin/content', array(
        'attributes' => array(
          'target' => '_blank',
        ),
      )),
    )),
  );
  return system_settings_form($form);
}

/**
 * Simple function to return a FAPI select options array.
 *
 * @see promote_disable_form()
 */
function _promote_disable_node_types() {
  $options = array();
  $node_types = node_type_get_types();
  foreach ($node_types as $key => $value) {
    $options[$key] = $value->name;
  }
  asort($options);
  return $options;
}

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

  // Are we on a node form?
  if (strpos($form_id, '_node_form') !== FALSE) {

    // The current form's node type.
    $node_type = $form['type']['#value'];

    // Our saved front page node types.
    $node_types = variable_get('promote_disable_node_types', array());

    // Is this node type one we've set?
    if (in_array($node_type, $node_types)) {

      // Unset the form element.
      unset($form['options']['promote']);

      // Add a submit function to the start of the #submit array.
      array_unshift($form['#submit'], '_promote_disable_form_submit');
    }

    // Our saved sticky page node types.
    $node_types = variable_get('promote_disable_sticky_node_types', array());

    // Is this node type one we've set?
    if (in_array($node_type, $node_types)) {

      // Unset the form element.
      unset($form['options']['sticky']);

      // Add a submit function to the start of the #submit array.
      array_unshift($form['#submit'], '_promote_disable_sticky_form_submit');
    }
  }
  elseif ($form_id == 'node_type_form') {

    // The current form's node type.
    $node_type = $form['type']['#default_value'];

    // Our saved front page node types.
    $node_types = variable_get('promote_disable_node_types', array());

    // Is this node type one we've set?
    if (in_array($node_type, $node_types)) {

      // Remove the option.
      unset($form['workflow']['node_options']['#options']['promote']);
    }

    // Our saved sticky node types.
    $node_types = variable_get('promote_disable_sticky_node_types', array());

    // Is this node type one we've set?
    if (in_array($node_type, $node_types)) {

      // Remove the option.
      unset($form['workflow']['node_options']['#options']['sticky']);
    }
  }
}

/**
 * Changes the value of the "promoted" form field if it is set.
 *
 * @see promote_disable_form_alter()
 */
function _promote_disable_form_submit($form, &$form_state) {

  // Change the appropriate values.
  $form_state['input']['promote'] = 0;
  $form_state['values']['promote'] = 0;
}

/**
 * Changes the value of the "sticky" form field if it is set.
 *
 * @see promote_disable_form_alter()
 */
function _promote_disable_sticky_form_submit($form, &$form_state) {

  // Change the appropriate values.
  $form_state['input']['sticky'] = 0;
  $form_state['values']['sticky'] = 0;
}

/**
 * Implements hook_node_presave().
 */
function promote_disable_node_presave($node) {

  // Our saved node types.
  $node_types = variable_get('promote_disable_node_types', array());

  // Is this node type one we've set?
  if (in_array($node->type, $node_types)) {
    $node->promote = 0;
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Remove promote and demote update options on admin/content if configured.
 */
function promote_disable_form_node_admin_content_alter(&$form, &$form_state, $form_id) {
  if (variable_get('promote_disabled_node_admin_content', FALSE)) {
    $options = array(
      'demote',
      'promote',
    );
    foreach ($options as $key) {
      unset($form['admin']['options']['operation']['#options'][$key]);
    }
  }
}

Functions

Namesort descending Description
promote_disable_form Page callback: Promote Disable settings.
promote_disable_form_alter Implements hook_form_alter().
promote_disable_form_node_admin_content_alter Implements hook_form_FORM_ID_alter().
promote_disable_menu Implements hook_menu().
promote_disable_node_presave Implements hook_node_presave().
_promote_disable_form_submit Changes the value of the "promoted" form field if it is set.
_promote_disable_node_types Simple function to return a FAPI select options array.
_promote_disable_sticky_form_submit Changes the value of the "sticky" form field if it is set.