Rename QuickStringTrait::assetNamesSummary() to QuickStringTrait::entityLabelsSummary().

This commit is contained in:
Michael Stenta 2023-05-08 16:43:44 -04:00
parent b282007ec9
commit 7f04229e35
6 changed files with 37 additions and 26 deletions

View File

@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- [Add QuickStringTrait::assetNamesSummary() and deprecate farm_log_asset_names_summary() #671](https://github.com/farmOS/farmOS/pull/671)
- [Add QuickStringTrait::entityLabelsSummary() and deprecate farm_log_asset_names_summary() #671](https://github.com/farmOS/farmOS/pull/671)
### Fixed

View File

@ -149,10 +149,10 @@ Available traits and the methods that they provide include:
Expects a keyed array of strings to concatenate together, along with an
optional array of keys that should be prioritized in case the full string
won't fit.
- `assetNamesSummary($assets, $cutoff)` - Generate a summary of asset names
for use in a log name. Example: "Asset 1, Asset 2, Asset 3 (+ 15 more)".
Note that this does NOT sanitize the asset names. It is the responsibility
of downstream code to do so, if it is printing the text to the page.
- `entityLabelsSummary($entities, $cutoff)` - Generate a summary of entity
labels. Example: "Asset 1, Asset 2, Asset 3 (+ 15 more)". Note that this
does NOT sanitize the entity labels. It is the responsibility of downstream
code to do so, if it is printing text to the page.
- `QuickTermTrait`
- `createTerm($values)` - Creates and returns a new term entity from an array
of values.

View File

@ -72,7 +72,7 @@ function farm_log_entity_prepare_form(EntityInterface $entity, $operation, FormS
* Returns a string summarizing the assets.
*
* @deprecated in farmOS:2.x and is removed from farmOS:3.x. Use
* \Drupal\farm_quick\Traits\QuickStringTrait::assetNamesSummary() instead.
* \Drupal\farm_quick\Traits\QuickStringTrait::entityLabelsSummary() instead.
*/
function farm_log_asset_names_summary(array $assets, $cutoff = 3) {
/** @var \Drupal\asset\Entity\AssetInterface[] $assets */

View File

@ -6,7 +6,6 @@ core_version_requirement: ^9
dependencies:
- drupal:taxonomy
- farm:asset
- farm:farm_log
- farm:farm_log_quantity
- farm:quantity
- log:log

View File

@ -132,26 +132,38 @@ trait QuickStringTrait {
}
/**
* Generate a summary of asset names for use in a log name.
* Generate a summary of entity labels.
*
* Note that this does NOT sanitize the asset names. It is the responsibility
* of downstream code to do so, if it is printing the text to the page.
* Note that this does NOT sanitize the entity labels. It is the
* responsibility of downstream code to do so, if it is printing text to the
* page.
*
* @param \Drupal\asset\Entity\AssetInterface[] $assets
* An array of assets.
* @param array $entities
* An array of entities.
* @param 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.
* The number of entity labels to include before summarizing the rest.
* If the number of entities exceeds the cutoff, only the first entity's
* label will be included, and the rest will be summarized as "(+ X more)".
* If the number of entities is less than or equal to the cutoff, or if the
* cutoff is 0, all entity labels will be included.
*
* @return string
* Returns a string summarizing the assets.
* Returns a string summarizing the entity labels.
*/
protected function assetNamesSummary(array $assets, $cutoff = 3) {
// @todo Move logic from farm_log_asset_names_summary() into here.
return farm_log_asset_names_summary($assets, $cutoff);
protected function entityLabelsSummary(array $entities, $cutoff = 3) {
$names = [];
foreach ($entities as $entity) {
$names[] = $entity->label();
}
if ($cutoff != 0) {
array_splice($names, $cutoff);
}
$output = implode(', ', $names);
$diff = count($entities) - count($names);
if ($diff > 0) {
$output .= ' (+ ' . $diff . ' ' . t('more') . ')';
}
return $output;
}
}

View File

@ -88,9 +88,9 @@ class QuickStringTest extends KernelTestBase {
}
/**
* Test assetNamesSummary() method.
* Test entityLabelsSummary() method.
*/
public function testAssetNamesSummary() {
public function testEntityLabelsSummary() {
// Create a test asset type.
$asset_type = AssetType::create([
@ -114,12 +114,12 @@ class QuickStringTest extends KernelTestBase {
// Test default with a cutoff of 3.
$expected = $assets[0]->label() . ', ' . $assets[1]->label() . ', ' . $assets[2]->label() . ' (+ 7 more)';
$name_summary = $this->assetNamesSummary($assets);
$name_summary = $this->entityLabelsSummary($assets);
$this->assertEquals($expected, $name_summary);
// Test with a cutoff of 1.
$expected = $assets[0]->label() . ' (+ 9 more)';
$name_summary = $this->assetNamesSummary($assets, 1);
$name_summary = $this->entityLabelsSummary($assets, 1);
$this->assertEquals($expected, $name_summary);
// Test with a cutoff of 0.
@ -128,7 +128,7 @@ class QuickStringTest extends KernelTestBase {
$labels[] = $asset->label();
}
$expected = implode(', ', $labels);
$name_summary = $this->assetNamesSummary($assets, 0);
$name_summary = $this->entityLabelsSummary($assets, 0);
$this->assertEquals($expected, $name_summary);
}