Using Drupal 8, I am trying to create a view plugin to add a relationship to an existing view. I'm basing the code off of this example https://www.foreach.be/blog/how-build-complex-filters-drupal-8 since I haven't found an explanation of how to add a relationship in D8.
I don't have any error messages but I'm not sure what I need to do to get this working.
I have a class file for the plugin at module_name/src/Plugin/views/relationship/CustomRelationship.php
// CustomRelationship.php
<?php
namespace Drupal\custom_module\Plugin\views\relationship;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\relationship\RelationshipPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Add custom view relationship
*
* @ingroup views_relationship_handlers
*
* @ViewsRelationship("custom_relationship")
*/
class CustomRelationship extends RelationshipPluginBase {
/**
* Relationship plugin.
*
*
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->valueTitle = t('Node Revision');
}
public function query() {
$configuration = array(
'table' => 'node_revision',
'field' => 'nid',
'left_table' => 'node_field_revision',
'left_field' => 'nid',
);
$join = Views::pluginManager('join')->createInstance('standard', $configuration);
$this->query->addRelationship('node_revision', $join, 'node_field_revision');
}
}
And in the main module folder there is a views.inc file
// custom_module.views.inc
<?php
/**
* Implements hook_views_data_alter().
*/
function custom_module_views_data_alter(&$data) {
$data['custom_module']['custom_relationship'] = array(
'title' => t('Node Revision'),
'help' => t('Provide node revision relationship.'),
'relationship' => array(
// ID of relationship handler plugin to use.
'id' => 'custom_relationship',
),
);
}