You are here

public function LingotekManagementForm::buildForm in Lingotek Translation 8

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/LingotekManagementForm.php, line 178
Contains \Drupal\Lingotek\Form\LingotekManagementForm.

Class

LingotekManagementForm
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;
  }
  $items_per_page = $this
    ->getItemsPerPage();

  /** @var PrivateTempStore $temp_store */
  $temp_store = $this->tempStoreFactory
    ->get('lingotek.management.filter.' . $this->entityTypeId);
  $entity_type = $this->entityManager
    ->getDefinition($this->entityTypeId);
  $properties = $this->entityManager
    ->getBaseFieldDefinitions($this->entityTypeId);
  $query = $this->entityQuery
    ->get($this->entityTypeId)
    ->pager($items_per_page);
  $has_bundles = $entity_type
    ->get('bundle_entity_type') != 'bundle';

  // Filter results.
  $labelFilter = $temp_store
    ->get('label');
  $bundleFilter = $temp_store
    ->get('bundle');
  $profileFilter = $temp_store
    ->get('profile');
  $sourceLanguageFilter = $temp_store
    ->get('source_language');
  if ($has_bundles && $bundleFilter) {
    $query
      ->condition($entity_type
      ->getKey('bundle'), $bundleFilter);
  }
  if ($labelFilter) {
    $labelKey = $entity_type
      ->getKey('label');
    if ($labelKey) {
      $query
        ->condition($labelKey, '%' . $labelFilter . '%', 'LIKE');
    }
  }
  if ($profileFilter) {
    $query
      ->condition('lingotek_profile', $profileFilter);
  }
  if ($sourceLanguageFilter) {
    $query
      ->condition($entity_type
      ->getKey('langcode'), $sourceLanguageFilter);
    $query
      ->condition('default_langcode', 1);
  }

  // We don't want items with language undefined.
  $query
    ->condition($entity_type
    ->getKey('langcode'), LanguageInterface::LANGCODE_NOT_SPECIFIED, '!=');
  $ids = $query
    ->execute();
  $entities = $this->entityManager
    ->getStorage($this->entityTypeId)
    ->loadMultiple($ids);
  $rows = [];
  if (!empty($entities)) {
    foreach ($entities as $entity_id => $entity) {
      $source = $this
        ->getSourceStatus($entity);
      $translations = $this
        ->getTranslationsStatuses($entity);
      $profile = $this->lingotekConfiguration
        ->getEntityProfile($entity, FALSE);
      $form['table'][$entity_id] = [
        '#type' => 'checkbox',
        '#value' => $entity
          ->id(),
      ];
      $rows[$entity_id] = [];
      if ($has_bundles) {
        $rows[$entity_id]['bundle'] = $this->entityManager
          ->getBundleInfo($entity
          ->getEntityTypeId())[$entity
          ->bundle()]['label'];
      }
      $rows[$entity_id] += [
        'title' => $entity
          ->hasLinkTemplate('canonical') ? $this
          ->getLinkGenerator()
          ->generate($entity
          ->label(), Url::fromRoute($entity
          ->urlInfo()
          ->getRouteName(), [
          $this->entityTypeId => $entity
            ->id(),
        ])) : $entity
          ->id(),
        'source' => $source,
        'translations' => $translations,
        'profile' => $profile ? $profile
          ->label() : '',
      ];
    }
  }
  $headers = [];
  if ($has_bundles) {
    $headers['bundle'] = $entity_type
      ->getBundleLabel();
  }
  $headers += [
    'title' => $has_bundles && $entity_type
      ->hasKey('label') ? $properties[$entity_type
      ->getKey('label')]
      ->getLabel() : $entity_type
      ->getLabel(),
    'source' => $this
      ->t('Source'),
    'translations' => $this
      ->t('Translations'),
    'profile' => $this
      ->t('Profile'),
  ];

  // Add filters.
  $form['filters'] = array(
    '#type' => 'details',
    '#title' => $this
      ->t('Filter'),
    '#open' => TRUE,
    '#weight' => 5,
    '#tree' => TRUE,
  );
  $form['filters']['wrapper'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'form--inline',
        'clearfix',
      ),
    ),
  );
  if ($entity_type
    ->getKey('label')) {
    $form['filters']['wrapper']['label'] = array(
      '#type' => 'textfield',
      '#title' => $has_bundles && $entity_type
        ->hasKey('label') ? $properties[$entity_type
        ->getKey('label')]
        ->getLabel() : $entity_type
        ->getLabel(),
      '#placeholder' => $this
        ->t('Filter by @title', [
        '@title' => $entity_type
          ->getBundleLabel(),
      ]),
      '#default_value' => $labelFilter,
      '#attributes' => array(
        'class' => array(
          'form-item',
        ),
      ),
    );
  }
  if ($has_bundles) {
    $form['filters']['wrapper']['bundle'] = array(
      '#type' => 'select',
      '#title' => $entity_type
        ->getBundleLabel(),
      '#options' => [
        '' => $this
          ->t('All'),
      ] + $this
        ->getAllBundles(),
      '#default_value' => $bundleFilter,
      '#attributes' => array(
        'class' => array(
          'form-item',
        ),
      ),
    );
  }
  $form['filters']['wrapper']['source_language'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Source language'),
    '#options' => [
      '' => $this
        ->t('All languages'),
    ] + $this
      ->getAllLanguages(),
    '#default_value' => $sourceLanguageFilter,
    '#attributes' => array(
      'class' => array(
        'form-item',
      ),
    ),
  );
  $form['filters']['wrapper']['profile'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Profile'),
    '#options' => [
      '' => $this
        ->t('All'),
    ] + $this->lingotekConfiguration
      ->getProfileOptions(),
    '#default_value' => $profileFilter,
    '#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',
    ),
  );
  $form['filters']['actions']['reset'] = array(
    '#type' => 'submit',
    '#value' => $this
      ->t('Reset'),
    '#submit' => array(
      '::resetFilterForm',
    ),
  );

  // 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['items_per_page'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Results per page:'),
    '#options' => [
      10 => 10,
      25 => 25,
      50 => 50,
      100 => 100,
      250 => 250,
      500 => 500,
    ],
    '#default_value' => $items_per_page,
    '#weight' => 60,
    '#ajax' => [
      'callback' => [
        $this,
        'itemsPerPageCallback',
      ],
      'event' => 'change',
    ],
  ];
  $form['#attached']['library'][] = 'lingotek/lingotek';
  $form['#attached']['library'][] = 'lingotek/lingotek.manage';
  return $form;
}