class ConfigSingleExportForm

Same name and namespace in other branches
  1. 11.x core/modules/config/src/Form/ConfigSingleExportForm.php \Drupal\config\Form\ConfigSingleExportForm
  2. 10 core/modules/config/src/Form/ConfigSingleExportForm.php \Drupal\config\Form\ConfigSingleExportForm
  3. 9 core/modules/config/src/Form/ConfigSingleExportForm.php \Drupal\config\Form\ConfigSingleExportForm
  4. 8.9.x core/modules/config/src/Form/ConfigSingleExportForm.php \Drupal\config\Form\ConfigSingleExportForm

Provides a form for exporting a single configuration file.

@internal

Hierarchy

Expanded class hierarchy of ConfigSingleExportForm

1 string reference to 'ConfigSingleExportForm'
config.routing.yml in core/modules/config/config.routing.yml
core/modules/config/config.routing.yml

File

core/modules/config/src/Form/ConfigSingleExportForm.php, line 22

Namespace

Drupal\config\Form
View source
class ConfigSingleExportForm extends FormBase {
  
  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;
  
  /**
   * The config storage.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $configStorage;
  
  /**
   * Tracks the valid config entity type definitions.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface[]
   */
  protected $definitions = [];
  
  /**
   * Constructs a new ConfigSingleImportForm.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Config\StorageInterface $config_storage
   *   The config storage.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, StorageInterface $config_storage) {
    $this->entityTypeManager = $entity_type_manager;
    $this->configStorage = $config_storage;
  }
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container->get('entity_type.manager'), $container->get('config.storage'));
  }
  
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'config_single_export_form';
  }
  
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, string $config_type = '', string $config_name = '') {
    foreach ($this->entityTypeManager
      ->getDefinitions() as $entity_type => $definition) {
      if ($definition->entityClassImplements(ConfigEntityInterface::class)) {
        $this->definitions[$entity_type] = $definition;
      }
    }
    $entity_types = array_map(function (EntityTypeInterface $definition) {
      return $definition->getLabel();
    }, $this->definitions);
    // Sort the entity types by label, then add the simple config to the top.
    uasort($entity_types, 'strnatcasecmp');
    $config_types = [
      'system.simple' => $this->t('Simple configuration'),
    ] + $entity_types;
    $form['config_type'] = [
      '#title' => $this->t('Configuration type'),
      '#type' => 'select',
      '#options' => $config_types,
      '#default_value' => $config_type,
    ];
    // The config_name element depends on the value of config_type.
    // Select and replace the wrapper element of the <select> tag
    $form_url = Url::fromRoute('config.export_single', [
      'config_type' => $config_type,
      'config_name' => $config_name,
    ]);
    (new Htmx())->post($form_url)
      ->onlyMainContent()
      ->select('*:has(>select[name="config_name"])')
      ->target('*:has(>select[name="config_name"])')
      ->swap('outerHTML')
      ->applyTo($form['config_type']);
    $default_type = $form_state->getValue('config_type', $config_type);
    $form['config_name'] = [
      '#title' => $this->t('Configuration name'),
      '#type' => 'select',
      '#options' => $this->findConfiguration($default_type),
      '#empty_option' => $this->t('- Select -'),
      '#default_value' => $config_name,
    ];
    // The export element depends on the value of config_type and config_name.
    // Select and replace the wrapper element of the export textarea.
    (new Htmx())->post($form_url)
      ->onlyMainContent()
      ->select('[data-export-wrapper]')
      ->target('[data-export-wrapper]')
      ->swap('outerHTML')
      ->applyTo($form['config_name']);
    $form['export'] = [
      '#title' => $this->t('Here is your configuration:'),
      '#type' => 'textarea',
      '#rows' => 24,
      '#wrapper_attributes' => [
        'data-export-wrapper' => TRUE,
      ],
    ];
    $pushUrl = FALSE;
    $trigger = $this->getHtmxTriggerName();
    if ($trigger == 'config_type') {
      $form = $this->updateConfigurationType($form, $form_state);
      // Also update the empty export element "out of band".
      (new Htmx())->swapOob('outerHTML:[data-export-wrapper]')
        ->applyTo($form['export'], '#wrapper_attributes');
      $pushUrl = Url::fromRoute('config.export_single', [
        'config_type' => $default_type,
        'config_name' => '',
      ]);
    }
    elseif ($trigger == 'config_name') {
      // A name is selected.
      $default_name = $form_state->getValue('config_name', $config_name);
      $form['export'] = $this->updateExport($form, $default_type, $default_name);
      // Update the url in the browser location bar.
      $pushUrl = Url::fromRoute('config.export_single', [
        'config_type' => $default_type,
        'config_name' => $default_name,
      ]);
    }
    elseif ($config_type && $config_name) {
      // We started with values, update the export using those.
      $form['export'] = $this->updateExport($form, $config_type, $config_name);
    }
    if ($pushUrl) {
      (new Htmx())->pushUrlHeader($pushUrl)
        ->applyTo($form);
    }
    return $form;
  }
  
  /**
   * Handles switching the configuration type selector.
   */
  public function updateConfigurationType($form, FormStateInterface $form_state) {
    $form['config_name']['#options'] = $this->findConfiguration($form_state->getValue('config_type'));
    $form['export']['#value'] = NULL;
    return $form;
  }
  
  /**
   * Handles switching the export textarea.
   */
  public function updateExport($form, string $config_type, string $config_name) {
    // Determine the full config name for the selected config entity.
    // Calling this in the main form build requires accounting for not yet
    // having input.
    if (!empty($config_type) && $config_type !== 'system.simple') {
      $definition = $this->entityTypeManager
        ->getDefinition($config_type);
      $name = $definition->getConfigPrefix() . '.' . $config_name;
    }
    else {
      $name = $config_name;
    }
    // Read the raw data for this config name, encode it, and display it.
    $exists = $this->configStorage
      ->exists($name);
    $form['export']['#value'] = !$exists ? NULL : Yaml::encode($this->configStorage
      ->read($name));
    $form['export']['#description'] = !$exists ? NULL : $this->t('Filename: %name', [
      '%name' => $name . '.yml',
    ]);
    return $form['export'];
  }
  
  /**
   * Handles switching the configuration type selector.
   */
  protected function findConfiguration($config_type) {
    $names = [];
    // For a given entity type, load all entities.
    if ($config_type && $config_type !== 'system.simple') {
      $entity_storage = $this->entityTypeManager
        ->getStorage($config_type);
      foreach ($entity_storage->loadMultiple() as $entity) {
        $entity_id = $entity->id();
        if ($label = $entity->label()) {
          $names[$entity_id] = new TranslatableMarkup('@id (@label)', [
            '@label' => $label,
            '@id' => $entity_id,
          ]);
        }
        else {
          $names[$entity_id] = $entity_id;
        }
      }
    }
    else {
      // Gather the config entity prefixes.
      $config_prefixes = array_map(function (EntityTypeInterface $definition) {
        return $definition->getConfigPrefix() . '.';
      }, $this->definitions);
      // Find all config, and then filter our anything matching a config prefix.
      $names += $this->configStorage
        ->listAll();
      $names = array_combine($names, $names);
      foreach ($names as $config_name) {
        foreach ($config_prefixes as $config_prefix) {
          if (str_starts_with($config_name, $config_prefix)) {
            unset($names[$config_name]);
          }
        }
      }
    }
    return $names;
  }
  
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Nothing to submit.
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ConfigSingleExportForm::$configStorage protected property The config storage.
ConfigSingleExportForm::$definitions protected property Tracks the valid config entity type definitions.
ConfigSingleExportForm::$entityTypeManager protected property The entity type manager.
ConfigSingleExportForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
ConfigSingleExportForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
ConfigSingleExportForm::findConfiguration protected function Handles switching the configuration type selector.
ConfigSingleExportForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ConfigSingleExportForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
ConfigSingleExportForm::updateConfigurationType public function Handles switching the configuration type selector.
ConfigSingleExportForm::updateExport public function Handles switching the export textarea.
ConfigSingleExportForm::__construct public function Constructs a new ConfigSingleImportForm.
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 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 2
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. 2
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user. 2
FormBase::getRequest protected function Gets the request object. Overrides HtmxRequestInfoTrait::getRequest
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.
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 54
HtmxRequestInfoTrait::getHtmxCurrentUrl protected function Retrieves the URL of the requesting page from an HTMX request header.
HtmxRequestInfoTrait::getHtmxPrompt protected function Retrieves the prompt from an HTMX request header.
HtmxRequestInfoTrait::getHtmxTarget protected function Retrieves the target identifier from an HTMX request header.
HtmxRequestInfoTrait::getHtmxTrigger protected function Retrieves the trigger identifier from an HTMX request header.
HtmxRequestInfoTrait::getHtmxTriggerName protected function Retrieves the trigger name from an HTMX request header.
HtmxRequestInfoTrait::isHtmxBoosted protected function Determines if the request is boosted by HTMX.
HtmxRequestInfoTrait::isHtmxHistoryRestoration protected function Determines if if the request is for history restoration.
HtmxRequestInfoTrait::isHtmxRequest protected function Determines if the request is sent by HTMX.
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. 25
MessengerTrait::messenger public function Gets the messenger. 25
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 2
RedirectDestinationTrait::getDestinationArray protected function Prepares a &#039;destination&#039; 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. 3
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. 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.