0

I want to create a node for each comparison of 2 nodes, so you end up with site.com/node-1-vs-node-2 and then on that page it shows both the node 1 and node 2 fields side by side in a table to compare them.

What am i looking at to get this done as quickly as possible? I have 50 nodes in total and want to do node 1 vs node 2, node 1 vs node 3, node 1 vs node 4, etc all the way through.

Would it be views, or entity reference which shows a table format for each output?

I'm using Drupal 8

2

You could achieve this quite easily with a custom module. In this example I'll use Drupal console to simplify settings every thing up.

  1. Create a module:

    drupal generate:module

Call it whatever you want.

  1. Create a controller:

    drupal generate:controller

Sample answers for this may be:

  • Controller class name: ComparisonController
  • Method title: CompareNodes
  • Action method name: compare_nodes
  • Route path: test/compare_nodes/{id1}/{id2}

Now edit your newly created controller. At the top add a use for the node module:

use Drupal\node\Entity\Node;
  1. Edit the compare_nodes method.

Here is some sample code:

public function compare_nodes($id1, $id2) {

  $node1 = Node::load($id1);
  $node2 = Node::load($id2);

  $fields_to_compare = ['body'];

  $build = [];

  foreach($fields_to_compare as $field) {
    $build['comparisons'][$field] = [
      '#type' => 'container',
    ];
    $build['comparisons'][$field]['value1'] = [
      '#markup' => 'Node 1 value: ' . $node1->get($field)->value,
      '#prefix' => '<div>',
      '#suffix' => '</div>',
    ];
    $build['comparisons'][$field]['value2'] = [
      '#markup' => 'Node 2 value: ' . $node2->get($field)->value,
      '#prefix' => '<div>',
      '#suffix' => '</div>',
    ];

  }

  return $build;

}

You can edit the list of fields to compare as required and also make any changes to the markup required.

Enable your module and now when you visit: /test/compare_nodes/1/2 you can compare the values of node 1 with node 2.

You can replace 1 and 2 with whatever ids you would like to compare.

| improve this answer | |
  • This is amazing, thank you for the time spent on it. I actually went with the bottom idea because im not a back-end dev. I created a node type called comparison and then had 2 node references for the first content type. Then the reference on the manage display showed the full nodes content and I styled them side by side so the same fields would be sat next to each other. I would though love to give your method a try as im now up to nearly 100 nodes and although i've created the node 1 vs node 2/3/4 etc, i now have to do node 2 vs node 3/4/5, etc and so forth and only your method would work. – effortDee Nov 26 '17 at 19:39
1

Assumptions

  • Node 1 is always the first target of comparison.
  • Nodes 2 to 50 are the "other" node.

If you actually want to create nodes to be the "vs" nodes, you need to use Entity Reference-- views alone can't create nodes.

Make a "vs" content type. Add two entity reference fields.

  • Field 1- "Node 1" (set the default value of the entity reference to node 1)
  • Field 2- "Other node"

Then create the 50 nodes by quickly referencing each node in order. This is a little painful but shouldn't take too long for just 50 nodes.

Now you need to create the display for these "vs" nodes that shows the comparison table. This you can use Views for-- create a View (of content: vs content type) page that overrides the path of the "vs" node. Use a contextual filter to grab the vs node ID, and then attach relationships to each entity reference.

Then you can build the comparison table in Views with the node fields.

This is not an exact step-by-step list, but should give you a general idea of what you need to do.

| improve this answer | |
  • thank you patrick, this is exactly what i ended up doing but because i didnt think that i would be comparing all nodes with each other , the above suggestion would be better, it's just above me. – effortDee Nov 26 '17 at 19:40
  • @effortDee Don't say "above" or "below" on this site because as people vote the position of the answers can change over time. It's better to say "the answer by Felix Eve". – Patrick Kenny Nov 27 '17 at 1:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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