1

I have a view of Node revisions. I want to only show revisions that are edits. I want to exclude the first revision of each node.

I think filtering for created <> changed would work, but I could find a filter in views, or a contrib module that does this.

Is there a way to do this without writing a custom view filter plugin?

2
  • Couldn't it also be true that the highest revision id is not necessarily the published or current one? Is it possible to query for node revisions whos state is not published?
    – Kevin
    Feb 9, 2023 at 16:45
  • I am only looking to exclude the first revision. The publish state, and if it is the current revision or not does not matter to me. Feb 9, 2023 at 17:24

1 Answer 1

2

Is there a way to do this without writing a custom view filter plugin?

I'm afraid not.

I am only looking to exclude the first revision.

mymodule/mymodule.module

<?php

/**
 * Implements hook_views_data().
 */
function mymodule_views_data() {
  $data = [];

  $data['node_revision']['exclude_first_node_revision_filter'] = [
    'title' => t('Exclude first node revision'),
    'help' => t('Excludes the first node revision.'),
    'filter' => ['id' => 'exclude_first_node_revision_filter'],
  ];

  return $data;
}

mymodule/src/Plugin/views/filter/ExcludeFirstNodeRevisionFilter.php

<?php

namespace Drupal\mymodule\Plugin\views\filter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
use Drupal\views\ViewExecutable;

/**
 * @ViewsFilter("exclude_first_node_revision_filter")
 */
class ExcludeFirstNodeRevisionFilter extends FilterPluginBase {

  /**
   * {@inheritdoc}
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);

    $this->valueTitle = t('Exclude first node revision');
    $this->definition['options callback'] = [$this, 'generateOptions'];
  }

  /**
   * {@inheritdoc}
   */
  public function query() {
    $this->ensureMyTable();

    // Exclude the first revision of each node.
    $this->query->addWhereExpression($this->options['group'], "node_revision.vid <> (SELECT MIN(vid) FROM {node_field_revision} WHERE nid = node_revision.nid)");
  }

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

  /**
   * {@inheritdoc}
   */
  protected function operatorForm(&$form, FormStateInterface $form_state) {
  }

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

}

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.