You are here

EntityUsageSourceLevel.php in Entity Usage 8.3

File

src/EntityUsageSourceLevel.php
View source
<?php

namespace Drupal\entity_usage;

use Drupal\Core\Entity\EntityInterface;

/**
 * Defines the interface for the service managing top-level source entities.
 */
class EntityUsageSourceLevel implements EntityUsageSourceLevelInterface {

  /**
   * Lists entity types we consider top-level.
   */
  const TOP_LEVEL_TYPES = [
    'node',
  ];

  /**
   * {@inheritdoc}
   */
  public static function isTopLevel(EntityInterface $entity) {

    // @todo Make it easier for sites to override this. Currently the way is to
    // override this service and write this method differently. Maybe it's worth
    // letting sites do this in an easier way.
    return in_array($entity
      ->getEntityTypeId(), self::TOP_LEVEL_TYPES);
  }

  /**
   * {@inheritdoc}
   */
  public static function isMiddleLevel(EntityInterface $entity) {

    // For us all entities that are not bottom are middle.
    return !self::isBottomLevel($entity);
  }

  /**
   * {@inheritdoc}
   */
  public static function isBottomLevel(EntityInterface $entity) {

    // If the entity has the usage tab configured, we consider it bottom.
    $types_with_local_task = \Drupal::config('entity_usage.settings')
      ->get('local_task_enabled_entity_types') ?: [];
    return in_array($entity
      ->getEntityTypeId(), $types_with_local_task);
  }

  /**
   * {@inheritdoc}
   */
  public static function getTopLevelEntityTypes() {

    // @todo Make it easier for sites to override this.
    return self::TOP_LEVEL_TYPES;
  }

  /**
   * {@inheritdoc}
   */
  public static function getMiddleLevelEntityTypes() {

    // For us all entities that are not bottom are middle.
    return array_diff(array_keys(\Drupal::entityTypeManager()
      ->getDefinitions()), self::getBottomLevelEntityTypes());
  }

  /**
   * {@inheritdoc}
   */
  public static function getBottomLevelEntityTypes() {
    return \Drupal::config('entity_usage.settings')
      ->get('local_task_enabled_entity_types') ?: [];
  }

}

Classes

Namesort descending Description
EntityUsageSourceLevel Defines the interface for the service managing top-level source entities.