-
-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an asset.logs service for retrieving logs that reference an asset #…
- Loading branch information
Showing
6 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Drupal\farm_log; | ||
|
||
use Drupal\asset\Entity\AssetInterface; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
|
||
/** | ||
* Service for loading logs that reference assets. | ||
*/ | ||
class AssetLogs implements AssetLogsInterface { | ||
|
||
/** | ||
* Entity type manager. | ||
* | ||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface | ||
*/ | ||
protected EntityTypeManagerInterface $entityTypeManager; | ||
|
||
/** | ||
* Log query factory. | ||
* | ||
* @var \Drupal\farm_log\LogQueryFactoryInterface | ||
*/ | ||
protected LogQueryFactoryInterface $logQueryFactory; | ||
|
||
/** | ||
* Class constructor. | ||
* | ||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager | ||
* Entity type manager. | ||
* @param \Drupal\farm_log\LogQueryFactoryInterface $log_query_factory | ||
* Log query factory. | ||
*/ | ||
public function __construct(EntityTypeManagerInterface $entity_type_manager, LogQueryFactoryInterface $log_query_factory) { | ||
$this->entityTypeManager = $entity_type_manager; | ||
$this->logQueryFactory = $log_query_factory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getLogs(AssetInterface $asset, string $log_type = NULL, bool $access_check = TRUE): array { | ||
$log_ids = $this->query($asset, $log_type, $access_check)->execute(); | ||
if (empty($log_ids)) { | ||
return []; | ||
} | ||
return $this->entityTypeManager->getStorage('log')->loadMultiple($log_ids); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFirstLog(AssetInterface $asset, string $log_type = NULL, bool $access_check = TRUE) { | ||
$log_ids = $this->query($asset, $log_type, $access_check, 1)->execute(); | ||
if (empty($log_ids)) { | ||
return NULL; | ||
} | ||
return $this->entityTypeManager->getStorage('log')->load(reset($log_ids)); | ||
} | ||
|
||
/** | ||
* Build a log query. | ||
* | ||
* @param \Drupal\asset\Entity\AssetInterface $asset | ||
* The asset entity. | ||
* @param string|null $log_type | ||
* Optionally filter by log type. | ||
* @param bool $access_check | ||
* Whether to check log entity access. | ||
* @param int|null $limit | ||
* The number of logs to return. | ||
* | ||
* @return \Drupal\Core\Entity\Query\QueryInterface | ||
* A query object. | ||
*/ | ||
protected function query(AssetInterface $asset, string $log_type = NULL, bool $access_check = TRUE, int $limit = NULL) { | ||
$options = [ | ||
'asset' => $asset, | ||
'direction' => 'ASC', | ||
]; | ||
if (!empty($limit)) { | ||
$options['limit'] = $limit; | ||
} | ||
$query = $this->logQueryFactory->getQuery($options); | ||
if (!empty($log_type)) { | ||
$query->condition('type', $log_type); | ||
} | ||
$query->accessCheck($access_check); | ||
return $query; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Drupal\farm_log; | ||
|
||
use Drupal\asset\Entity\AssetInterface; | ||
|
||
/** | ||
* The interface for asset logs service. | ||
*/ | ||
interface AssetLogsInterface { | ||
|
||
/** | ||
* Get all logs for an asset. | ||
* | ||
* @param \Drupal\asset\Entity\AssetInterface $asset | ||
* The asset entity. | ||
* @param string|null $log_type | ||
* Optionally filter by log type. | ||
* @param bool $access_check | ||
* Whether to check log entity access (defaults to TRUE). | ||
* | ||
* @return \Drupal\log\Entity\LogInterface[] | ||
* Returns an array of Log entities. | ||
*/ | ||
public function getLogs(AssetInterface $asset, string $log_type = NULL, bool $access_check = TRUE): array; | ||
|
||
/** | ||
* Get the first log of an asset. | ||
* | ||
* @param \Drupal\asset\Entity\AssetInterface $asset | ||
* The asset entity. | ||
* @param string|null $log_type | ||
* Optionally filter by log type. | ||
* @param bool $access_check | ||
* Whether to check log entity access. | ||
* | ||
* @return \Drupal\log\Entity\LogInterface|null | ||
* Returns a log entity or NULL if no logs were found. | ||
*/ | ||
public function getFirstLog(AssetInterface $asset, string $log_type = NULL, bool $access_check = TRUE); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters