You are here

node_add_order.module in Util 6.3

Same filename and directory in other branches
  1. 7 contribs/node_add_order/node_add_order.module

Changes the order of content types on the node/add page.

File

contribs/node_add_order/node_add_order.module
View source
<?php

/**
 * @file
 * Changes the order of content types on the node/add page.
 */

/**
 * Implements hook_menu().
 */
function node_add_order_menu() {
  $items = array();
  $items['admin/content/types/order'] = array(
    'title' => 'Order create content',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_add_order_settings',
    ),
    'access callback' => '_node_add_access',
    'weight' => 10,
  );
  return $items;
}

/**
 * Implements hook_theme_registry_alter().
 */
function node_add_order_theme_registry_alter(&$theme_registry) {
  $path = drupal_get_path('module', 'node_add_order');
  $theme_registry['node_add_list']['file'] = 'node_add_order.module';
  $theme_registry['node_add_list']['include_files'][0] = 'node_add_order.module';
  $theme_registry['node_add_list']['theme_path'] = $path;
  $theme_registry['node_add_list']['theme_paths'][0] = $path;
  $theme_registry['node_add_list']['function'] = 'theme_node_add_order';
}

/**
 * Change the order of the create content list.
 * Original copied from theme_node_add_list().
 */
function theme_node_add_order($content) {
  $output = '';
  $order = variable_get('node_add_order_sort', 'alpha');
  if ($content) {
    foreach ($content as $key => $item) {
      switch ($order) {

        // Alphabetically.
        case 'alpha':
          $content[$key]['node_add_order'] = $item['title'];
          break;

        // Number of nodes.
        case 'count':
          $query = "SELECT COUNT(n.nid) FROM {node} n " . "INNER JOIN {node_type} t ON n.type = t.type " . "WHERE t.name = '%s' ";
          $count = db_result(db_query($query, $item['title']));

          // Go negative to sort descending.
          $content[$key]['node_add_order'] = -$count;
          break;

        // Last created.
        case 'last':
          $query = "SELECT MAX(n.created) FROM {node} n " . "INNER JOIN {node_type} t ON n.type = t.type " . "WHERE t.name = '%s' ";
          $last = db_result(db_query($query, $item['title']));

          // Go negative to sort descending.
          $content[$key]['node_add_order'] = -$last;
          break;
      }
    }
    uasort($content, '_node_add_order_compare');
    $output .= '<dl class="node-type-list">';
    foreach ($content as $item) {
      $title = $item['title'];
      if ($order == 'count') {
        $title .= ' (' . -$item['node_add_order'] . ')';
      }
      $output .= '<dt>' . l($title, $item['href'], $item['localized_options']) . '</dt>';
      $output .= '<dd>' . filter_xss_admin($item['description']) . '</dd>';
    }
    $output .= '</dl>';
  }
  return $output;
}

/**
 * Sort comparison function.
 */
function _node_add_order_compare($a, $b) {
  if ($a['node_add_order'] == $b['node_add_order']) {
    return 0;
  }
  return $a['node_add_order'] < $b['node_add_order'] ? -1 : 1;
}

/**
 * Settings page.
 */
function node_add_order_settings() {
  $form = array();
  $form['intro'] = array(
    '#type' => 'markup',
    '#value' => '<p>' . t('This page allows you to change the order of the content types on the "Create content" page.') . '</p>',
  );
  $options = array(
    'alpha' => t('Alphabetically'),
    'count' => t('Number of uses'),
    'last' => t('Last created'),
  );
  $form['node_add_order_sort'] = array(
    '#type' => 'radios',
    '#options' => $options,
    '#default_value' => variable_get('node_add_order_sort', 'alpha'),
    '#title' => t('Order of content types'),
    '#description' => t('Select the method by which you wish to order (sort) the content types.'),
    '#attributes' => array(
      'class' => 'container-inline',
    ),
  );
  return system_settings_form($form);
}

Functions

Namesort descending Description
node_add_order_menu Implements hook_menu().
node_add_order_settings Settings page.
node_add_order_theme_registry_alter Implements hook_theme_registry_alter().
theme_node_add_order Change the order of the create content list. Original copied from theme_node_add_list().
_node_add_order_compare Sort comparison function.