You are here

public function ContentEntitySourcePluginUi::overviewSearchFormPart in Translation Management Tool 8

Builds search form for entity sources overview.

Parameters

array $form: Drupal form array.

FormStateInterface $form_state: Drupal form_state array.

string $type: Entity type.

Return value

array Drupal form array.

Overrides SourcePluginUiBase::overviewSearchFormPart

File

sources/content/src/ContentEntitySourcePluginUi.php, line 36

Class

ContentEntitySourcePluginUi
Content entity source plugin UI.

Namespace

Drupal\tmgmt_content

Code

public function overviewSearchFormPart(array $form, FormStateInterface $form_state, $type) {
  $form = parent::overviewSearchFormPart($form, $form_state, $type);
  $entity_type = \Drupal::entityTypeManager()
    ->getDefinition($type);
  $field_definitions = \Drupal::service('entity_field.manager')
    ->getBaseFieldDefinitions($type);
  $label_key = $entity_type
    ->getKey('label');
  if (!empty($label_key)) {
    $label = (string) $field_definitions[$label_key]
      ->getlabel();
    $form['search_wrapper']['search'][$label_key] = array(
      '#type' => 'textfield',
      '#title' => $label,
      '#size' => 25,
      '#default_value' => isset($_GET[$label_key]) ? $_GET[$label_key] : NULL,
    );
  }
  $form['search_wrapper']['search']['langcode'] = array(
    '#type' => 'language_select',
    '#title' => t('Source Language'),
    '#empty_option' => t('- Any -'),
    '#default_value' => isset($_GET['langcode']) ? $_GET['langcode'] : NULL,
  );
  $bundle_key = $entity_type
    ->getKey('bundle');
  $bundle_options = $this
    ->getTranslatableBundles($type);
  if (count($bundle_options) > 1) {
    $form['search_wrapper']['search'][$bundle_key] = array(
      '#type' => 'select',
      '#title' => $entity_type
        ->getBundleLabel(),
      '#options' => $bundle_options,
      '#empty_option' => t('- Any -'),
      '#default_value' => isset($_GET[$bundle_key]) ? $_GET[$bundle_key] : NULL,
    );
  }
  elseif (count($bundle_options) == 0) {
    $this
      ->messenger()
      ->addWarning($this
      ->t('Entity translation is not enabled for any of existing content types. To use this functionality go to Content types administration and enable entity translation for desired content types.'));
    unset($form['search_wrapper']);
    return $form;
  }
  $form['search_wrapper']['search']['target_language'] = array(
    '#type' => 'language_select',
    '#title' => $this
      ->t('Target language'),
    '#empty_option' => $this
      ->t('- Any -'),
    '#default_value' => isset($_GET['target_language']) ? $_GET['target_language'] : NULL,
  );
  $form['search_wrapper']['search']['target_status'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Target status'),
    '#options' => array(
      'untranslated_or_outdated' => $this
        ->t('Untranslated or outdated'),
      'untranslated' => $this
        ->t('Untranslated'),
      'outdated' => $this
        ->t('Outdated'),
    ),
    '#default_value' => isset($_GET['target_status']) ? $_GET['target_status'] : NULL,
    '#states' => array(
      'invisible' => array(
        ':input[name="search[target_language]"]' => array(
          'value' => '',
        ),
      ),
    ),
  );
  return $form;
}