You are here

public function DepthUpdateForm::buildForm in Taxonomy Term Depth 8

Same name and namespace in other branches
  1. 8.2 src/Form/DepthUpdateForm.php \Drupal\taxonomy_term_depth\Form\DepthUpdateForm::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/DepthUpdateForm.php, line 30

Class

DepthUpdateForm

Namespace

Drupal\taxonomy_term_depth\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $vocabulary = NULL) {

  //Ensure that we have vocabulary
  $vocabulary = \Drupal::request()
    ->get('taxonomy_vocabulary');

  /**
   * @var \Drupal\Core\Database\Connection
   */
  $dbh = \Drupal::database();
  $countAll = $dbh
    ->select('taxonomy_term_field_data', 'ttd')
    ->condition('ttd.vid', $vocabulary
    ->id())
    ->countQuery()
    ->execute()
    ->fetchField();
  $countEmpty = $dbh
    ->select('taxonomy_term_field_data', 'ttd')
    ->condition('ttd.vid', $vocabulary
    ->id())
    ->condition('ttd.depth_level', '', 'IS NULL')
    ->countQuery()
    ->execute()
    ->fetchField();

  // Truncate until two digits at the end without rounding the value.
  $percentProcessed = floor((100 - 100 * $countEmpty / $countAll) * 100) / 100;
  $percentProcessed = $percentProcessed > 100 ? 100 : $percentProcessed;
  $form['display']['processed_info'] = [
    '#type' => 'item',
    'value' => [
      '#markup' => '
            <span class="title">Processed</span>
            <span class="value">' . $percentProcessed . '</span>
            <span class="suffix">%</span>
        ',
    ],
  ];
  if ($percentProcessed < 100 && ($queued_count = taxonomy_term_depth_queue_manager($vocabulary
    ->id())
    ->queueSize()) > 1) {
    $form['display']['queued_info'] = [
      '#type' => 'item',
      'value' => [
        '#markup' => '
            <span class="title">Queued</span>
            <span class="value">' . $queued_count . '</span>
            <span class="suffix">terms</span>
        ',
      ],
    ];
  }
  $form['actions']['rebuild all'] = [
    '#identity' => 'btn_rebuild_all',
    '#value' => t('Rebuild all terms (in batch)'),
    '#type' => 'submit',
  ];
  $form['actions']['rebuild all voc'] = [
    '#identity' => 'btn_rebuild_all_voc',
    '#value' => t('Rebuild all terms in all vocabularies (in batch)'),
    '#type' => 'submit',
  ];
  $form['actions']['rebuild all queue'] = [
    '#identity' => 'btn_rebuild_all_queue',
    '#value' => t('Queue all items to rebuild'),
    '#type' => 'submit',
  ];
  $form['actions']['rebuild all voc queue'] = [
    '#identity' => 'btn_rebuild_all_voc_queue',
    '#value' => t('Queue all items to rebuild (for all vocabularies)'),
    '#type' => 'submit',
  ];
  if ($percentProcessed < 100) {
    $form['actions']['rebuild missing queue'] = [
      '#identity' => 'btn_rebuild_missing_queue',
      '#value' => t('Queue missing items'),
      '#type' => 'submit',
    ];
  }
  $form['actions']['rebuild missing all voc queue'] = [
    '#identity' => 'btn_rebuild_missing_all_voc_queue',
    '#value' => t('Queue missing items (for all vocabularies)'),
    '#type' => 'submit',
  ];
  $form['vid'] = [
    '#type' => 'value',
    '#value' => $vocabulary
      ->id(),
  ];
  return $form;
}