You are here

public function PathautoAdminDelete::buildForm in Pathauto 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/PathautoAdminDelete.php, line 63

Class

PathautoAdminDelete
Alias mass delete form.

Namespace

Drupal\pathauto\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['delete'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Choose aliases to delete'),
    '#tree' => TRUE,
  ];

  // First we do the "all" case.
  $total_count = $this->aliasStorageHelper
    ->countAll();
  $form['delete']['all_aliases'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('All aliases'),
    '#default_value' => FALSE,
    '#description' => $this
      ->t('Delete all aliases. Number of aliases which will be deleted: %count.', [
      '%count' => $total_count,
    ]),
  ];

  // Next, iterate over all visible alias types.
  $definitions = $this->aliasTypeManager
    ->getVisibleDefinitions();
  foreach ($definitions as $id => $definition) {

    /** @var \Drupal\pathauto\AliasTypeInterface $alias_type */
    $alias_type = $this->aliasTypeManager
      ->createInstance($id);
    $count = $this->aliasStorageHelper
      ->countBySourcePrefix($alias_type
      ->getSourcePrefix());
    $form['delete']['plugins'][$id] = [
      '#type' => 'checkbox',
      '#title' => (string) $definition['label'],
      '#default_value' => FALSE,
      '#description' => $this
        ->t('Delete aliases for all @label. Number of aliases which will be deleted: %count.', [
        '@label' => (string) $definition['label'],
        '%count' => $count,
      ]),
    ];
  }
  $form['options'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Delete options'),
    '#tree' => TRUE,
  ];

  // Provide checkbox for not deleting custom aliases.
  $form['options']['keep_custom_aliases'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Only delete automatically generated aliases'),
    '#default_value' => TRUE,
    '#description' => $this
      ->t('When checked, aliases which have been manually set are not affected by this mass-deletion.'),
  ];

  // Warn them and give a button that shows we mean business.
  $form['warning'] = [
    '#markup' => '<p>' . $this
      ->t('<strong>Note:</strong> there is no confirmation. Be sure of your action before clicking the "Delete aliases now!" button.<br />You may want to make a backup of the database and/or the path_alias and path_alias_revision tables prior to using this feature.') . '</p>',
  ];
  $form['buttons']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Delete aliases now!'),
  ];
  return $form;
}