3

How do I programmatically hide the Authored by part in a content type using hooks?

  • 2
    You know you can disable this manually by editing the article content type and unchecking Display author and date information. in the Display settings section? – Neograph734 May 28 '16 at 8:39
4

I assume you need to hide the Authored by part basing on some conditions you check at run-time. If you need to hide that only for a content type, for example for the Article content type, then you can change settings in admin/structure/types/manage/article.

screenshot

If you want to hide that information basing on the user ID of the user who created the node, then you could use the following code.

function mymodule_preprocess_node(&$vars) {
  $node = $vars['node'];

  // Remove that information if the node has been created from the user #1.  
  if ($node->uid == 1) {
    $vars['display_submitted'] = FALSE;
    $vars['submitted'] = '';
    $vars['user_picture'] = '';
  }
}

Notice I am setting the same variables set by template_preprocess_node(), which uses the following code.

  // Display post information only on certain node types.
  if (variable_get('node_submitted_' . $node->type, TRUE)) {
    $variables['display_submitted'] = TRUE;
    $variables['submitted'] = t('Submitted by !username on !datetime', array('!username' => $variables['name'], '!datetime' => $variables['date']));
    $variables['user_picture'] = theme_get_setting('toggle_node_user_picture') ? theme('user_picture', array('account' => $node)) : '';
  }
  else {
    $variables['display_submitted'] = FALSE;
    $variables['submitted'] = '';
    $variables['user_picture'] = '';
  }

The template files used by Drupal core check $display_submitted before rendering $submitted and $user_picture. Probably Drupal core sets also $variables['submitted'] and $variables['user_picture'] for those template files that render them without checking first $display_submitted.

| improve this answer | |
  • thx that's exactly what i'm looking for :) – Ala Aloui May 28 '16 at 15:27
1

This variable is added in template_preprocess_node, which is the second step in template processing.

These processors pass the variables by reference, so in any of the lower hooks (you can use a theme or a module to implement hook_preprocess_HOOK), you can alter this variable.

So in your module or theme (change MYMODULE with theme name), use this code:

function MYMODULE_preprocess_node(&$variables) {
  $variables['display_submitted'] = FALSE;
}
| improve this answer | |

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.