You are here

class ConfigPartialExportForm in Config Partial Export 8

Construct the storage changes in a configuration synchronization form.

Hierarchy

Expanded class hierarchy of ConfigPartialExportForm

1 string reference to 'ConfigPartialExportForm'
config_partial_export.routing.yml in ./config_partial_export.routing.yml
config_partial_export.routing.yml

File

src/Form/ConfigPartialExportForm.php, line 19

Namespace

Drupal\config_partial_export\Form
View source
class ConfigPartialExportForm extends FormBase {

  /**
   * The active configuration object.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $activeStorage;

  /**
   * The snapshot configuration object.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $snapshotStorage;

  /**
   * The configuration manager.
   *
   * @var \Drupal\Core\Config\ConfigManagerInterface
   */
  protected $configManager;

  /**
   * The file system service.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * Constructs the object.
   *
   * @param \Drupal\Core\Config\StorageInterface $active_storage
   *   The target storage.
   * @param \Drupal\Core\Config\StorageInterface $snapshot_storage
   *   The snapshot storage.
   * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
   *   Configuration manager.
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system service.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state object of the current site instance.
   */
  public function __construct(StorageInterface $active_storage, StorageInterface $snapshot_storage, ConfigManagerInterface $config_manager, FileSystemInterface $file_system, StateInterface $state) {
    $this->activeStorage = $active_storage;
    $this->snapshotStorage = $snapshot_storage;
    $this->configManager = $config_manager;
    $this->fileSystem = $file_system;
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.storage'), $container
      ->get('config.storage.snapshot'), $container
      ->get('config.manager'), $container
      ->get('file_system'), $container
      ->get('state'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'config_partial_export_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $snapshot_comparer = new StorageComparer($this->activeStorage, $this->snapshotStorage, $this->configManager);
    $change_list = [];
    if ($snapshot_comparer
      ->createChangelist()
      ->hasChanges()) {
      $this
        ->messenger()
        ->addWarning($this
        ->t('Your current configuration has changed.'));
      foreach ($snapshot_comparer
        ->getAllCollectionNames() as $collection) {
        foreach ($snapshot_comparer
          ->getChangelist(NULL, $collection) as $config_names) {
          if (empty($config_names)) {
            continue;
          }
          foreach ($config_names as $config_name) {
            $change_list[$config_name]['name'] = $config_name;
          }
        }
      }
    }
    if (empty($change_list)) {
      $user_input = $form_state
        ->getUserInput();
      if (isset($user_input['change_list'])) {
        $change_list = $user_input['change_list'];
      }
    }
    ksort($change_list);
    $form['change_list'] = [
      '#type' => 'tableselect',
      '#header' => [
        'name' => $this
          ->t('Name'),
      ],
      '#options' => $change_list,
    ];
    $form['description'] = [
      '#markup' => '<p><b>' . $this
        ->t('Use the export button to download the selected files listed above.') . '</b></p>',
    ];
    $form['addSystemSiteInfo'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Add system.site info'),
    ];
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Export'),
    ];
    $last_selection = $this->state
      ->get('config_partial_export_form');
    $current_user_id = $this
      ->currentUser()
      ->id();
    if (!empty($last_selection[$current_user_id])) {
      $current_user_last_selection = $last_selection[$current_user_id];
      $form['change_list']['#default_value'] = $current_user_last_selection['status_checkboxes_all'];
      $form['addSystemSiteInfo']['#default_value'] = $current_user_last_selection['status_checkbox_system'];
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $user_input = $form_state
      ->getUserInput();
    $count = 0;
    if (!empty($user_input)) {
      foreach ($user_input['change_list'] as $change_item) {
        if ($change_item) {
          $count++;
        }
      }
    }
    if ((empty($user_input['change_list']) || !$count) && empty($user_input['addSystemSiteInfo'])) {
      $form_state
        ->setErrorByName('', $this
        ->t('No items selected.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $user_input = $form_state
      ->getUserInput();
    $change_list = $user_input['change_list'] ?: [];
    $add_system_site_info = $user_input['addSystemSiteInfo'];
    $change_list_booleans = [];
    foreach ($change_list as $key => $value) {
      if ($value) {
        $change_list_booleans[$key] = TRUE;
      }
    }
    $last_selection = $this->state
      ->get('config_partial_export_form');
    $last_selection[$this
      ->currentUser()
      ->id()] = [
      'status_checkboxes_all' => $change_list_booleans,
      'status_checkbox_system' => (bool) $add_system_site_info,
    ];
    $this->state
      ->set('config_partial_export_form', $last_selection);
    $this
      ->createArchive(array_filter($change_list), $add_system_site_info);
    $form_state
      ->setRedirect('config_partial.export_partial_download');
  }

  /**
   * Creates a tarball based on $change_list.
   *
   * Creates a tarball based on $change_list in the temporary directory
   * set on admin/config/media/file-system page.
   *
   * @param array $change_list
   *   Array of modified config files.
   * @param bool $add_system_site_info
   *   If TRUE the system.site.yml file will be added to change list.
   */
  public function createArchive(array $change_list, $add_system_site_info = FALSE) {
    $this->fileSystem
      ->delete($this->fileSystem
      ->getTempDirectory() . '/config_partial.tar.gz');
    $archiver = new ArchiveTar($this->fileSystem
      ->getTempDirectory() . '/config_partial.tar.gz', 'gz');

    // Get raw configuration data without overrides.
    if ($add_system_site_info && !in_array('system.site', $change_list)) {
      $change_list[] = 'system.site';
    }
    foreach ($change_list as $name) {
      $yaml = Yaml::encode($this->configManager
        ->getConfigFactory()
        ->get($name)
        ->getRawData());
      $archiver
        ->addString("{$name}.yml", $yaml);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigPartialExportForm::$activeStorage protected property The active configuration object.
ConfigPartialExportForm::$configManager protected property The configuration manager.
ConfigPartialExportForm::$fileSystem protected property The file system service.
ConfigPartialExportForm::$snapshotStorage protected property The snapshot configuration object.
ConfigPartialExportForm::$state protected property The state service.
ConfigPartialExportForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
ConfigPartialExportForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
ConfigPartialExportForm::createArchive public function Creates a tarball based on $change_list.
ConfigPartialExportForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ConfigPartialExportForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
ConfigPartialExportForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
ConfigPartialExportForm::__construct public function Constructs the object.
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.
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.