Skip to content

Commit

Permalink
Merge pull request #553 from AcclaroInc/release/3.4.4
Browse files Browse the repository at this point in the history
Release/3.4.4
  • Loading branch information
sidedwards authored Jan 18, 2025
2 parents e991284 + 3b0a22e commit 728d90f
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .craftplugin
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"pluginName": "Translations for Craft",
"pluginDescription": "Drive global growth with simplified translation workflows.",
"pluginVersion": "3.4.3",
"pluginVersion": "3.4.4",
"pluginAuthorName": "Acclaro",
"pluginVendorName": "Acclaro",
"pluginAuthorUrl": "http://www.acclaro.com/",
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 3.4.4- 2025-01-17

### Added
- Support for TableMaker field. ([AcclaroInc#547](https://github.com/AcclaroInc/craft-translations/issues/547))


## 3.4.3- 2025-01-13

### Fixed

- An issue with Neo block propagation for non-localized blocks. ([AcclaroInc#562](https://github.com/AcclaroInc/pm-craft-translations/issues/562))

## 3.4.2 - 2024-12-16
Expand Down
1 change: 1 addition & 0 deletions src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ class Constants
'craft\fields\RadioButtons',
'benf\neo\Field',
'verbb\vizy\fields\VizyField',
'verbb\tablemaker\fields\TableMakerField',
'craft\redactor\Field',
'presseddigital\linkit\fields\LinkitField',
'verbb\hyper\fields\HyperField',
Expand Down
4 changes: 3 additions & 1 deletion src/services/fieldtranslator/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use lenz\linkfield\fields\LinkField as TypedLinkField;
use verbb\hyper\fields\HyperField as HyperLinkField;
use verbb\navigation\fields\NavigationField;
use verbb\tablemaker\fields\TableMakerField;

class Factory
{
Expand Down Expand Up @@ -78,7 +79,8 @@ class Factory
Embed::class => NsmFieldsTranslator::class,
VizyField::class => VizyFieldTranslator::class,
NavigationField::class => NavigationFieldTranslator::class,
CkEditorField::class => GenericFieldTranslator::class
CkEditorField::class => GenericFieldTranslator::class,
TableMakerField::class => TableMakerFieldTranslator::class
);

public function makeTranslator(Field $field)
Expand Down
138 changes: 138 additions & 0 deletions src/services/fieldtranslator/TableMakerFieldTranslator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
/**
* Translations for Craft plugin for Craft CMS 5.x
*
* Translations for Craft eliminates error prone and costly copy/paste workflows for launching human translated Craft CMS web content.
*
* @link http://www.acclaro.com/
* @copyright Copyright (c) 2018 Acclaro
*/

namespace acclaro\translations\services\fieldtranslator;

use craft\base\Field;
use craft\base\Element;
use acclaro\translations\Translations;
use acclaro\translations\services\ElementTranslator;

class TableMakerFieldTranslator extends GenericFieldTranslator
{
/**
* {@inheritdoc}
*/
public function toTranslationSource(ElementTranslator $elementTranslator, Element $element, Field $field)
{
$source = array();

$data = $element->getFieldValue($field->handle);

$columnsData = $data['columns'];
$columns = $this->getTableColumns($columnsData);

foreach ($data['rows'] as $i => $row) {
foreach ($columns as $id => $name) {
$columnKey = sprintf('%s.%s.%s', $field->handle, 'columnTitle', $id);
$source[$columnKey] = $name;

$key = sprintf('%s.%s.%s', $field->handle, $i, $name);

// Check to translate dropdown lable not values
if ($this->isColumnDropdown($columnsData[$id])) {
$source[$key] = $columnsData[$id]['options'][$i]['label'];
} else {
$source[$key] = isset($row[$id]) ? $row[$id] : '';
}
}
}
return $source;
}

/**
* {@inheritdoc}
*/
public function toPostArray(ElementTranslator $elementTranslator, Element $element, Field $field)
{
$fieldHandle = $field->handle;

$fieldData = $element->getFieldValue($fieldHandle);

return $fieldData ? array($fieldHandle => $fieldData) : array();
}

/**
* {@inheritdoc}
*/
public function toPostArrayFromTranslationTarget(ElementTranslator $elementTranslator, Element $element, Field $field, $sourceSite, $targetSite, $fieldData)
{
$fieldHandle = $field->handle;

$post = $this->toPostArray($elementTranslator, $element, $field);
foreach ($fieldData as $i => $row) {
if ($i === 'columnTitle') {
foreach ($row as $index => $columnTitle) {
$post[$fieldHandle]['columns'][$index]['heading'] = $columnTitle;
}
} else if (isset($post[$fieldHandle]['rows'][$i])) {
// Custom logic to replace the dropdown lable in place of value
$this->handleDropdownValues($post, $row, $fieldHandle, $i);
$post[$fieldHandle]['rows'][$i] = array_values($row);
}
}
return $post;
}

/**
* {@inheritdoc}
*/
public function getWordCount(ElementTranslator $elementTranslator, Element $element, Field $field)
{
$wordCount = 0;

$data = $element->getFieldValue($field->handle);

$columns = $this->getTableColumns($data['columns']);

foreach ($data['rows'] as $i => $row) {
foreach ($columns as $id => $name) {
$value = $row[$id] ?? "";

$wordCount += Translations::$plugin->wordCounter->getWordCount($value);
}
}

return $wordCount;
}

private function getTableColumns($columnsData)
{
$columns = [];

foreach ($columnsData as $info) {
array_push($columns, $info['heading']);
}

return $columns;
}

private function isColumnDropdown($columnsData)
{
return $columnsData['type'] === 'select';
}

private function handleDropdownValues(&$post, &$row, $fieldHandle, $i)
{
$columnsData = $post[$fieldHandle]['columns'];
$columns = $this->getTableColumns($columnsData);

foreach ($columns as $id => $name) {
if ($this->isColumnDropdown($columnsData[$id])) {
foreach ($post[$fieldHandle]['columns'][$id]['options'] as $index => $dd) {
if ($dd['value'] === $post[$fieldHandle]['rows'][$i][$id]) {
$post[$fieldHandle]['columns'][$id]['options'][$index]['label'] = $row[$name];
$row[$name] = $post[$fieldHandle]['rows'][$i][$id];
}
}
}
}
}
}

0 comments on commit 728d90f

Please sign in to comment.