"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.9/core/modules/config_translation/src/Form/ConfigTranslationDeleteForm.php" (18 Nov 2020, 4381 Bytes) of package /linux/www/drupal-8.9.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ConfigTranslationDeleteForm.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\config_translation\Form;
    4 
    5 use Drupal\config_translation\ConfigMapperManagerInterface;
    6 use Drupal\Core\Cache\Cache;
    7 use Drupal\Core\Extension\ModuleHandlerInterface;
    8 use Drupal\Core\Form\ConfirmFormBase;
    9 use Drupal\Core\Form\FormStateInterface;
   10 use Drupal\Core\Routing\RouteMatchInterface;
   11 use Drupal\Core\Url;
   12 use Drupal\language\ConfigurableLanguageManagerInterface;
   13 use Symfony\Component\DependencyInjection\ContainerInterface;
   14 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
   15 
   16 /**
   17  * Builds a form to delete configuration translation.
   18  *
   19  * @internal
   20  */
   21 class ConfigTranslationDeleteForm extends ConfirmFormBase {
   22 
   23   /**
   24    * The language manager.
   25    *
   26    * @var \Drupal\language\ConfigurableLanguageManagerInterface
   27    */
   28   protected $languageManager;
   29 
   30   /**
   31    * The configuration mapper manager.
   32    *
   33    * @var \Drupal\config_translation\ConfigMapperManagerInterface
   34    */
   35   protected $configMapperManager;
   36 
   37   /**
   38    * The module handler.
   39    *
   40    * @var \Drupal\Core\Extension\ModuleHandlerInterface
   41    */
   42   protected $moduleHandler;
   43 
   44   /**
   45    * The configuration translation to be deleted.
   46    *
   47    * @var \Drupal\config_translation\ConfigMapperInterface
   48    */
   49   protected $mapper;
   50 
   51   /**
   52    * The language of configuration translation.
   53    *
   54    * @var \Drupal\Core\Language\LanguageInterface
   55    */
   56   protected $language;
   57 
   58   /**
   59    * Constructs a ConfigTranslationDeleteForm.
   60    *
   61    * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
   62    *   The language override configuration storage.
   63    * @param \Drupal\config_translation\ConfigMapperManagerInterface $config_mapper_manager
   64    *   The configuration mapper manager.
   65    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   66    *   The module handler.
   67    */
   68   public function __construct(ConfigurableLanguageManagerInterface $language_manager, ConfigMapperManagerInterface $config_mapper_manager, ModuleHandlerInterface $module_handler) {
   69     $this->languageManager = $language_manager;
   70     $this->configMapperManager = $config_mapper_manager;
   71     $this->moduleHandler = $module_handler;
   72   }
   73 
   74   /**
   75    * {@inheritdoc}
   76    */
   77   public static function create(ContainerInterface $container) {
   78     return new static(
   79       $container->get('language_manager'),
   80       $container->get('plugin.manager.config_translation.mapper'),
   81       $container->get('module_handler')
   82     );
   83   }
   84 
   85   /**
   86    * {@inheritdoc}
   87    */
   88   public function getQuestion() {
   89     return $this->t('Are you sure you want to delete the @language translation of %label?', ['%label' => $this->mapper->getTitle(), '@language' => $this->language->getName()]);
   90   }
   91 
   92   /**
   93    * {@inheritdoc}
   94    */
   95   public function getConfirmText() {
   96     return $this->t('Delete');
   97   }
   98 
   99   /**
  100    * {@inheritdoc}
  101    */
  102   public function getCancelUrl() {
  103     return new Url($this->mapper->getOverviewRouteName(), $this->mapper->getOverviewRouteParameters());
  104   }
  105 
  106   /**
  107    * {@inheritdoc}
  108    */
  109   public function getFormId() {
  110     return 'config_translation_delete_form';
  111   }
  112 
  113   /**
  114    * {@inheritdoc}
  115    */
  116   public function buildForm(array $form, FormStateInterface $form_state, RouteMatchInterface $route_match = NULL, $plugin_id = NULL, $langcode = NULL) {
  117     /** @var \Drupal\config_translation\ConfigMapperInterface $mapper */
  118     $mapper = $this->configMapperManager->createInstance($plugin_id);
  119     $mapper->populateFromRouteMatch($route_match);
  120 
  121     $language = $this->languageManager->getLanguage($langcode);
  122     if (!$language) {
  123       throw new NotFoundHttpException();
  124     }
  125 
  126     $this->mapper = $mapper;
  127     $this->language = $language;
  128     return parent::buildForm($form, $form_state);
  129   }
  130 
  131   /**
  132    * {@inheritdoc}
  133    */
  134   public function submitForm(array &$form, FormStateInterface $form_state) {
  135     foreach ($this->mapper->getConfigNames() as $name) {
  136       $this->languageManager->getLanguageConfigOverride($this->language->getId(), $name)->delete();
  137     }
  138 
  139     // Flush all persistent caches.
  140     $this->moduleHandler->invokeAll('cache_flush');
  141     foreach (Cache::getBins() as $cache_backend) {
  142       $cache_backend->deleteAll();
  143     }
  144 
  145     $this->messenger()->addStatus($this->t('@language translation of %label was deleted', ['%label' => $this->mapper->getTitle(), '@language' => $this->language->getName()]));
  146 
  147     $form_state->setRedirectUrl($this->getCancelUrl());
  148   }
  149 
  150 }