2

One of my tasks requires me to disable the links to username and date fields generated from diff_diffs_show() whose output is rendered as in the following screenshot.

screenshot

The code outputting those links is the following one.

// Generate table header (date, username, log message).
$old_header = t('!date by !username', array(
  '!date' => l(format_date($old_node->revision_timestamp), "node/$node->nid/revisions/$old_node->vid/view", array('absolute' => 1)),
  '!username' => theme('username', array('account' => $node_revisions[$old_vid])),
));
$new_header = t('!date by !username', array(
  '!date' => l(format_date($new_node->revision_timestamp), "node/$node->nid/revisions/$new_node->vid/view", array('absolute' => 1)),
  '!username' => theme('username', array('account' => $node_revisions[$new_vid])),
));

How I can change the output of the function so that it doesn't output links?

Basically, I want the username and date fields in plain texts instead of links.

2
  • Welcome to Drupal Answers! Is there any reason the link to the user accounts should be removed? Only users with the permission to access other user accounts would see the usernames are links. The users without that permission would just see the username as plain text. It's quite helpful to administrator users to have a link to the user account in revision pages. I would rather not change that.
    – apaderno
    Mar 6, 2021 at 10:53
  • The same is true for the link to the revision: Only users who can see revisions would see a link, and for those users is much helpful to see a link. I would not remove those links for users with the permission to administer nodes.
    – apaderno
    Mar 6, 2021 at 10:55

1 Answer 1

0

diff_diffs_show() is the page callback for the node/%node/revisions/view route. The only way to change the output for that route is changing the page callback with hook_menu_alter().

function mymodule_menu_alter(&$items) {
  $items['node/%node/revisions/view']['page callback'] = 'mymodule_diffs_show';
}

function mymodule_diffs_show($node, $old_vid, $new_vid, $state = NULL) {
  // Attaches the CSS.
  $build['#attached'] = diff_build_attachments();

  $default_state = variable_get('diff_default_state_node', 'raw');
  if (empty($state)) {
    $state = $default_state;
  }
  $state = str_replace('-', '_', $state);
  if (!array_key_exists($state, diff_available_states())) {
    $state = $default_state;
  }

  // Same title as the 'Revisions' tab. Blocked by non-page requests.
  if (node_is_page($node)) {
    drupal_set_title(t('Revisions for %title', array('%title' => $node->title)), PASS_THROUGH);
  }
  $node_revisions = node_revision_list($node);

  $old_node = node_load($node->nid, $old_vid);
  $new_node = node_load($node->nid, $new_vid);

  // Generate table header (date, username, log message).
  $old_header = t('!date by !username', array(
    '!date' => format_date($old_node->revision_timestamp),
    '!username' => format_username($node_revisions[$old_vid]),
  ));
  $new_header = t('!date by !username', array(
    '!date' => (format_date($new_node->revision_timestamp),
    '!username' => format_username($node_revisions[$new_vid]),
  ));

  $old_log = $old_node->log != '' ? '<p class="revision-log">' . filter_xss($old_node->log) . '</p>' : '';
  $new_log = $new_node->log != '' ? '<p class="revision-log">' . filter_xss($new_node->log) . '</p>' : '';

  // Generate previous diff/next diff links.
  $nav_suffix = ($default_state != $state) ? '/' . str_replace('_', '-', $state) : '';
  $next_vid = _diff_get_next_vid($node_revisions, $new_vid);
  if ($next_vid) {
    $next_link = l(t('Next difference >'), 'node/' . $node->nid . '/revisions/view/' . $new_vid . '/' . $next_vid . $nav_suffix, array('absolute' => 1));
  }
  else {
    $next_link = '';
  }
  $prev_vid = _diff_get_previous_vid($node_revisions, $old_vid);
  if ($prev_vid) {
    $prev_link = l(t('< Previous difference'), 'node/' . $node->nid . '/revisions/view/' . $prev_vid . '/' . $old_vid . $nav_suffix, array('absolute' => 1));
  }
  else {
    $prev_link = '';
  }

  $header = _diff_default_header($old_header, $new_header);
  $rows = array();
  if ($old_log || $new_log) {
    $rows['logs'] = array(
      array(
        'data' => $old_log,
        'colspan' => 2,
      ),
      array(
        'data' => $new_log,
        'colspan' => 2,
      ),
    );
  }
  $rows['navigation'] = array(
    array(
      'data' => $prev_link,
      'class' => array('diff-prevlink'),
      'colspan' => 2,
    ),
    array(
      'data' => $next_link,
      'class' => array('diff-nextlink'),
      'colspan' => 2,
    ),
  );

  $links = array();
  foreach (diff_available_states('node') as $alternative_state => $label) {
    $links[$alternative_state] = array(
      'title' => $label,
      'href' => "node/{$node->nid}/revisions/view/{$old_vid}/{$new_vid}" . ($alternative_state == $default_state ? '' : '/' . str_replace('_', '-', $alternative_state)),
    );
  }
  if (count($links) > 1) {
    $state_links = theme('links', array(
      'links' => $links,
      'attributes' => array('class' => array('links', 'inline')),
    ));
    $rows['states'] = array(
      array(
        'data' => $state_links,
        'class' => 'diff-links',
        'colspan' => 4,
      ),
    );
  }
  $rows = array_merge($rows, _diff_body_rows($old_node, $new_node, $state));

  $build['diff_table'] = array(
    '#theme' => 'table__diff__standard',
    '#header' => $header,
    '#rows' => $rows,
    '#attributes' => array('class' => array('diff')),
    '#colgroups' => _diff_default_cols(),
    '#sticky' => FALSE,
  );

  // Allow users to hide or set the display mode of the preview.
  if (node_is_page($node) && $view_mode = variable_get('diff_view_mode_preview_node_' . $new_node->type, 'full')) {
    $header = '';
    if ($node->vid == $new_vid) {
      $header .= '<div class="diff-section-title">' . t('Current revision:') . '</div>';
    }
    else {
      $header .= '<div class="diff-section-title">' . t('Revision of @new_date:', array('@new_date' => format_date($new_node->revision_timestamp))) . '</div>';
    }
    $build['diff_preview']['header']['#markup'] = $header;
    // Don't include node links or comments when viewing the diff.
    $build['diff_preview']['content'] = function_exists('entity_view') ? entity_view('node', array($new_node), $view_mode) : node_view($new_node, $view_mode);
    if (isset($build['diff_preview']['content']['links'])) {
      unset($build['diff_preview']['content']['links']);
    }
    if (isset($build['diff_preview']['content']['comments'])) {
      unset($build['diff_preview']['content']['comments']);
    }
  }
  return $build;
}

The part getting the username is simplified: The code should check the username length, and truncate it if it's longer than 20 characters; the username should also be sanitized via check_plain().
See template_preprocess_username() to understand how that can be implemented.

I would rather not implement the code shown in this answer, since:

  • The node/%node/revisions/view route is accessible by the users who have the administer nodes permission, who should be allowed to see any link to revision pages
  • The users without the access user profiles permission wouldn't see links to user accounts in any case, but the users with the administer users permission should be allowed to see link to user accounts in any page, as that simplifies their work
2
  • Thanks for answering my question. Actually I am using this diff module and token [node:diff-markdown] to send out the changes in a node in an email template. I don't want users who are getting these emails to have access to the links in the email they receive. I thought disabling the links here would solve the issue but the email template still contains the links for users and datefield. Is there a better way of achieving this solution? Mar 9, 2021 at 23:31
  • Without more details, I would suggest to alter the sent email with hook_mail_alter().
    – apaderno
    Mar 11, 2021 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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