cloud-8.x-2.0-beta1/src/Form/CloudConfigDeleteMultipleForm.php

src/Form/CloudConfigDeleteMultipleForm.php
<?php

namespace Drupal\cloud\Form;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\cloud\Entity\CloudConfig;
use Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface;

/**
 * Provides an entities deletion confirmation form.
 */
class CloudConfigDeleteMultipleForm extends CloudConfigProcessMultipleForm {

  /**
   * Constructs a new AwsCloudProcessMultiple object.
   *
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
   *   The tempstore factory.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   * @param \Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface $cloud_config_plugin_manager
   *   The cloud service provider plugin manager (CloudConfigPluginManager).
   */
  public function __construct(AccountInterface $current_user,
                              EntityTypeManagerInterface $entity_type_manager,
                              PrivateTempStoreFactory $temp_store_factory,
                              MessengerInterface $messenger,
                              CloudConfigPluginManagerInterface $cloud_config_plugin_manager) {

    parent::__construct(
      $current_user,
      $entity_type_manager,
      $temp_store_factory,
      $messenger,
      $cloud_config_plugin_manager
    );
    $this->tempStore = $temp_store_factory->get('entity_delete_multiple_confirm');
  }

  /**
   * {@inheritdoc}
   */
  public function getBaseFormId() {

    return 'aws_cloud_process_multiple_confirm_form';
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {

    // Get entity type ID from the route because ::buildForm has not yet been
    // called.
    $entity_type_id = $this->getRouteMatch()->getParameter('entity_type_id');
    return $entity_type_id . '_delete_multiple_confirm_form';
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {

    return $this->t('Delete');
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {

    return $this->formatPlural(count($this->selection),
      'Are you sure you want to delete this @item?',
      'Are you sure you want to delete these @items?', [
        '@item' => $this->entityType->getSingularLabel(),
        '@items' => $this->entityType->getPluralLabel(),
      ]
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {

    $route = \Drupal::routeMatch();
    $cloud_context = $route->getParameter('cloud_context');
    return new Url(
      'entity.' . $this->entityTypeId . '.collection',
      ['cloud_context' => $cloud_context]
    );
  }

  /**
   * Process entity.
   *
   * @param \Drupal\cloud\Entity\CloudConfig $entity
   *   An entity object.
   */
  protected function processEntity(CloudConfig $entity) {

    $entity->delete();
  }

  /**
   * Process an entity and related AWS resource.
   *
   * @param \Drupal\cloud\Entity\CloudConfig $entity
   *   An entity object.
   *
   * @return bool
   *   Succeeded or failed.
   */
  protected function process(CloudConfig $entity) {

    $message = $this->t('The @type "@label" has been deleted.', [
      '@type'  => $entity->getEntityType()->getLabel(),
      '@label' => $entity->label(),
    ]);

    $this->processEntity($entity);
    $this->messenger->addMessage($message);
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    parent::submitForm($form, $form_state);

    // Clear block and menu cache.
    CloudConfig::updateCache();
  }

  /**
   * Returns the message to show the user after an item was processed.
   *
   * @param int $count
   *   Count of processed translations.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
   *   The item processed message.
   */
  protected function getProcessedMessage($count) {

    return $this->formatPlural($count, 'Deleted @count item.', 'Deleted @count items.');
  }

  /**
   * Returns the message to show the user when an item has not been processed.
   *
   * @param int $count
   *   Count of processed translations.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
   *   The item inaccessible message.
   */
  protected function getInaccessibleMessage($count) {

    return $this->formatPlural($count,
      "@count item has not been deleted because you do not have the necessary permissions.",
      "@count items have not been deleted because you do not have the necessary permissions."
    );
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc