You are here

public function LingotekConfigManagementForm::buildForm in Lingotek Translation 8

Same name and namespace in other branches
  1. 8.2 src/Form/LingotekConfigManagementForm.php \Drupal\lingotek\Form\LingotekConfigManagementForm::buildForm()
  2. 4.0.x src/Form/LingotekConfigManagementForm.php \Drupal\lingotek\Form\LingotekConfigManagementForm::buildForm()
  3. 3.0.x src/Form/LingotekConfigManagementForm.php \Drupal\lingotek\Form\LingotekConfigManagementForm::buildForm()
  4. 3.1.x src/Form/LingotekConfigManagementForm.php \Drupal\lingotek\Form\LingotekConfigManagementForm::buildForm()
  5. 3.2.x src/Form/LingotekConfigManagementForm.php \Drupal\lingotek\Form\LingotekConfigManagementForm::buildForm()
  6. 3.3.x src/Form/LingotekConfigManagementForm.php \Drupal\lingotek\Form\LingotekConfigManagementForm::buildForm()
  7. 3.4.x src/Form/LingotekConfigManagementForm.php \Drupal\lingotek\Form\LingotekConfigManagementForm::buildForm()
  8. 3.5.x src/Form/LingotekConfigManagementForm.php \Drupal\lingotek\Form\LingotekConfigManagementForm::buildForm()
  9. 3.6.x src/Form/LingotekConfigManagementForm.php \Drupal\lingotek\Form\LingotekConfigManagementForm::buildForm()
  10. 3.7.x src/Form/LingotekConfigManagementForm.php \Drupal\lingotek\Form\LingotekConfigManagementForm::buildForm()
  11. 3.8.x src/Form/LingotekConfigManagementForm.php \Drupal\lingotek\Form\LingotekConfigManagementForm::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 FormInterface::buildForm

File

src/Form/LingotekConfigManagementForm.php, line 144
Contains \Drupal\Lingotek\Form\LingotekConfigManagementForm.

Class

LingotekConfigManagementForm
Form for bulk management of content.

Namespace

Drupal\lingotek\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  if ($redirect = $this
    ->checkSetup()) {
    return $redirect;
  }
  $showingFields = FALSE;
  $this->filter = $this
    ->getFilter();

  // ToDo: Find a better filter?
  if ($this->filter === 'config') {
    $mappers = array_filter($this->mappers, function ($mapper) {
      return $mapper instanceof ConfigNamesMapper && !$mapper instanceof ConfigEntityMapper && !$mapper instanceof ConfigFieldMapper;
    });
  }
  elseif (substr($this->filter, -7) == '_fields') {
    $showingFields = TRUE;
    $mapper = $this->mappers[$this->filter];
    $base_entity_type = $mapper
      ->getPluginDefinition()['base_entity_type'];
    $ids = \Drupal::entityQuery('field_config')
      ->condition('id', $base_entity_type . '.', 'STARTS_WITH')
      ->execute();
    $fields = FieldConfig::loadMultiple($ids);
    $mappers = [];
    foreach ($fields as $id => $field) {
      $new_mapper = clone $mapper;
      $new_mapper
        ->setEntity($field);
      $mappers[$field
        ->id()] = $new_mapper;
    }
  }
  else {
    $mapper = $this->mappers[$this->filter];
    $ids = \Drupal::entityQuery($this->filter)
      ->execute();
    $entities = \Drupal::entityManager()
      ->getStorage($this->filter)
      ->loadMultiple($ids);

    /** @var ConfigEntityMapper $mapper  */
    $mappers = [];
    foreach ($entities as $entity) {
      $new_mapper = clone $mapper;
      $new_mapper
        ->setEntity($entity);
      $mappers[$entity
        ->id()] = $new_mapper;
    }
  }
  $rows = [];
  foreach ($mappers as $mapper_id => $mapper) {
    $is_config_entity = $mapper instanceof ConfigEntityMapper;
    $source = $this
      ->getSourceStatus($mapper);
    $translations = $this
      ->getTranslationsStatuses($mapper);
    $profile = $is_config_entity ? $this->lingotekConfiguration
      ->getConfigEntityProfile($mapper
      ->getEntity(), FALSE) : $this->lingotekConfiguration
      ->getConfigProfile($mapper_id, FALSE);
    $form['table'][$mapper_id] = [
      '#type' => 'checkbox',
      '#value' => $mapper_id,
    ];
    $rows[$mapper_id] = [];
    $rows[$mapper_id] += [
      'title' => $mapper
        ->getTitle(),
      'source' => $source,
      'translations' => $translations,
      'profile' => $profile ? $profile
        ->label() : '',
    ];
    if ($showingFields) {
      $entity_type_id = $mapper
        ->getEntity()
        ->get('entity_type');
      $bundle = $mapper
        ->getEntity()
        ->get('bundle');
      $bundle_info = \Drupal::service('entity_type.bundle.info')
        ->getBundleInfo($entity_type_id);
      if (isset($bundle_info[$bundle])) {
        $rows[$mapper_id]['bundle'] = $bundle_info[$bundle]['label'];
      }
      else {
        $rows[$mapper_id]['bundle'] = $bundle;
      }
    }
  }

  // Add filters.
  $form['filters'] = array(
    '#type' => 'details',
    '#title' => $this
      ->t('Select config bundle'),
    '#open' => TRUE,
    '#weight' => 5,
    '#tree' => TRUE,
  );
  $form['filters']['wrapper'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'form--inline',
        'clearfix',
      ),
    ),
  );
  $form['filters']['wrapper']['bundle'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Filter'),
    '#options' => [
      'config' => $this
        ->t('Config'),
    ] + $this
      ->getAllBundles(),
    '#default_value' => $this->filter,
    '#attributes' => array(
      'class' => array(
        'form-item',
      ),
    ),
  );
  $form['filters']['actions'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'clearfix',
      ),
    ),
  );
  $form['filters']['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => $this
      ->t('Filter'),
    '#submit' => array(
      '::filterForm',
    ),
  );

  // If we are showing field config instances, we need to show bundles for
  // a better UX.
  $headers = $showingFields ? [
    'bundle' => $this
      ->t('Bundle'),
  ] : [];
  $headers += [
    'title' => $this
      ->t('Entity'),
    'source' => $this
      ->t('Source'),
    'translations' => $this
      ->t('Translations'),
    'profile' => $this
      ->t('Profile'),
  ];

  // Build an 'Update options' form.
  $form['options'] = array(
    '#type' => 'details',
    '#title' => $this
      ->t('Bulk document management'),
    '#open' => TRUE,
    '#attributes' => array(
      'class' => array(
        'container-inline',
      ),
    ),
    '#weight' => 10,
  );
  $form['options']['operation'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Action'),
    '#title_display' => 'invisible',
    '#options' => $this
      ->generateBulkOptions(),
  );
  $form['options']['submit'] = array(
    '#type' => 'submit',
    '#value' => $this
      ->t('Execute'),
  );
  $form['table'] = [
    '#header' => $headers,
    '#options' => $rows,
    '#empty' => $this
      ->t('No content available'),
    '#type' => 'tableselect',
    '#weight' => 30,
  ];
  $form['pager'] = [
    '#type' => 'pager',
    '#weight' => 50,
  ];
  $form['#attached']['library'][] = 'lingotek/lingotek';
  return $form;
}