You are here

class EntityOperations in Radioactivity 8.3

Same name and namespace in other branches
  1. 4.0.x src/EntityOperations.php \Drupal\radioactivity\EntityOperations

Defines a class for reacting to entity events.

@package Drupal\radioactivity

Hierarchy

Expanded class hierarchy of EntityOperations

1 file declares its use of EntityOperations
radioactivity.module in ./radioactivity.module
Provides a field type which can be used as a hotness metric.

File

src/EntityOperations.php, line 18

Namespace

Drupal\radioactivity
View source
class EntityOperations implements ContainerInjectionInterface {

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  private $moduleHandler;

  /**
   * The entity type bundle info service.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  private $bundleInfo;

  /**
   * EntityOperations constructor.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The module handler service.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundleInfo
   *   The entity type bundle info service.
   */
  public function __construct(ModuleHandlerInterface $moduleHandler, EntityTypeBundleInfoInterface $bundleInfo) {
    $this->moduleHandler = $moduleHandler;
    $this->bundleInfo = $bundleInfo;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('module_handler'), $container
      ->get('entity_type.bundle.info'));
  }

  /**
   * Determines if the entity is moderated by the Content Moderation module.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity to check.
   *
   * @return bool
   *   TRUE if the entity leverages content moderation, FALSE otherwise.
   */
  private function entityLeveragesContentModeration(EntityInterface $entity) {
    if (!$entity instanceof ContentEntityInterface) {
      return FALSE;
    }
    if ($entity
      ->getEntityType()
      ->hasHandlerClass('moderation')) {
      $bundles = $this->bundleInfo
        ->getBundleInfo($entity
        ->getEntityType()
        ->id());
      return isset($bundles[$entity
        ->bundle()]['workflow']);
    }
    return FALSE;
  }

  /**
   * Acts on an entity and set published status based on the moderation state.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity being saved.
   *
   * @see hook_entity_presave()
   */
  public function entityPresave(EntityInterface $entity) {

    // Check if the entity is being updated by the radioactivity processor.
    if (!isset($entity->radioactivityUpdate)) {
      return;
    }
    if (!$this->moduleHandler
      ->moduleExists('content_moderation') || !$this
      ->entityLeveragesContentModeration($entity)) {

      // Entity is not using content moderation, so we don't have to implement
      // our work around.
      return;
    }

    // Prevent the Content Moderation module from creating a new revision.
    if ($entity instanceof RevisionableInterface) {
      $entity
        ->setNewRevision(FALSE);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityOperations::$bundleInfo private property The entity type bundle info service.
EntityOperations::$moduleHandler private property The module handler service.
EntityOperations::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
EntityOperations::entityLeveragesContentModeration private function Determines if the entity is moderated by the Content Moderation module.
EntityOperations::entityPresave public function Acts on an entity and set published status based on the moderation state.
EntityOperations::__construct public function EntityOperations constructor.