You are here

class HistoryUserTimestamp in Drupal 10

Same name in this branch
  1. 10 core/modules/history/src/Plugin/views/filter/HistoryUserTimestamp.php \Drupal\history\Plugin\views\filter\HistoryUserTimestamp
  2. 10 core/modules/history/src/Plugin/views/field/HistoryUserTimestamp.php \Drupal\history\Plugin\views\field\HistoryUserTimestamp
Same name and namespace in other branches
  1. 8 core/modules/history/src/Plugin/views/field/HistoryUserTimestamp.php \Drupal\history\Plugin\views\field\HistoryUserTimestamp
  2. 9 core/modules/history/src/Plugin/views/field/HistoryUserTimestamp.php \Drupal\history\Plugin\views\field\HistoryUserTimestamp

Field handler to display the marker for new content.

The handler is named history_user, because of compatibility reasons, the table is history.

Plugin annotation

@ViewsField("history_user_timestamp");

Hierarchy

  • class \Drupal\node\Plugin\views\field\Node extends \Drupal\views\Plugin\views\field\FieldPluginBase

Expanded class hierarchy of HistoryUserTimestamp

File

core/modules/history/src/Plugin/views/field/HistoryUserTimestamp.php, line 21

Namespace

Drupal\history\Plugin\views\field
View source
class HistoryUserTimestamp extends Node {

  /**
   * {@inheritdoc}
   */
  public function usesGroupBy() {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);
    if (\Drupal::currentUser()
      ->isAuthenticated()) {
      $this->additional_fields['created'] = [
        'table' => 'node_field_data',
        'field' => 'created',
      ];
      $this->additional_fields['changed'] = [
        'table' => 'node_field_data',
        'field' => 'changed',
      ];
      if (\Drupal::moduleHandler()
        ->moduleExists('comment') && !empty($this->options['comments'])) {
        $this->additional_fields['last_comment'] = [
          'table' => 'comment_entity_statistics',
          'field' => 'last_comment_timestamp',
        ];
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['comments'] = [
      'default' => FALSE,
    ];
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    if (\Drupal::moduleHandler()
      ->moduleExists('comment')) {
      $form['comments'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Check for new comments as well'),
        '#default_value' => !empty($this->options['comments']),
      ];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function query() {

    // Only add ourselves to the query if logged in.
    if (\Drupal::currentUser()
      ->isAnonymous()) {
      return;
    }
    parent::query();
  }

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {

    // Let's default to 'read' state.
    // This code shadows node_mark, but it reads from the db directly and
    // we already have that info.
    $mark = MARK_READ;
    if (\Drupal::currentUser()
      ->isAuthenticated()) {
      $last_read = $this
        ->getValue($values);
      $changed = $this
        ->getValue($values, 'changed');
      $last_comment = \Drupal::moduleHandler()
        ->moduleExists('comment') && !empty($this->options['comments']) ? $this
        ->getValue($values, 'last_comment') : 0;
      if (!$last_read && $changed > HISTORY_READ_LIMIT) {
        $mark = MARK_NEW;
      }
      elseif ($changed > $last_read && $changed > HISTORY_READ_LIMIT) {
        $mark = MARK_UPDATED;
      }
      elseif ($last_comment > $last_read && $last_comment > HISTORY_READ_LIMIT) {
        $mark = MARK_UPDATED;
      }
      $build = [
        '#theme' => 'mark',
        '#status' => $mark,
      ];
      return $this
        ->renderLink(\Drupal::service('renderer')
        ->render($build), $values);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HistoryUserTimestamp::buildOptionsForm public function Provide link to node option. Overrides Node::buildOptionsForm
HistoryUserTimestamp::defineOptions protected function Overrides Node::defineOptions
HistoryUserTimestamp::init public function Overrides Node::init
HistoryUserTimestamp::query public function
HistoryUserTimestamp::render public function Overrides Node::render
HistoryUserTimestamp::usesGroupBy public function
Node::renderLink protected function Prepares link to the node.