preprocess_comment пропало имя пользователя в комментарии

Главные вкладки

Аватар пользователя svisch svisch 3 июня 2018 в 18:12

Хочу изменить формат вывода даты в комментариях. Делаю препроцесс:

function template_preprocess_comment(&$variables) {
  $date = $variables['comment']->getCreatedTime();
  $variables['created'] = \Drupal::service('date.formatter')->formatInterval(REQUEST_TIME - $date);
  $variables['submitted'] = t('Submitted by username on datetime', array('username' => $variables['author'], 'datetime' => '<span class="comments-ago">' . $variables['created'] . ' ago </span>'));
}

После этого перестает отображаться Автор комментария. Ни аноним, оставивший сове имя при комментировании, не зареганый пользователь. С чем это может быть связано?

Лучший ответ

Аватар пользователя svisch svisch 3 июня 2018 в 20:29

Вот так все заработало:

function template_preprocess_comment(&$variables) {
  $comment = $variables['elements']['#comment'];
  $commented_entity = $comment
    ->getCommentedEntity();
  $variables['comment'] = $comment;
  $variables['commented_entity'] = $commented_entity;
  $variables['threaded'] = $variables['elements']['#comment_threaded'];
  $account = $comment
    ->getOwner();
  $username = array(
    '#theme' => 'username',
    '#account' => $account,
  );
  $variables['author'] = drupal_render($username);
  $variables['author_id'] = $comment
    ->getOwnerId();
  $variables['new_indicator_timestamp'] = $comment
    ->getChangedTime();
  $variables['created'] = format_date($comment
    ->getCreatedTime());

  $date = $variables['comment']->getCreatedTime();
  $variables['created'] = \Drupal::service('date.formatter')->formatInterval(REQUEST_TIME - $date);
  $variables['submitted'] = t('@username on <span class="comments-ago">@datetime</span> назад', array('@username' => $variables['author'], '@datetime' => $variables['created']));
}

Комментарии

Аватар пользователя svisch svisch 3 июня 2018 в 20:29

Вот так все заработало:

function template_preprocess_comment(&$variables) {
  $comment = $variables['elements']['#comment'];
  $commented_entity = $comment
    ->getCommentedEntity();
  $variables['comment'] = $comment;
  $variables['commented_entity'] = $commented_entity;
  $variables['threaded'] = $variables['elements']['#comment_threaded'];
  $account = $comment
    ->getOwner();
  $username = array(
    '#theme' => 'username',
    '#account' => $account,
  );
  $variables['author'] = drupal_render($username);
  $variables['author_id'] = $comment
    ->getOwnerId();
  $variables['new_indicator_timestamp'] = $comment
    ->getChangedTime();
  $variables['created'] = format_date($comment
    ->getCreatedTime());

  $date = $variables['comment']->getCreatedTime();
  $variables['created'] = \Drupal::service('date.formatter')->formatInterval(REQUEST_TIME - $date);
  $variables['submitted'] = t('@username on <span class="comments-ago">@datetime</span> назад', array('@username' => $variables['author'], '@datetime' => $variables['created']));
}