View source
<?php
define('ONLYONE_FORMAT_INITIAL_DRUSH', ' (');
define('ONLYONE_FORMAT_FINAL_DRUSH', ')');
define('ONLYONE_FORMAT_INITIAL_ADMIN', ' <strong>(');
define('ONLYONE_FORMAT_FINAL_ADMIN', ') </strong>');
function onlyone_help($path, $arg) {
switch ($path) {
case 'admin/help#onlyone':
if (variable_get('onlyone_new_menu_entry')) {
$onlyone_add_page = t('<a href="@link">Add content (Only One)</a> page', array(
'@link' => url('onlyone/add'),
));
}
else {
$onlyone_add_page = t('<em>Add content (Only One)</em> page (onlyone/add)');
}
$routes = array(
'@settings-page' => url('admin/config/content/onlyone/settings'),
'@content' => url('admin/content'),
'!onlyone_add_page' => $onlyone_add_page,
'@add-content' => url('node/add'),
);
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Allow a content type only once (Only One) module allows the creation of Only One content per language in the selected content types for this configuration.') . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Configuring content types') . '</dt>';
$output .= '<dd>' . t('To configure the content types to allow for Only One content per language, visit the <a href="@config-page">Only One</a> page, in the <em>Available content types for Only One</em> section and <em>check</em> the content types that should have Only One content per language. For this you need the <em>Administer Only One</em> permission.', array(
'@config-page' => url('admin/config/content/onlyone'),
)) . '</dd>';
$output .= '<dt>' . t('Configuring module settings') . '</dt>';
$output .= '<dd>' . t('To configure the module settings visit the <a href="@settings-page">Settings</a> page, if you want to have the configured content types in a new menu entry named <em>Add content (Only One)</em> you must check the option <em>Show configured content types in a new menu entry?</em>, the new menu link will be available in the <a href="@content">Content</a> page as an action link to the !onlyone_add_page, then the <a href="@add-content">Add content</a> page will show the not configured content types. For this you need the <em>Administer Only One</em> permission.', $routes) . '</dd>';
$output .= '<dt>' . t('Creating content') . '</dt>';
$output .= '<dd>' . t('Once you try to <a href="@add-content">Add content</a>, if the chosen content type is configured to have Only One content and it already has one content created in the actual language, you will be redirected to <em>edit</em> the content, otherwise, you will go to create a new onw.', array(
'@add-content' => url('node/add'),
)) . '</dd>';
$output .= '</dl>';
return $output;
}
}
function onlyone_admin_paths() {
if (variable_get('node_admin_theme')) {
$paths = array(
'onlyone/add' => TRUE,
);
return $paths;
}
}
function onlyone_menu() {
$items['admin/config/content/onlyone'] = array(
'title' => 'Only One',
'description' => 'Configure the content types to allow for Only One node per language.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'config_content_types',
),
'access arguments' => array(
'administer onlyone',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'onlyone.admin.inc',
);
$items['admin/config/content/onlyone/default'] = array(
'title' => 'Only One',
'description' => 'Configure the content types to allow for Only One node per language.',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 2,
);
$items['admin/config/content/onlyone/settings'] = array(
'title' => 'Settings',
'description' => 'Only One Settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'onlyone_admin_settings',
),
'access arguments' => array(
'administer onlyone',
),
'type' => MENU_LOCAL_TASK,
'file' => 'onlyone.admin.inc',
'weight' => 2,
);
if (variable_get('onlyone_new_menu_entry')) {
$items['onlyone/add'] = array(
'title' => 'Add content (Only One)',
'page callback' => 'onlyone_add_page',
'access callback' => '_node_add_access',
'file' => 'onlyone.pages.inc',
'menu_name' => 'management',
'type' => MENU_NORMAL_ITEM,
'weight' => -1,
);
}
return $items;
}
function onlyone_menu_link_alter(&$item) {
if ($item['page callback'] == 'onlyone_add_page') {
$item['plid'] = 9;
}
}
function onlyone_menu_local_tasks_alter(&$data, $router_item, $root_path) {
if (variable_get('onlyone_new_menu_entry') && $root_path == 'admin/content') {
$item = menu_get_item('onlyone/add');
if ($item['access']) {
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
'#weight' => -1,
);
}
}
}
function onlyone_theme_registry_alter(&$theme_registry) {
if (variable_get('onlyone_new_menu_entry')) {
$theme_registry['node_add_list']['file'] = 'onlyone.pages.inc';
$theme_registry['node_add_list']['theme path'] = drupal_get_path('module', 'onlyone');
$theme_registry['node_add_list']['function'] = 'onlyone_node_add_list';
}
}
function onlyone_preprocess_node_add_list(&$variables) {
$onlyone_content_types = variable_get('onlyone_node_types');
$current_path = current_path();
$onlyone_new_menu_entry = variable_get('onlyone_new_menu_entry');
if ($current_path == 'onlyone/add') {
$variables['message'] = t('You have not configured any content type yet, go to the <a href="@config-content-types">Only One page</a> to configure the content types.', array(
'@config-content-types' => url('admin/config/content/onlyone'),
));
}
else {
if ($onlyone_new_menu_entry) {
$variables['message'] = t('All the content types are configured to have Only One node. Go to the <a href="@add-onlyone-content-type">Add content (Only One)</a> page to create or edit content.', array(
'@add-onlyone-content-type' => url('onlyone/add'),
));
}
else {
$variables['message'] = t('You have not created any content types yet. Go to the <a href="@create-content">Add content type</a> page to create content types.', array(
'@create-content' => url('admin/structure/types/add'),
));
}
}
module_load_include('inc', 'onlyone', 'onlyone.helpers');
foreach ($variables['content'] as $key => $element) {
$content_type = substr($element['href'], 9);
if (in_array($content_type, $onlyone_content_types)) {
if (!($onlyone_new_menu_entry xor $current_path == 'onlyone/add')) {
if (_onlyone_exists_nodes_content_type($content_type)) {
$content_type_name = $variables['content'][$key]['title'];
$variables['content'][$key]['title'] = t('@content_type_name (Edit)', array(
'@content_type_name' => $content_type_name,
));
}
$original_description = empty($variables['content'][$key]['description']) ? '' : rtrim($variables['content'][$key]['description'], '.') . '.';
$variables['content'][$key]['description'] = t('!description <strong>Only a single node can be created and edited</strong>.', array(
'!description' => $original_description,
));
}
else {
unset($variables['content'][$key]);
}
}
}
}
function onlyone_form_node_form_alter(&$form, &$form_state, $form_id) {
$onlyone_content_types = variable_get('onlyone_node_types');
$content_type = $form['type']['#value'];
$node = $form_state['node'];
if (isset($onlyone_content_types) && in_array($content_type, $onlyone_content_types, TRUE) && (!isset($node->nid) || isset($node->is_new))) {
module_load_include('inc', 'onlyone', 'onlyone.helpers');
$nid = _onlyone_exists_nodes_content_type($content_type);
if ($nid) {
drupal_goto('node/' . $nid . '/edit');
}
}
}
function onlyone_permission() {
return array(
'administer onlyone' => array(
'title' => t('Administer Only One'),
'description' => t('Allow access to configure the module settings.'),
),
);
}
function onlyone_node_type_delete($info) {
$content_type = $info->type;
module_load_include('inc', 'onlyone', 'onlyone.helpers');
_onlyone_delete_content_type_config($content_type);
}
function onlyone_node_insert($node) {
module_load_include('inc', 'onlyone', 'onlyone.helpers');
_onlyone_rebuild_menu($node->type);
}
function onlyone_node_delete($node) {
module_load_include('inc', 'onlyone', 'onlyone.helpers');
drupal_register_shutdown_function('_onlyone_rebuild_menu', $node);
}
function onlyone_node_validate($node, $form, &$form_state) {
$onlyone_content_types = variable_get('onlyone_node_types');
if (in_array($node->type, $onlyone_content_types)) {
module_load_include('inc', 'onlyone', 'onlyone.helpers');
$nid = _onlyone_exists_nodes_content_type($node->type, $node->language);
if ($nid && $nid != $node->nid) {
$existing_node = node_load($nid);
$values = array(
'%content_type' => $node->type,
'@href' => url(drupal_get_path_alias('node/' . $existing_node->nid)),
'!title' => $existing_node->title,
'%language' => t('Language neutral'),
);
form_set_error('language', t("The content type %content_type is configured to have Only One node per language but the node <a href='@href'>!title</a> exists for the %language language.", $values));
}
}
}