LockFieldsFormManager.php in TMGMT Translator Smartling 8.4
File
src/Smartling/LockFields/LockFieldsFormManager.php
View source
<?php
namespace Drupal\tmgmt_smartling\Smartling\LockFields;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
class LockFieldsFormManager {
private $lockFieldsManager;
private $languageManager;
public function __construct(LockFieldsManager $lock_fields_manager, LanguageManagerInterface $language_manager) {
$this->lockFieldsManager = $lock_fields_manager;
$this->languageManager = $language_manager;
}
public function addLockFieldsListToForm(array &$form, FormStateInterface $form_state) {
$entity = $form_state
->getFormObject()
->getEntity();
$entity_lang_code = $entity
->get('default_langcode')
->getLangcode();
$default_site_lang_code = $this->languageManager
->getDefaultLanguage()
->getId();
$translatable_fields = $this->lockFieldsManager
->getTranslatableFieldsByContentEntity($entity);
if ($entity_lang_code != $default_site_lang_code) {
$form['smartling'] = [
'#type' => 'details',
'#title' => new TranslatableMarkup('Smartling management'),
'#group' => 'advanced',
'#attached' => [
'drupalSettings' => [
'smartling' => [
'checkAllId' => [
'edit-locked-fields',
],
],
],
'library' => [
'tmgmt_smartling/checkAll',
],
],
];
$form['smartling']['locked_fields'] = [
'#title' => new TranslatableMarkup('Lock fields'),
'#type' => 'checkboxes',
'#description' => t('Selected fields will not be overwritten.'),
'#options' => $translatable_fields,
'#default_value' => $this->lockFieldsManager
->getLockedFieldsByContentEntity($entity),
];
$form['smartling']['locked_fields']['check_all'] = [
'#weight' => 1,
'#markup' => '<div class="check-control"><a href="#" class="check-all">' . new TranslatableMarkup('Check all / Uncheck All') . '</a></div>',
];
$form['actions']['submit']['#submit'][] = [
LockFieldsFormManager::class,
'submitLockFieldsList',
];
}
}
public static function submitLockFieldsList(array &$form, FormStateInterface $form_state) {
\Drupal::getContainer()
->get('tmgmt_smartling.lock_fields_manager')
->setLockedFields($form_state
->getFormObject()
->getEntity(), $form_state
->getValue('locked_fields'));
}
}