diff --git a/sources/content/src/Plugin/tmgmt/Source/ContentEntitySource.php b/sources/content/src/Plugin/tmgmt/Source/ContentEntitySource.php index ff98065..b3b054f 100644 --- a/sources/content/src/Plugin/tmgmt/Source/ContentEntitySource.php +++ b/sources/content/src/Plugin/tmgmt/Source/ContentEntitySource.php @@ -11,6 +11,7 @@ use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\Plugin\DataType\EntityReference; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemInterface; +use Drupal\Core\Field\Plugin\Field\FieldType\StringItem; use Drupal\Core\TypedData\OptionsProviderInterface; use Drupal\Core\TypedData\Type\StringInterface; use Drupal\Core\TypedData\PrimitiveInterface; @@ -124,6 +125,11 @@ class ContentEntitySource extends SourcePluginBase { '#text' => $property->getValue(), '#translate' => $translate, ); + if ($translate) { + if ($field_item instanceof StringItem) { + $data[$key][$index][$property_key]['#max_length'] = $field_item->defaultStorageSettings()['max_length']; + } + } if ($property_definition->getDataType() == 'filter_format') { $format = $property->getValue(); diff --git a/src/Form/JobItemForm.php b/src/Form/JobItemForm.php index b40681e..309975e 100644 --- a/src/Form/JobItemForm.php +++ b/src/Form/JobItemForm.php @@ -7,11 +7,11 @@ namespace Drupal\tmgmt\Form; +use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\Xss; -use Drupal\Core\Url; use Drupal\tmgmt\Entity\JobItem; use Drupal\tmgmt\JobItemInterface; use Drupal\tmgmt\TranslatorRejectDataInterface; @@ -172,6 +172,25 @@ class JobItemForm extends TmgmtFormBase { } /** + * Gets the translatable fields of a given job item. + * + * @param JobItemInterface $item + * The job item. + * + * @return array $fields + * Returns the translatable fields of the job item. + */ + private function getTranslatableFields(JobItemInterface $item) { + $fields = []; + foreach ($item->getData() as $key => $value) { + if (isset($value[0]['value']['#translate']) && $value[0]['value']['#translate']) { + $fields[$key] = $value; + } + } + return $fields; + } + + /** * Gets the parent key given the parent array and the key of the child. * * @param string $name @@ -196,6 +215,7 @@ class JobItemForm extends TmgmtFormBase { */ public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); + /** @var JobItem $item */ $item = $this->buildEntity($form, $form_state); // First invoke the validation method on the source controller. $source_ui = $this->sourceManager->createUIInstance($item->getPlugin()); @@ -205,12 +225,30 @@ class JobItemForm extends TmgmtFormBase { $translator_ui = $this->translatorManager->createUIInstance($item->getTranslator()->getPluginId()); $translator_ui->reviewFormValidate($form, $form_state, $item); } + + $translatable_fields = $this->getTranslatableFields($item); foreach ($form_state->getValues() as $key => $value) { - if (is_array($value) && isset($value['translation'])) { - // If there is an empty field then sets and error to fill it. - if (($value['translation'] == '') && ($value['source'] != '') && ($form_state->getTriggeringElement()['#value'] != 'Validate HTML tags')) { - $parent_key = $this->getParentKey($key, $form['review']); - $form_state->setError($form['review'][$parent_key][$key]['translation'], $this->t('The field is empty')); + $parent_key = $this->getParentKey($key, $form['review']); + if (array_key_exists($parent_key, $translatable_fields) && ($form_state->getTriggeringElement()['#value'] != 'Validate HTML tags')) { + // If has HTML tags will be an array. + if (is_array($value['translation'])) { + $label = $value['translation']['value']; + } + else { + $label = $value['translation']; + } + + // Validate that is not empty. + if (empty($label)) { + $form_state->setError($form['review'][$parent_key][$key]['translation'], $this->t('The field is empty.')); + continue; + } + + // Validate that is not longer than the max length. + if (isset($translatable_fields[$parent_key][0]['value']['#max_length']) + && ($translatable_fields[$parent_key][0]['value']['#max_length'] < strlen($label))) { + $form_state->setError($form['review'][$parent_key][$key]['translation'], + $this->t('The field has more than :size characters.', array(':size' => $translatable_fields[$parent_key][0]['value']['#max_length']))); } } } diff --git a/src/Tests/TMGMTUiTest.php b/src/Tests/TMGMTUiTest.php index f20c7f4..8cf689a 100644 --- a/src/Tests/TMGMTUiTest.php +++ b/src/Tests/TMGMTUiTest.php @@ -498,23 +498,46 @@ class TMGMTUiTest extends TMGMTTestBase { // Tests that there is always a title. \Drupal::state()->set('tmgmt.test_source_data', array( 'title' => array( - '0|value' => array( - '#text' => '

Source text bold and Italic

', - '#label' => 'Title', + array( + 'value' => array( + '#text' => '

Source text bold and Italic

', + '#label' => 'Title', + '#translate' => TRUE, + ), ), ), 'body' => array( 'deep_nesting' => array( '#text' => '

Source body bold and Italic

', '#label' => 'Body', - ) + '#translate' => TRUE, + ), ), )); $item5 = $job->addItem('test_source', 'test', 4); - $this->drupalGet('admin/tmgmt/items/' . $item5->id()); - $this->drupalPostForm(NULL, [], t('Save')); - $this->assertText(t('The field is empty')); + $this->drupalPostForm('admin/tmgmt/items/' . $item5->id(), [], t('Save')); + $this->assertText(t('The field is empty.')); + + // Tests field is less than max_length. + \Drupal::state()->set('tmgmt.test_source_data', array( + 'title' => array( + array( + 'value' => array( + '#text' => '

Source text bold and Italic

', + '#label' => 'Title', + '#translate' => TRUE, + '#max_length' => 10, + ), + ), + ), + )); + $item5 = $job->addItem('test_source', 'test', 4); + + $this->drupalPostForm('admin/tmgmt/items/' . $item5->id(), [ + 'title|0|value[translation]' => '

Source text bold and Italic

', + ], t('Save')); + $this->assertText(t('The field has more than :size characters.', array(':size' => 10))); // Test for the text with format set. \Drupal::state()->set('tmgmt.test_source_data', array( diff --git a/tmgmt_test/src/Plugin/tmgmt/Source/TestSource.php b/tmgmt_test/src/Plugin/tmgmt/Source/TestSource.php index e2e1227..eee578c 100644 --- a/tmgmt_test/src/Plugin/tmgmt/Source/TestSource.php +++ b/tmgmt_test/src/Plugin/tmgmt/Source/TestSource.php @@ -67,6 +67,7 @@ class TestSource extends SourcePluginBase { 'deep_nesting' => array( '#text' => 'Text for job item with type @type and id @id.', '#label' => 'Label for job item with type @type and id @id.', + '#translate' => TRUE, ), ), ));