4

Hey there I want to change or hide a drupal message from the CommentForm class.

In my entity_insert() hook I set a new message.

I have an idea of how to do it but still no idea of where to do it.

idea:

whether unset the $_SESSION['messages'] and set it with my message after I unset it.

Or

drupal_get_message($type, TRUE).

problem my message will be set before the one I want to exclude. So if I unset the message array my message will be gone, too.

So my Question is where to put those in myModule?

I also tried to hook in the specific form and add a submit handler but that doesn't work.

/**
 * Implements hook_form_FORM_ID_alter().
 */
function my_module_form_comment_form_alter(&$form, FormStateInterface $form_state) {
  $form['actions']['submit']['#submit'][]  = 'my_module_comment_form_submit';
}

/**
 * Submit handler.
 */
function my_module_comment_form_submit(&$form, FormStateInterface $form_state) {
  $message = $_SESSION['messages'];
}

Ok I've figured it out. But that solution is pretty ugly in my opinion.

I do this in a hook_preprocess.

/**
 * Implements hook_preprocess_HOOK().
 */
function my_module_preprocess_node(&$variables) {
  $user = Drupal::currentUser();
  $entity_id = $variables['attributes']['data-history-node-id'];
  $entity_type = $variables['elements']['comment']['#entity_type'];
  $bundle = $variables['elements']['comment']['#bundle'];
  $comment_type = FieldConfig::load($entity_type . '.' . $bundle . '.comment');
  $maxCommmentUser = $comment_type->getThirdPartySetting('my_module', 'edit-limit-per-user', FALSE) + 1;
  $limitUser = $comment_type->getThirdPartySetting('my_module', 'edit-limit-per-user', FALSE);
  $currentCommentUser = my_module_get_user($entity_id);
  $maxCommentNode = $comment_type->getThirdPartySetting('my_module', 'edit-limit-per-node', FALSE) + 1;
  $limitNode = $comment_type->getThirdPartySetting('my_module', 'edit-limit-per-node', FALSE);
  $currentCommentNode = my_module_get_node($entity_id);
  if ($maxCommentNode) {
    if ($maxCommentNode <= $currentCommentNode && !$user->hasPermission('bypass comment limit')) {
      drupal_get_messages('status', TRUE);
      drupal_set_message(t('The Node you want to comment has reached its maximum of @maxcommentnode comments', ['@maxcommentnode' => $limitNode]));

    }
  }
  if ($maxCommmentUser) {
    if ($maxCommmentUser <= $currentCommentUser && !$user->hasPermission('bypass comment limit')) {
      drupal_get_messages('status', TRUE);
      drupal_set_message(t('You have commented @maxcomment times, please edit or delete some of your older comments', ['@maxcomment' => $limitUser]));
    }
  }
}
  • Did you try unsetting only $_SESSION['messages']['status'] also? – Codium Aug 25 '16 at 8:23
  • Yes my problem was that in my hook the fully message does not exist yet so I have to get it at a later point to check on it an manipulate it. – Insasse Aug 25 '16 at 8:27
  • so maybe set in using different type? – Codium Aug 25 '16 at 8:49
4

I was seeking a similar solution and found this great post describing using a hook_preprocess_HOOK(), in this case, hook_preprocess_status_messages() - https://stackoverflow.com/questions/38788080/remove-default-message-in-drupal-8

For my purpose to override a message, I use a simple strpos in the logic:

/**
 * Implements hook_preprocess_HOOK().
 *
 * @param $variables
 */
function mymodule_preprocess_status_messages(&$variables) {
  if(isset($variables['message_list']['status'])){
    $status_messages = $variables['message_list']['status'];
    foreach($status_messages as $delta => $message) {
      if (strpos((string) $message, 'The message that I am expecting') !== FALSE) {
        $variables['message_list']['status'][$delta] = "Some different message text";
      }
    }
  }
}
2

I think the answer might be not to use hook_entity_insert(). By that time, it is too late: the content has already been created, so the system will create the message accordingly.

As the hook_entity_insert documentation says: hook_entity_insert responds to the creation of a new entity.

Maybe try another hook from the Entity API, one that does not cause the unwanted message to be generated.

1

Assuming the messages in question are form_errors from validation of the form:

Rather than altering the messages after the fact you could override/replace the #validate function(s) that produce the errors in the first place (from hook_form_alter) and provide your own custom errors.

  • I tried that too unfortunately this message still shows up :-/ – Insasse Aug 25 '16 at 9:32
  • The message is a status message not an error message. – Insasse Aug 25 '16 at 12:42
0
/**
 * Implements hook_preprocess_HOOK().
 */
function comment_limit_preprocess_node(&$variables) {
  if (in_array('node-max', $_SESSION['messages']['status']) && in_array('user-max', $_SESSION['messages']['status'])) {
    drupal_get_messages('status', TRUE);
    drupal_set_message(t('The comment limit for this node has been reached!'));
    drupal_set_message(t('Your comment limit has been reached!'));
  }
  elseif (in_array('node-max', $_SESSION['messages']['status'])) {
    drupal_get_messages('status', TRUE);
    drupal_set_message(t('The comment limit for this node has been reached!'));
  }
  elseif (in_array('user-max', $_SESSION['messages']['status'])) {
    drupal_get_messages('status', TRUE);
    drupal_set_message(t('Your comment limit has been reached!'));
  }
}

This looks a little prettier and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.