drupal
Подписаться на эту метку по RSS
Drupal 7: изменяем статусные сообщения
Рубрика: Web-разработка и SEOМетки: drupal | php | интернет | программирование | сайты
Дата: 28/11/2015 15:16:15
Drupal 7, после создания очередной ноды или комментария, выводит сообщение, что-то вроде «Forum topic %title has been created.» (пример для форумного топика). Вы, конечно же, хотите написать здесь что-то своё. А что, если текст зависит от того опубликована нода или отправлена на подтверждение администратору? Это как-раз мой случай, потому что я использую модуль Simple Regex Filter, чтобы отсеивать подозрительные сообщения на форумах.
Самое простое решение, позволяющее изменить все сообщения вида «@type %title has been created.», — установить модуль Strings Override, и вперёд. Для сложных случаев, когда нужна какая-то логика: устанавливается модуль Disable Messages, а затем используется экшн Rules «Set a message».
Однако в моем теме Disable Message ломает стили, да и не слишком гибкое это решение.
Поэтому вот ещё версия. Если комментарии на английском вам не понятны, пишите ниже, я постараюсь объяснить или помочь переделать код ваши нужды.
/**
* We customize 'Forum topic ... has been created.' message when
* it goes to the approval queue. This is done in two steps:
*
* 1. Add new status messages implementing hook_node_insert().
* This is done for both Published and Unpublished nodes.
* For Published nodes we just duplicate the old message.
* For Unpublished nodes we inform that the message is waiting for approval.
*
* 2. Remove old status message implementing hook_form_FORM_ID_alter().
* We override 'submit' action where we remove old messages in case it
* matches the pattern '@type %title has been created.'.
* We only remove a single match, so even if we duplicate a message
* on the first step, one of them will still be displayed.
*
* We customize also the Drupal behavior when a Forum Topic was updated and
* the new revision goes to the Approval Queue. Now it redirects a user to
* the Forum Category and shows the similar status message to inform the user.
* We use hook_node_update() for that.
*/
/**
* Implements hook_node_insert()
*/
function MY_MODULE_node_insert($node) {
/**
* Add custom status messages when a node is inserted
*/
if ($node->type == 'forum') {
if ($node->status == NODE_NOT_PUBLISHED) {
drupal_set_message(t('Your forum topic has been queued for review by site administrators and will be published after approval.'));
}
elseif ($node->status == NODE_PUBLISHED) {
$message = t('@type %title has been created.', array('@type' => node_type_get_name($node), '%title' => $node->title));
drupal_set_message($message, 'status', TRUE);
}
}
}
/**
* Implements hook_node_update()
*/
function MY_MODULE_node_update($node) {
/**
* If a Forum Topic was updated and the new revision goes to the Approval Queue
* we redirect a user to the Forum Category and show a message to keep him informed.
*/
if ($node->type == 'forum') {
if ($node->status == NODE_NOT_PUBLISHED) {
drupal_set_message(t('Your forum topic has been queued for review by site administrators and will be published after approval.'));
drupal_goto('forum/' . $node->forum_tid);
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
* @see MY_MODULE_node_insert()
*/
function MY_MODULE_form_forum_node_form_alter(&$form, &$form_state, $form_id) {
/**
* Override 'submit' action for 'forum_node_form' form
*/
$form['actions']['submit']['#submit'][] = '_MY_MODULE_form_forum_node_form_submit';
}
/**
* @see MY_MODULE_form_forum_node_form_alter()
*/
function _MY_MODULE_form_forum_node_form_submit($form, &$form_state) {
/**
* Remove the old status message 'Forum topic ... has been created.'
*/
if (!empty($_SESSION['messages']['status'])) {
$node = $form_state['node'];
$old_message = t('@type %title has been created.', array('@type' => node_type_get_name($node), '%title' => $node->title));
$old_message_key = array_search($old_message, $_SESSION['messages']['status']);
if ($old_message_key !== FALSE) {
unset($_SESSION['messages']['status'][$old_message_key]);
// Reset array indexes. Otherwise it doesn’t work with some theme templates.
$_SESSION['messages']['status'] = array_values($_SESSION['messages']['status']);
// Remove the empty status message wrapper if no other messages have been set.
if (empty($_SESSION['messages']['status'])) {
unset($_SESSION['messages']['status']);
}
}
}
}