BulkEditForm.php in Views Bulk Edit 8.2
File
src/Form/BulkEditForm.php
View source
<?php
namespace Drupal\views_bulk_edit\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BulkEditForm extends ConfirmFormBase {
use BulkEditFormTrait;
protected $tempStore;
protected $count = 0;
protected $bundleInfo;
public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityRepositoryInterface $entityRepository, PrivateTempStoreFactory $temp_store_factory, TimeInterface $time, AccountInterface $currentUser, EntityTypeBundleInfoInterface $bundleInfo, EntityFieldManagerInterface $entityFieldManager) {
$this->entityTypeManager = $entityTypeManager;
$this->entityRepository = $entityRepository;
$this->tempStore = $temp_store_factory
->get('entity_edit_multiple');
$this->time = $time;
$this->currentUser = $currentUser;
$this->bundleInfo = $bundleInfo;
$this->entityFieldManager = $entityFieldManager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('entity.repository'), $container
->get('tempstore.private'), $container
->get('datetime.time'), $container
->get('current_user'), $container
->get('entity_type.bundle.info'), $container
->get('entity_field.manager'));
}
public function getFormId() {
return 'bulk_edit_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$bundle_data = [];
foreach ($this
->getBulkEditEntityData() as $entity_type_id => $bundle_entities) {
$bundle_info = $this->bundleInfo
->getBundleInfo($entity_type_id);
foreach ($bundle_entities as $bundle => $entities) {
$this->count += count($entities);
$bundle_data[$entity_type_id][$bundle] = $bundle_info[$bundle]['label'];
}
}
$form = $this
->buildBundleForms($form, $form_state, $bundle_data);
$form = parent::buildForm($form, $form_state);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->submitConfigurationForm($form, $form_state);
foreach ($this
->getBulkEditEntityData() as $entity_type_id => $bundle_entities) {
foreach ($bundle_entities as $bundle => $entities) {
$entities = $this->entityTypeManager
->getStorage($entity_type_id)
->loadMultiple(array_keys($entities));
foreach ($entities as $entity) {
$this
->execute($entity);
}
}
}
$this
->clearBulkEditEntityData();
}
public function getQuestion() {
return $this
->formatPlural($this->count, 'Are you sure you want to edit this entity?', 'Are you sure you want to edit these entities?');
}
public function getCancelUrl() {
return new Url('<front>');
}
protected function getBulkEditEntityData() {
return $this->tempStore
->get($this->currentUser
->id()) ?: [];
}
protected function clearBulkEditEntityData() {
$this->tempStore
->delete($this->currentUser
->id());
}
}