You are here

class LockFieldsFormManager in TMGMT Translator Smartling 8.4

Class LockFieldsFormManager

@package Drupal\tmgmt_smartling\Smartling\LockFields

Hierarchy

Expanded class hierarchy of LockFieldsFormManager

1 string reference to 'LockFieldsFormManager'
tmgmt_smartling.services.yml in ./tmgmt_smartling.services.yml
tmgmt_smartling.services.yml
1 service uses LockFieldsFormManager
tmgmt_smartling.lock_fields_form_manager in ./tmgmt_smartling.services.yml
\Drupal\tmgmt_smartling\Smartling\LockFields\LockFieldsFormManager

File

src/Smartling/LockFields/LockFieldsFormManager.php, line 14

Namespace

Drupal\tmgmt_smartling\Smartling\LockFields
View source
class LockFieldsFormManager {
  private $lockFieldsManager;
  private $languageManager;

  /**
   * LockFieldsFormManager constructor.
   *
   * @param LockFieldsManager $lock_fields_manager
   * @param LanguageManagerInterface $language_manager
   */
  public function __construct(LockFieldsManager $lock_fields_manager, LanguageManagerInterface $language_manager) {
    $this->lockFieldsManager = $lock_fields_manager;
    $this->languageManager = $language_manager;
  }

  /**
   * Extends given form array with 'Lock fields' tab.
   *
   * @param $form
   * @param FormStateInterface $form_state
   *
   * @return array
   */
  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',
      ];
    }
  }

  /**
   * Saves locked fields list using States API
   *
   * We don't use TMGMT Job Item
   * for storing data because there are will be situations when one entity
   * sits in many TMGMT Job Items and it will not be clear which item to choose
   * for getting/setting locked fields list.
   *
   * This callback is intentionally static and passed to form as
   *
   * @code
   * $form['actions']['submit']['#submit'][] = [
   *   LockFieldsFormManager::class,
   *   'submitLockFieldsList'
   * ];
   * @endcode
   *
   * to avoid the serialization issue because $this contains implicit relation
   * to \Drupal\Core\Database\Connection object which is not serializable and
   * throws an exception from __sleep method.
   *
   * @param array $form
   * @param FormStateInterface $form_state
   */
  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'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LockFieldsFormManager::$languageManager private property
LockFieldsFormManager::$lockFieldsManager private property
LockFieldsFormManager::addLockFieldsListToForm public function Extends given form array with 'Lock fields' tab.
LockFieldsFormManager::submitLockFieldsList public static function Saves locked fields list using States API
LockFieldsFormManager::__construct public function LockFieldsFormManager constructor.