0

A field in my Content Type A references a Content Type B.

I'd like to find, in each instance of Content Type B, a list of all the As that reference it at a particular point in time.

[Update]: I made the question less ambiguous in the light of the answer I found with help from others here.

2

3 Answers 3

1

If you want a quick solution in code, you can use the same way as in drupal 7:

Add a field (configurable in view modes):

hook_entity_extra_field_info

Put the code for the field in here:

hook_ENTITY_TYPE_view

Use this code:

How to check if an article is referenced from a collection?

2
  • V useful. Will experiment with this.
    – iainH
    Dec 22, 2015 at 16:15
  • I used the code you referred to (item 3) in my answer.
    – iainH
    Jan 1, 2016 at 23:26
1

In D7, Entity Views Attachment was the most popular solution for this. It has no D8 port yet however.

D8 modules which offer this functionality include:

  • Display Suite. It enables you to add a view as a dynamic field; that view could use a contextual filter to focus its content on the current node URL.

  • Viewfield, as you suggested. You use a token to specify a default view, and choose to "force the default" to prevent content editors from changing the selected view.

  • Display fields, an interesting new D8 module. It allows you to place views as fields on a node, and is meant to allow contextual arguments, but I've not been able to set this up properly (see the issue)

2
  • V useful. Will experiment with Display fields too. The Embed a view variant sounds ideal.
    – iainH
    Dec 22, 2015 at 16:15
  • I've just tried Display fields views embedding and been unsuccessful, I've edited my answer to mention. Please accept if you're happy with the answer.
    – Jonathan
    Dec 22, 2015 at 17:27
0

This code is in my agenda.module. The where-used relation is used as follows in the form validation handler that is called when the user clicks the Delete button on the delete confirmation form:

The Agenda Node cannot be deleted if there are any Meeting Nodes that refer to it.

use Drupal\Core\Form\FormStateInterface;

function agenda_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  switch ($form_id) {
    // ...
    case "node_agenda_delete_form":
      $form['#validate'][] = '_agenda_delete_validation';
      break;
    // ...
  }
}

function _agenda_delete_validation(&$form, FormStateInterface $form_state) {
  $agenda_node = $form_state->getFormObject()->getEntity();
  $agenda_name = $agenda_node->getTitle();
  $agenda_node_id = $agenda_node->id();
  $query = \Drupal::entityQuery('node')
    ->condition('type', 'meeting')
    ->condition('field_agenda', $agenda_node_id);
  $nids = $query->execute();
  $count_nids = count($nids);
  if ($count_nids > 0) {
    $msg_stem = ($count_nids == 1) ? 'There is one Meeting that references "' : 'There are '.$count_nids.' Meetings that reference "';
    $form_state->setError($form, $msg_stem.$agenda_name.'". It cannot be deleted yet.' );
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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