You are here

class ContentHubDeleteClientConfirmForm in Acquia Content Hub 8.2

Class ContentHubDeleteClientConfirmForm.

Defines a confirmation form to confirm deletion of Acquia Content Hub Client.

@package Drupal\acquia_contenthub\Form

Hierarchy

Expanded class hierarchy of ContentHubDeleteClientConfirmForm

1 file declares its use of ContentHubDeleteClientConfirmForm
ClientDeleteConfirmForm.php in modules/acquia_contenthub_publisher/src/Form/Client/ClientDeleteConfirmForm.php
1 string reference to 'ContentHubDeleteClientConfirmForm'
acquia_contenthub.routing.yml in ./acquia_contenthub.routing.yml
acquia_contenthub.routing.yml

File

src/Form/ContentHubDeleteClientConfirmForm.php, line 24

Namespace

Drupal\acquia_contenthub\Form
View source
class ContentHubDeleteClientConfirmForm extends FormBase {
  use AcquiaContentHubUnregisterHelperTrait;

  /**
   * The Content Hub connection manager.
   *
   * @var \Drupal\acquia_contenthub\ContentHubConnectionManager
   */
  protected $chConnectionManager;

  /**
   * Event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * AcquiaContentHubUnregisterEvent event.
   *
   * @var \Drupal\acquia_contenthub\Event\AcquiaContentHubUnregisterEvent
   */
  protected $event;

  /**
   * The client factory.
   *
   * @var \Drupal\acquia_contenthub\Client\ClientFactory
   */
  protected $clientFactory;

  /**
   * ContentHubDeleteClientConfirmForm constructor.
   *
   * @param \Drupal\acquia_contenthub\ContentHubConnectionManager $ch_connection_manager
   *   The Content Hub connection manager service.
   * @param \Drupal\acquia_contenthub\Client\ClientFactory $client_factory
   *   The Client Factory.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
   *   Symfony event dispatcher.
   */
  public function __construct(ContentHubConnectionManager $ch_connection_manager, ClientFactory $client_factory, EventDispatcherInterface $eventDispatcher) {
    $this->chConnectionManager = $ch_connection_manager;
    $this->clientFactory = $client_factory;
    $this->eventDispatcher = $eventDispatcher;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {

    /** @var \Drupal\acquia_contenthub\ContentHubConnectionManager $ch_connection_manager */
    $ch_connection_manager = $container
      ->get('acquia_contenthub.connection_manager');

    /** @var \Drupal\acquia_contenthub\Client\ClientFactory $client_factory */
    $client_factory = $container
      ->get('acquia_contenthub.client.factory');

    /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher */
    $event_dispatcher = $container
      ->get('event_dispatcher');
    return new static($ch_connection_manager, $client_factory, $event_dispatcher);
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) : array {
    $webhook_uuid = $this
      ->getWebhookUuid();
    if (empty($webhook_uuid)) {
      $this
        ->messenger()
        ->addError($this
        ->t('Cannot find webhook uuid.'));
      return $form;
    }
    $this
      ->dispatchEvent($webhook_uuid);
    $orphaned_entities_amount = $this->event
      ->getOrphanedEntitiesAmount();
    if ($orphaned_entities_amount !== 0) {
      $form['delete_entities']['orphaned_entites'] = [
        '#type' => 'details',
        '#open' => TRUE,
        '#description' => $this
          ->t('There are @count entities published from this client: @client. You have to delete/reoriginate those entities before proceeding with the unregistration. @blank
          If you want to delete those entities and unregister the client, use the following drush command on the given client "drush ach-disconnect --delete=all".', [
          '@count' => $orphaned_entities_amount,
          '@client' => $this->event
            ->getClientName(),
          '@blank' => new FormattableMarkup('<br>', []),
        ]),
        '#title' => $this
          ->t('Un-register Acquia Content Hub'),
      ];
    }
    if ($this->event
      ->getOrphanedFilters()) {
      $form['delete_filters'] = [
        '#type' => 'details',
        '#title' => $this
          ->t('After un-registration the following filters will be deleted:'),
        '#open' => TRUE,
      ];
      $form['delete_filters']['orphaned_filters'] = [
        '#type' => 'table',
        '#title' => $this
          ->t('Orphaned filters'),
        '#header' => [
          'Filter name',
          'Filter UUID',
        ],
        '#rows' => $this
          ->formatOrphanedFiltersTable($this->event
          ->getOrphanedFilters()),
      ];
      if ($this
        ->checkDiscoveryRoute()) {
        $form['actions']['redirect'] = [
          '#type' => 'link',
          '#title' => $this
            ->t('Go to Discovery Interface'),
          '#url' => Url::fromRoute('acquia_contenthub_curation.discovery'),
          '#attributes' => [
            'class' => [
              'button',
            ],
          ],
        ];
      }
    }
    if (empty($this->event
      ->getOrphanedFilters()) && !$orphaned_entities_amount) {
      $form['safe_message'] = [
        '#markup' => $this
          ->t('Everything is in order, safe to proceed.'),
      ];
    }
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Unregister'),
      '#button_type' => 'primary',
      '#attributes' => [
        'disabled' => (bool) $orphaned_entities_amount,
      ],
    ];
    $form['settings'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Content Hub Settings'),
      '#button_type' => 'primary',
      '#name' => 'settings',
    ];
    $form['subscription'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Cancel'),
      '#button_type' => 'primary',
      '#name' => 'subscription',
    ];
    return $form;
  }

  /**
   * Dispatches AcquiaContentHubUnregisterEvent.
   *
   * @param string $webhook_uuid
   *   Webhook uuid.
   */
  public function dispatchEvent(string $webhook_uuid) {
    $this->event = new AcquiaContentHubUnregisterEvent($webhook_uuid);
    $this->eventDispatcher
      ->dispatch(AcquiaContentHubEvents::ACH_UNREGISTER, $this->event);
  }

  /**
   * Get webhook UUID.
   *
   * @return string
   *   Webhook uuid.
   *
   * @throws \Exception
   */
  public function getWebhookUuid() : string {
    $client = $this->clientFactory
      ->getClient();
    $settings = $client
      ->getSettings();
    $remote_settings = $client
      ->getRemoteSettings();
    foreach ($remote_settings['webhooks'] as $webhook) {
      if ($webhook['client_name'] === $settings
        ->getName()) {
        return $webhook['uuid'];
      }
    }
    return '';
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->getTriggeringElement()['#name'] === 'subscription') {
      $form_state
        ->setRedirect('acquia_contenthub.subscription_settings');
      return;
    }
    if ($form_state
      ->getTriggeringElement()['#name'] === 'settings') {
      $form_state
        ->setRedirect('acquia_contenthub.admin_settings');
      return;
    }
    $client = $this->clientFactory
      ->getClient();
    if (!$client) {
      $this
        ->messenger()
        ->addError("Couldn't instantiate client. Please check connection settings.");
      $form_state
        ->setRedirect('acquia_contenthub.admin_settings');
      return;
    }
    try {
      $this->chConnectionManager
        ->unregister($this->event);
    } catch (\Exception $e) {
      $this
        ->messenger()
        ->addError($this
        ->t('Error during unregistration: @error_message', [
        '@error_message' => $e
          ->getMessage(),
      ]));
      return;
    }
    $form_state
      ->setRedirect('acquia_contenthub.admin_settings');
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return "contenthub_delete_client_confirmation";
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AcquiaContentHubUnregisterHelperTrait::checkDiscoveryRoute public function Checks if Discovery Interface route exists.
AcquiaContentHubUnregisterHelperTrait::formatOrphanedFiltersTable protected function Format rows for render array.
ContentHubDeleteClientConfirmForm::$chConnectionManager protected property The Content Hub connection manager.
ContentHubDeleteClientConfirmForm::$clientFactory protected property The client factory.
ContentHubDeleteClientConfirmForm::$event protected property AcquiaContentHubUnregisterEvent event.
ContentHubDeleteClientConfirmForm::$eventDispatcher protected property Event dispatcher.
ContentHubDeleteClientConfirmForm::buildForm public function Form constructor. Overrides FormInterface::buildForm 1
ContentHubDeleteClientConfirmForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
ContentHubDeleteClientConfirmForm::dispatchEvent public function Dispatches AcquiaContentHubUnregisterEvent. 1
ContentHubDeleteClientConfirmForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 1
ContentHubDeleteClientConfirmForm::getWebhookUuid public function Get webhook UUID. 1
ContentHubDeleteClientConfirmForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
ContentHubDeleteClientConfirmForm::__construct public function ContentHubDeleteClientConfirmForm constructor.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.