You are here

protected function EntityReferenceFieldFormatter::isApplicableFieldFormatter in Entity Embed 8

Checks if the field formatter is applicable.

Return value

\Drupal\Core\Access\AccessResult Returns the access result.

Overrides FieldFormatterEntityEmbedDisplayBase::isApplicableFieldFormatter

File

src/Plugin/entity_embed/EntityEmbedDisplay/EntityReferenceFieldFormatter.php, line 111

Class

EntityReferenceFieldFormatter
Entity Embed Display reusing entity reference field formatters.

Namespace

Drupal\entity_embed\Plugin\entity_embed\EntityEmbedDisplay

Code

protected function isApplicableFieldFormatter() {
  $access = parent::isApplicableFieldFormatter();

  // Don't bother checking if not allowed.
  if ($access
    ->isAllowed()) {
    if ($this
      ->getPluginId() === 'entity_reference:entity_reference_entity_view') {

      // This option disables entity_reference_entity_view plugin for content
      // entity types. If it is truthy then the plugin is enabled for all
      // entity types.
      $mode = $this->configFactory
        ->get('entity_embed.settings')
        ->get('rendered_entity_mode');
      if ($mode) {

        // Return *allowed* object.
        return $access;
      }

      // Only allow this if this is not a content entity type.
      $entity_type_id = $this
        ->getEntityTypeFromContext();
      if ($entity_type_id) {
        $definition = $this->entityTypeManager
          ->getDefinition($entity_type_id);
        return $access
          ->andIf(AccessResult::allowedIf(!$definition
          ->entityClassImplements(ContentEntityInterface::class)));
      }
    }
  }
  return $access;
}