TaxonomyEntityIndexAdminReindexForm.php in Taxonomy Entity Index 8
File
src/Form/TaxonomyEntityIndexAdminReindexForm.php
View source
<?php
namespace Drupal\taxonomy_entity_index\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TaxonomyEntityIndexAdminReindexForm extends FormBase {
protected $entityTypeManager;
protected $configFactory;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->configFactory = $config_factory;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('config.factory'));
}
public function getFormId() {
return 'taxonomy_entity_index_admin_reindex_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$entity_types = $this->entityTypeManager
->getDefinitions();
$entity_types_to_index = $this->configFactory
->get('taxonomy_entity_index.settings')
->get('types');
foreach ($entity_types as $entity_type_id => $entity_type) {
if (!is_null($entity_type
->getBaseTable()) && $entity_type
->entityClassImplements('\\Drupal\\Core\\Entity\\ContentEntityInterface') && in_array($entity_type_id, $entity_types_to_index)) {
$options[$entity_type_id] = $entity_type
->getLabel() . " <em>({$entity_type_id})</em>";
}
}
asort($options);
$form['description'] = [
'#markup' => $this
->t('<p>Use this form to reindex all terms for the selected entity types.</p>'),
];
$form['types'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Entity types'),
'#options' => $options,
'#description' => $this
->t('Re-index all terms for the selected entity types.'),
'#required' => TRUE,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Rebuild index'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$types = array_filter($form_state
->getValue([
'types',
]));
foreach ($types as $type) {
$operations[] = [
'taxonomy_entity_index_reindex_entity_type',
[
$type,
],
];
}
$batch = [
'operations' => $operations,
'finished' => 'taxonomy_entity_index_reindex_finished',
];
batch_set($batch);
}
}