@bbinkovitz
In https://www.drupal.org/node/2301499 you say you like the recursion check.
I improved the check by using less resources:
The code in above node starts with:

      if (empty($recursive)) {
        $wrapper = entity_metadata_wrapper($entity_type, $entity);
        $recursive[] = array(
          'type' => $entity_type,
          'id' => $wrapper->getIdentifier(),
        );
        unset ($wrapper);
      }

It now reads:

      if (empty($recursive)) {
        $ids = entity_extract_ids($entity_type, $entity);
        $recursive[] = array(
          'type' => $entity_type,
          'id' => $ids[0],
        );
      }

Off course I don't know your implementation, so it's up to you how to implement the recursive check.

Comments

bbinkovitz’s picture

Thanks! Actually I have committed a modified version of this concept, with credit to your username, to the 8.x-1.x branch of this module already. 8.x has a couple of issues, such as entityreference doesn't give a target_type, so I am still trying to figure out a workaround for that (currently hardcoded to "node" for testing). Also there is no entity_metadata_wrapper() in 8. But the relevant code is:

    // Initialize circular rendering check.
		static $recursive = array();
		// Add current entity.
		if (empty($recursive)) {
      $recursive[] = "$entity_type-$entity_id";
		}

Then inside the foreach:

      // Check circular rendering.
      if (!in_array("$target_type-$target_id", $recursive)) {
          // Render with our view mode.
          $elements[$delta] = entity_view($node, $this->getSetting('view_mode'));
          // Add this entity to the list of possible recursions.
          $recursive[] = "$entity_type-$entity_id";
      }
      else {
        // If there's recursion, then just render the name.
        $elements[$delta] = array('#markup' => $node->label());
        watchdog('Views Hybrid', t('There is a recursive entity reference relationship between entities: @entities'), array('@entities' => implode(',', $recursive) . ",$entity_type-$entity_id"), WATCHDOG_DEBUG);
      }