function farm_log_asset_names_summary in farmOS 2.x
Generate a summary of asset names for use in a log name.
Note that this function does NOT sanitize the asset names. This is the responsibility of downstream code, if it is printing the text to the page.
Parameters
\Drupal\asset\Entity\AssetInterface[] $assets: An array of assets.
int $cutoff: The number of asset names to include before summarizing the rest. If the number of assets exceeds the cutoff, only the first asset's name will be included, and the rest will be summarized as "(+ X more)". If the number of assets is less than or equal to the cutoff, or if the cutoff is 0, all asset names will be included.
Return value
string Returns a string summarizing the assets.
2 calls to farm_log_asset_names_summary()
- AssetGroupActionForm::submitForm in modules/
asset/ group/ src/ Form/ AssetGroupActionForm.php - Form submission handler.
- AssetMoveActionForm::submitForm in modules/
core/ location/ src/ Form/ AssetMoveActionForm.php - Form submission handler.
File
- modules/
core/ log/ farm_log.module, line 74 - Contains farm_log.module.
Code
function farm_log_asset_names_summary(array $assets, $cutoff = 3) {
/** @var \Drupal\asset\Entity\AssetInterface[] $assets */
$names = [];
foreach ($assets as $asset) {
$names[] = $asset
->label();
}
$count = count($names);
if ($cutoff == 0 || count($names) <= $cutoff) {
$output = implode(', ', $names);
}
else {
$output = $names[0] . ' (+' . ($count - 1) . ' ' . t('more') . ')';
}
return $output;
}