Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: translate categories labels in component #94

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions src/Controller/Component/CategoriesComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use BEdita\Core\Model\Entity\Category;
use BEdita\Core\Model\Table\CategoriesTable;
use BEdita\I18n\Core\I18nTrait;
use Cake\Collection\CollectionInterface;
use Cake\Controller\Component;
use Cake\Database\Expression\QueryExpression;
Expand All @@ -19,6 +20,7 @@
*/
class CategoriesComponent extends Component
{
use I18nTrait;
use LocatorAwareTrait;

/**
Expand Down Expand Up @@ -48,9 +50,10 @@ public function initialize(array $config): void
*
* @param string|null $type Object type name.
* @param int|null $parentId ID of parent category.
* @param string|null $lang Language code to use.
* @return \Cake\ORM\Query
*/
public function load(string|null $type = null, int|null $parentId = null): Query
public function load(string|null $type = null, int|null $parentId = null, string|null $lang = null): Query
{
$query = $this->Categories->find()
->where([$this->Categories->aliasField('enabled') => true]);
Expand All @@ -64,11 +67,14 @@ public function load(string|null $type = null, int|null $parentId = null): Query
$query = $query->find('type', [$type]);
}

$lang ??= $this->getLang();
$query
->order([$this->Categories->aliasField('name')])
->formatResults(function (CollectionInterface $results): CollectionInterface {
return $results->map(function (Category $category): Category {
->formatResults(function (CollectionInterface $results) use ($lang): CollectionInterface {
return $results->map(function (Category $category) use ($lang): Category {
$category->set('slug', Text::slug($category->name));
$category = $this->dangerouslyTranslateLabel($category, $lang);

$category->clean();

return $category;
Expand All @@ -81,29 +87,28 @@ public function load(string|null $type = null, int|null $parentId = null): Query
/**
* Load a category by its name and type
*
* @param string|null $name Category name.
* @param string|null $type Category type.
* @param string $name Category name.
* @param string $type Category type.
* @param string|null $lang Language code to use.
* @return \Cake\ORM\Query
*/
public function loadByName(string $name, string $type): Query
public function loadByName(string $name, string $type, string|null $lang = null): Query
{
$query = $this->Categories->find()
->where([$this->Categories->aliasField('enabled') => true])
->where([$this->Categories->aliasField('name') => $name]);
$lang ??= $this->getLang();

$query = $query->find('type', [$type]);

$query
->formatResults(function (CollectionInterface $results): CollectionInterface {
return $results->map(function (Category $category): Category {
return $this->Categories->find()
->where([$this->Categories->aliasField('enabled') => true])
->where([$this->Categories->aliasField('name') => $name])
->find('type', [$type])
->formatResults(function (CollectionInterface $results) use ($lang): CollectionInterface {
return $results->map(function (Category $category) use ($lang): Category {
$category->set('slug', Text::slug($category->name));
$category = $this->dangerouslyTranslateLabel($category, $lang);
$category->clean();

return $category;
});
});

return $query;
}

/**
Expand Down Expand Up @@ -132,6 +137,26 @@ protected function buildCategoriesSubquery(Table $table, array $categories): Que
}));
}

/**
* Dangerous processor to set label to its translation.
*
* **WARNING**: do NOT save entities that have been processed by this processor.
*
* @param \BEdita\Core\Model\Entity\Category $category Category entity to process.
* @param string|null $lang Language code to use.
* @return \BEdita\Core\Model\Entity\Category
*/
protected function dangerouslyTranslateLabel(Category $category, string|null $lang = null): Category
{
if ($lang === null || !array_key_exists($lang, $category->get('labels'))) {
return $category;
}

$category->label = $category->get('labels')[$lang];

return $category;
}

/**
* Filter contents that are in one or more of the given categories.
*
Expand Down
7 changes: 4 additions & 3 deletions src/Model/ObjectsLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,10 @@ protected function dangerouslyTranslateFields(ObjectEntity $object, string|null
}

if (!empty($object->get('categories'))) {
foreach ($object->categories as &$category) {
if (array_key_exists($lang, $category->labels)) {
$category->label = $category->labels[$lang];
foreach ($object->get('categories') as &$category) {
/** @type \BEdita\Core\Model\Entity\Category $category */
if (array_key_exists($lang, $category->get('labels'))) {
$category->label = $category->get('labels')[$lang];
}
}
unset($category);
Expand Down
Loading