You are here

public function LingotekSettingsTabContentForm::buildForm in Lingotek Translation 3.1.x

Same name and namespace in other branches
  1. 8 src/Form/LingotekSettingsTabContentForm.php \Drupal\lingotek\Form\LingotekSettingsTabContentForm::buildForm()
  2. 8.2 src/Form/LingotekSettingsTabContentForm.php \Drupal\lingotek\Form\LingotekSettingsTabContentForm::buildForm()
  3. 4.0.x src/Form/LingotekSettingsTabContentForm.php \Drupal\lingotek\Form\LingotekSettingsTabContentForm::buildForm()
  4. 3.0.x src/Form/LingotekSettingsTabContentForm.php \Drupal\lingotek\Form\LingotekSettingsTabContentForm::buildForm()
  5. 3.2.x src/Form/LingotekSettingsTabContentForm.php \Drupal\lingotek\Form\LingotekSettingsTabContentForm::buildForm()
  6. 3.3.x src/Form/LingotekSettingsTabContentForm.php \Drupal\lingotek\Form\LingotekSettingsTabContentForm::buildForm()
  7. 3.4.x src/Form/LingotekSettingsTabContentForm.php \Drupal\lingotek\Form\LingotekSettingsTabContentForm::buildForm()
  8. 3.5.x src/Form/LingotekSettingsTabContentForm.php \Drupal\lingotek\Form\LingotekSettingsTabContentForm::buildForm()
  9. 3.6.x src/Form/LingotekSettingsTabContentForm.php \Drupal\lingotek\Form\LingotekSettingsTabContentForm::buildForm()
  10. 3.7.x src/Form/LingotekSettingsTabContentForm.php \Drupal\lingotek\Form\LingotekSettingsTabContentForm::buildForm()
  11. 3.8.x src/Form/LingotekSettingsTabContentForm.php \Drupal\lingotek\Form\LingotekSettingsTabContentForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/LingotekSettingsTabContentForm.php, line 40

Class

LingotekSettingsTabContentForm
Configure Lingotek

Namespace

Drupal\lingotek\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\lingotek\LingotekConfigurationServiceInterface $lingotek_config */
  $lingotek_config = \Drupal::service('lingotek.configuration');
  $entity_type_definitions = \Drupal::entityTypeManager()
    ->getDefinitions();

  // Get the profiles
  $this
    ->retrieveProfileOptions();

  // Retrieve bundles
  $this
    ->retrieveBundles();

  // Retrieve translatable bundles
  $this
    ->retrieveTranslatableBundles();
  $readOnly = FALSE;
  if ($this->countTranslatableBundles > self::CONTENT_SINGLE_FORM_THRESHOLD) {
    $readOnly = TRUE;
  }
  $form['parent_details'] = [
    '#type' => 'details',
    '#title' => t('Translate Content Entities'),
  ];
  $form['parent_details']['list']['#type'] = 'container';
  $form['parent_details']['list']['#attributes']['class'][] = 'entity-meta';

  // If user specifies no translatable entities, post this message
  if (empty($this->translatable_bundles)) {
    $form['parent_details']['empty_message'] = [
      '#markup' => t('There are no translatable content entities specified. You can enable translation for the desired content entities on the <a href=":translation-entity">Content language</a> page.', [
        ':translation-entity' => $this->urlGenerator
          ->generateFromRoute('language.content_settings_page'),
      ]),
    ];
  }

  // I. Loop through all entities and create a details container for each
  foreach ($this->translatable_bundles as $entity_id => $bundles) {
    $entity_key = 'entity-' . $entity_id;
    $form['parent_details']['list'][$entity_key] = [
      '#type' => 'details',
      '#title' => $entity_type_definitions[$entity_id]
        ->getLabel(),
      'content' => [],
    ];

    /** @var \Drupal\lingotek\Moderation\LingotekModerationFactoryInterface $moderationFactory */
    $moderationFactory = \Drupal::service('lingotek.moderation_factory');

    /** @var \Drupal\lingotek\Moderation\LingotekModerationSettingsFormInterface $moderationForm */
    $moderationForm = $moderationFactory
      ->getModerationSettingsForm();
    $bundle_label = $entity_type_definitions[$entity_id]
      ->getBundleLabel();
    $header = [
      $this
        ->t('Enable'),
      $bundle_label,
      $this
        ->t('Translation Profile'),
      'moderation' => $moderationForm
        ->getColumnHeader(),
      $this
        ->t('Fields'),
    ];
    if (!$moderationForm
      ->needsColumn($entity_id)) {
      unset($header['moderation']);
    }
    $table = [
      '#type' => 'table',
      '#header' => $header,
      '#empty' => $this
        ->t('No Entries'),
    ];

    // II. Loop through bundles per entity and make a table
    foreach ($bundles as $bundle_id => $bundle) {
      $row = [];
      $row['enabled'] = [
        '#type' => 'checkbox',
        '#label' => $this
          ->t('Enabled'),
        '#default_value' => $lingotek_config
          ->isEnabled($entity_id, $bundle_id),
        '#id' => 'edit-' . str_replace('_', '-', $entity_id) . '-' . str_replace('_', '-', $bundle_id) . ($readOnly ? '-readonly' : '') . '-enabled',
        '#name' => $entity_id . '[' . $bundle_id . ($readOnly ? '-readonly' : '') . '][enabled]',
        '#ajax' => [
          'callback' => [
            $this,
            'ajaxRefreshEntityFieldsForm',
          ],
          'progress' => [
            'type' => 'throbber',
            'message' => NULL,
          ],
          'wrapper' => 'container-' . str_replace('_', '-', $entity_id) . '-' . str_replace('_', '-', $bundle_id) . ($readOnly ? '-readonly' : ''),
        ],
      ];
      $row['content_type'] = [
        '#type' => 'container',
      ];
      $row['content_type']['item'] = [
        '#type' => 'item',
        '#title' => $bundle['label'],
      ];
      if ($readOnly) {
        $row['content_type']['edit'] = [
          '#type' => 'link',
          '#title' => $this
            ->t('edit settings individually'),
          '#url' => Url::fromRoute('lingotek.settings.content_form', [
            'entity_type' => $entity_id,
            'bundle' => $bundle_id,
          ]),
          '#ajax' => [
            'class' => [
              'use-ajax',
            ],
          ],
          '#attributes' => [
            'class' => [
              'use-ajax',
            ],
            'data-dialog-type' => 'modal',
            'data-dialog-options' => Json::encode([
              'width' => 861,
              'height' => 700,
            ]),
          ],
        ];
        $row['enabled']['#attributes']['disabled'] = TRUE;
      }
      $row['profiles'] = $this
        ->retrieveProfiles($entity_id, $bundle_id);
      $moderation = $moderationForm
        ->form($entity_id, $bundle_id);
      if (!empty($moderation)) {
        $row['moderation'] = $moderation;
      }
      $row['fields_container'] = $this
        ->generateFieldsForm($form_state, $entity_id, $bundle_id);
      $table[$bundle_id] = $row;
    }

    // III. Add table to respective details
    $form['parent_details']['list'][$entity_key]['content'][$entity_id] = $table;
  }
  if (!empty($this->translatable_bundles)) {
    $form['parent_details']['note'] = [
      '#markup' => t('Note: changing the profile will update all settings for existing nodes except for the project, workflow, vault, and storage method (e.g. node/field)'),
    ];
    $form['parent_details']['actions']['#type'] = 'actions';
    $form['parent_details']['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save'),
      '#button_type' => 'primary',
    ];
  }
  $form['#attached']['library'][] = 'lingotek/lingotek.settings';
  return $form;
}