sites_group_overrides-1.x-dev/src/Form/RevertToDefaults.php

src/Form/RevertToDefaults.php
<?php

declare(strict_types=1);

namespace Drupal\sites_group_overrides\Form;

use Drupal\Core\Url;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\group\Entity\GroupRelationshipInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\sites_group_overrides\SitesGroupOverridesServiceInterface;

/**
 * Delete overrides (group_relationship) for the specified entity.
 */
final class RevertToDefaults extends ConfirmFormBase {

  protected ?array $relationships = NULL;

  /**
   * Constructs a RevertToDefaults object.
   *
   * @param \Drupal\sites_group_overrides\SitesGroupOverridesServiceInterface $sitesGroupOverridesService
   *   The sites group overrides service.
   */
  public function __construct(
    protected readonly SitesGroupOverridesServiceInterface $sitesGroupOverridesService,
  ) { }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('sites_group_overrides.service'),
    );
  }

  /**
   * The group relationship to revert.
   */
  protected GroupRelationshipInterface $groupRelationship;

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, ?GroupRelationshipInterface $group_relationship = NULL) {
    $this->groupRelationship = $group_relationship;
    $form = parent::buildForm($form, $form_state);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId(): string {
    return 'sites_group_overrides_revert_to_defaults';
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion(): TranslatableMarkup {
    return $this->t('Are you sure you want to delete all overrides including layout overrides for @title?', [
      '@title' => $this->groupRelationship->getEntity()->label(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this->t('Revert to defaults');
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl(): Url {
    return $this->groupRelationship->getEntity()->toUrl('edit-form');
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $this->sitesGroupOverridesService->revertOverride($this->groupRelationship);

    $this->messenger()->addStatus($this->t('@title was reverted to defaults.', [
      '@title' => $this->groupRelationship->getEntity()->label(),
    ]));
    $form_state->setRedirectUrl($this->groupRelationship->getEntity()->toUrl());
  }

}

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

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