Is it possible to use this module to create a related content block via a contextual filter?

I'm using Entity Embed with a content type ("Parent") that can embed several other content types ("Child1", "Child2", "Child3"). When visiting a node of type Child1/2/3, I'd like to display either (1) a link back to the Parent node that has the current node embedded within it, or (2) links to the Child1/2/3 nodes that are also on the same Parent node.

Hope this makes sense! Any guidance is appreciated.

Comments

kittysunshine created an issue. See original summary.

marcoscano’s picture

Have you tried using the ::listUsage() method of the service this module provides?

To achieve the first scenario I believe something similar to the following code should do the trick:

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function MY_MODULE_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  /** @var \Drupal\entity_usage\EntityUsage $usage_service */
  $usage_service = \Drupal::service('entity_usage.usage');
  $usage = $usage_service->listUsage($entity);
  if (!empty($usage['node'])) {
    foreach ($usage['node'] as $nid => $qty) {
      $node = Node::load($nid);
      if ($node) {
        $build['extra_link_' . $nid] = $node->toLink(t('The parent node of the node you are viewing is: !title', ['!title' => $node->getTitle()]))->toRenderable();
      }
    }
  }
}

For the second scenario, this module does not provide an API function to retrieve that info directly, but the structure of the information in the database is not complex, you should be able to perform a custom query on {entity_usage} and fetch all "siblings" of the node you are viewing.

marcoscano’s picture

Status: Active » Fixed

Closing for now, feel free to re-open if you need further information.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

graham73may’s picture

I needed similar functionality to what is being discussed here.

Some of my sources were Paragraphs so I needed to use listSources() which returned the source_type.

<?php

/**
 * Add Usage output to the template to facilitate "Appears in".
 *
 * Implements hook_ENTITY_TYPE_view().
 */
function iisd_frontend_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  if ($build['#view_mode'] === 'full') {
    /** @var \Drupal\entity_usage\EntityUsage $usage_service */
    $usage_service = \Drupal::service('entity_usage.usage');

    if ($usage_service instanceof EntityUsage) {
      $sources = $usage_service->listSources($entity, FALSE);

      if (!empty($sources)) {
        $nids = [];
        $render_arrays = [];

        foreach ($sources as $source) {
          if ($source['source_type'] === 'node') {
            $node = Node::load($source['source_id']);

            if ($node instanceof Node) {
              $nid = $node->id();

              if (!in_array($nid, $nids)) {
                $nids[] = $nid;

                $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');

                $render_arrays[] = $view_builder->view($node, 'teaser');
              }
            }
          }

          if ($source['source_type'] === 'paragraph') {
            $paragraph_entity = Paragraph::load($source['source_id']);
            $parent_node = $paragraph_entity->getParentEntity();

            // If you have nested paragraphs you will need to loop
            // the parent.
            // while ($source->getParentEntity() && $source->getParentEntity() != NodeInterface) {
            //
            // }

            if ($parent_node instanceof Node) {
              $nid = $parent_node->id();

              if (!in_array($nid, $nids)) {
                $nids[] = $nid;

                $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');

                $render_arrays[] = $view_builder->view($parent_node, 'teaser');
              }
            }
          }
        }
      }
    }

    if (!empty($render_arrays)) {
      $build['appears_in'] = $render_arrays;
    }
  }
}
?>