class WorkspacesTestHooks

Hook implementations for workspaces_test.

Hierarchy

Expanded class hierarchy of WorkspacesTestHooks

File

core/modules/workspaces/tests/modules/workspaces_test/src/Hook/WorkspacesTestHooks.php, line 15

Namespace

Drupal\workspaces_test\Hook
View source
class WorkspacesTestHooks {
  public function __construct(#[Autowire(service: 'keyvalue')] protected readonly KeyValueFactoryInterface $keyValueFactory) {
  }
  
  /**
   * Implements hook_entity_type_alter().
   */
  public function entityTypeAlter(array &$entity_types) : void {
    $state = \Drupal::state();
    // Allow all entity types to have their definition changed dynamically for
    // testing purposes.
    foreach ($entity_types as $entity_type_id => $entity_type) {
      $entity_types[$entity_type_id] = $state->get("{$entity_type_id}.entity_type", $entity_types[$entity_type_id]);
    }
  }
  
  /**
   * Implements hook_ENTITY_TYPE_translation_create() for 'entity_test_mulrevpub'.
   */
  public function entityTranslationCreate() : void {
    /** @var \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager */
    $workspace_manager = \Drupal::service('workspaces.manager');
    $this->keyValueFactory
      ->get('ws_test')
      ->set('workspace_was_active', $workspace_manager->hasActiveWorkspace());
  }
  
  /**
   * Implements hook_entity_create().
   */
  public function entityCreate(EntityInterface $entity) : void {
    $this->incrementHookCount('hook_entity_create', $entity);
  }
  
  /**
   * Implements hook_entity_presave().
   */
  public function entityPresave(EntityInterface $entity) : void {
    $this->incrementHookCount('hook_entity_presave', $entity);
  }
  
  /**
   * Implements hook_entity_insert().
   */
  public function entityInsert(EntityInterface $entity) : void {
    $this->incrementHookCount('hook_entity_insert', $entity);
  }
  
  /**
   * Implements hook_entity_update().
   */
  public function entityUpdate(EntityInterface $entity) : void {
    $this->incrementHookCount('hook_entity_update', $entity);
  }
  
  /**
   * Increments the invocation count for a specific entity hook.
   *
   * @param string $hook_name
   *   The name of the hook being invoked (e.g., 'hook_entity_create').
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity object involved in the hook.
   */
  protected function incrementHookCount(string $hook_name, EntityInterface $entity) : void {
    $key = $entity->getEntityTypeId() . '.' . $hook_name . '.count';
    $count = $this->keyValueFactory
      ->get('ws_test')
      ->get($key, 0);
    $this->keyValueFactory
      ->get('ws_test')
      ->set($key, $count + 1);
  }

}

Members

Title Sort descending Modifiers Object type Summary
WorkspacesTestHooks::entityCreate public function Implements hook_entity_create().
WorkspacesTestHooks::entityInsert public function Implements hook_entity_insert().
WorkspacesTestHooks::entityPresave public function Implements hook_entity_presave().
WorkspacesTestHooks::entityTranslationCreate public function Implements hook_ENTITY_TYPE_translation_create() for 'entity_test_mulrevpub'.
WorkspacesTestHooks::entityTypeAlter public function Implements hook_entity_type_alter().
WorkspacesTestHooks::entityUpdate public function Implements hook_entity_update().
WorkspacesTestHooks::incrementHookCount protected function Increments the invocation count for a specific entity hook.
WorkspacesTestHooks::__construct public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.