You are here

protected function LingotekSettingsTabContentForm::retrieveFields in Lingotek Translation 3.4.x

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

File

src/Form/LingotekSettingsTabContentForm.php, line 322

Class

LingotekSettingsTabContentForm
Configure Lingotek

Namespace

Drupal\lingotek\Form

Code

protected function retrieveFields(FormStateInterface $form_state, $entity_id, $bundle_id, $readOnly = FALSE) {
  $provideDefaults = $form_state
    ->getTemporaryValue('provideDefaults') ?: [];
  $entity_type = \Drupal::entityTypeManager()
    ->getDefinition($entity_id);

  /** @var \Drupal\lingotek\LingotekConfigurationServiceInterface $lingotek_config */
  $lingotek_config = \Drupal::service('lingotek.configuration');
  $content_translation_manager = \Drupal::service('content_translation.manager');
  $storage_definitions = \Drupal::service('entity_field.manager')
    ->getFieldStorageDefinitions($entity_id);
  $field_checkboxes = [];
  if ($content_translation_manager
    ->isSupported($entity_id)) {
    $fields = \Drupal::service('entity_field.manager')
      ->getFieldDefinitions($entity_id, $bundle_id);

    // Find which fields the user previously selected
    foreach ($fields as $field_id => $field_definition) {
      $checkbox_choice = 0;

      // We allow non-translatable entity_reference_revisions fields through.
      // See https://www.drupal.org/node/2788285
      if (!empty($storage_definitions[$field_id]) && $storage_definitions[$field_id]
        ->getProvider() != 'content_translation' && !in_array($storage_definitions[$field_id]
        ->getName(), [
        $entity_type
          ->getKey('langcode'),
        $entity_type
          ->getKey('default_langcode'),
        'revision_translation_affected',
      ]) && ($field_definition
        ->isTranslatable() || ($field_definition
        ->getType() == 'cohesion_entity_reference_revisions' || $field_definition
        ->getType() == 'entity_reference_revisions' || $field_definition
        ->getType() == 'path')) && !$field_definition
        ->isComputed() && !$field_definition
        ->isReadOnly()) {
        $checkbox_choice = 0;
        if ($value = $lingotek_config
          ->isFieldLingotekEnabled($entity_id, $bundle_id, $field_id)) {
          $checkbox_choice = $value;
        }
        if (isset($provideDefaults[$entity_id][$bundle_id]) && $provideDefaults[$entity_id][$bundle_id] && $lingotek_config
          ->shouldFieldLingotekEnabled($entity_id, $bundle_id, $field_id)) {
          $checkbox_choice = '1';
        }
        $id = 'edit-' . str_replace('_', '-', $entity_id) . '-' . str_replace('_', '-', $bundle_id) . ($readOnly ? '-readonly' : '') . '-fields-' . str_replace('_', '-', $field_id);
        $field_checkbox = [
          '#type' => 'checkbox',
          '#title' => $field_definition
            ->getLabel(),
          '#default_value' => $checkbox_choice,
          '#checked' => $checkbox_choice,
          '#name' => $entity_id . '[' . $bundle_id . ($readOnly ? '-readonly' : '') . '][fields][' . $field_id . ']',
          '#id' => $id,
          '#attributes' => [
            'data-drupal-selector' => $id,
            'id' => $id,
            'name' => $entity_id . '[' . $bundle_id . ($readOnly ? '-readonly' : '') . '][fields][' . $field_id . ']',
          ],
        ];
        if ($readOnly) {
          $field_checkbox['#attributes']['disabled'] = TRUE;
        }
        $field_checkboxes[$field_id] = $field_checkbox;

        // Display the column translatability configuration widget.
        module_load_include('inc', 'content_translation', 'content_translation.admin');
        $column_element = content_translation_field_sync_widget($field_definition);
        if ($column_element) {
          $default_properties = $lingotek_config
            ->getDefaultFieldPropertiesLingotekEnabled($entity_id, $bundle_id, $field_id);
          $properties_checkbox_choice = $lingotek_config
            ->getFieldPropertiesLingotekEnabled($entity_id, $bundle_id, $field_id);
          if ($provideDefaults && !$properties_checkbox_choice) {
            $properties_checkbox_choice = $default_properties;
          }
          foreach ($column_element['#options'] as $property_id => $property) {
            $checked = FALSE;
            if ($checkbox_choice) {
              $checked = isset($properties_checkbox_choice[$property_id]) ? $properties_checkbox_choice[$property_id] == '1' || $properties_checkbox_choice[$property_id] === $property_id : FALSE;
            }
            $id = 'edit-' . str_replace('_', '-', $entity_id) . '-' . str_replace('_', '-', $bundle_id) . ($readOnly ? '-readonly' : '') . '-fields-' . str_replace('_', '-', $field_id) . 'properties-' . str_replace('_', '-', $property_id);
            $property_checkbox = [
              '#type' => 'checkbox',
              '#title' => $property,
              '#default_value' => $checked,
              '#checked' => $checked,
              '#name' => $entity_id . '[' . $bundle_id . ($readOnly ? '-readonly' : '') . '][fields][' . $field_id . ':properties][' . $property_id . ']',
              '#id' => $id,
              '#attributes' => [
                'data-drupal-selector' => $id,
                'id' => $id,
                'name' => $entity_id . '[' . $bundle_id . ($readOnly ? '-readonly' : '') . '][fields][' . $field_id . ':properties][' . $property_id . ']',
                'class' => [
                  'field-property-checkbox',
                ],
              ],
            ];
            if ($readOnly) {
              $property_checkbox['#attributes']['disabled'] = TRUE;
            }
            $property_checkbox['#states']['checked'] = [
              [
                ':input[name="' . $field_checkbox['#name'] . '"]' => [
                  'checked' => TRUE,
                ],
                ':input[name="' . $property_checkbox['#name'] . '"]' => [
                  'checked' => TRUE,
                ],
              ],
            ];
            if ($checked || isset($default_properties[$property_id]) && $default_properties[$property_id] === $property_id) {
              $property_checkbox['#states']['unchecked'] = [
                ':input[name="' . $field_checkbox['#name'] . '"]' => [
                  'unchecked' => TRUE,
                ],
              ];
            }
            $field_checkboxes[$field_id . ':properties'][$property_id] = $property_checkbox;
          }
        }
      }
      elseif ($field_definition
        ->getType() == 'path' && $field_definition
        ->isComputed()) {
        if ($value = $lingotek_config
          ->isFieldLingotekEnabled($entity_id, $bundle_id, $field_id)) {
          $checkbox_choice = $value;
        }
        if (isset($provideDefaults[$entity_id][$bundle_id]) && $provideDefaults[$entity_id][$bundle_id] && $lingotek_config
          ->shouldFieldLingotekEnabled($entity_id, $bundle_id, $field_id)) {
          $checkbox_choice = '1';
        }
        $id = 'edit-' . str_replace('_', '-', $entity_id) . '-' . str_replace('_', '-', $bundle_id) . ($readOnly ? '-readonly' : '') . '-fields-' . str_replace('_', '-', $field_id);
        $field_checkbox = [
          '#type' => 'checkbox',
          '#title' => $field_definition
            ->getLabel(),
          '#checked' => $checkbox_choice,
          '#default_value' => $checkbox_choice,
          '#name' => $entity_id . '[' . $bundle_id . ($readOnly ? '-readonly' : '') . '][fields][' . $field_id . ']',
          '#id' => $id,
          '#attributes' => [
            'data-drupal-selector' => $id,
          ],
        ];
        if ($readOnly) {
          $field_checkbox['#attributes']['disabled'] = TRUE;
        }
        $field_checkboxes[$field_id] = $field_checkbox;
      }
    }
  }
  if ($entity_id === 'cohesion_layout') {
    $field_checkboxes['json_values']['#default_value'] = TRUE;
    $field_checkboxes['styles']['#default_value'] = FALSE;
    $field_checkboxes['template']['#default_value'] = FALSE;

    // field_checkboxes['json_values']['#attributes']['disabled'] = 'disabled';
    // $field_checkboxes['styles']['#attributes']['disabled'] = 'disabled';
    // $field_checkboxes['template']['#attributes']['disabled'] = 'disabled';
  }
  return $field_checkboxes;
}