config.admin.inc 4.58 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

/**
 * @file
 * Admin page callbacks for the config module.
 */

use Drupal\Core\Config\StorageInterface;
9 10
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

/**
 * Helper function to construct the storage changes in a configuration synchronization form.
 *
 * @param array $form
 *   The form structure to add to. Passed by reference.
 * @param array $form_state
 *   The current state of the form. Passed by reference.
 * @param Drupal\Core\Config\StorageInterface $source_storage
 *   The source storage to retrieve differences from.
 * @param Drupal\Core\Config\StorageInterface $target_storage
 *   The target storage to compare differences to.
 */
function config_admin_sync_form(array &$form, array &$form_state, StorageInterface $source_storage, StorageInterface $target_storage) {
  $source_list = $source_storage->listAll();
  if (empty($source_list)) {
    $form['no_changes'] = array(
      '#markup' => t('There is no configuration to import.'),
    );
30
    $form['actions']['#access'] = FALSE;
31 32 33 34 35 36 37 38 39 40 41
    return $form;
  }

  $config_changes = config_sync_get_changes($source_storage, $target_storage);
  if (empty($config_changes)) {
    $form['no_changes'] = array(
      '#markup' => t('There are no configuration changes.'),
    );
    return $form;
  }

42 43 44
  // Add the AJAX library to the form for dialog support.
  $form['#attached']['library'][] = array('system', 'drupal.ajax');

45 46 47 48
  foreach ($config_changes as $config_change_type => $config_files) {
    if (empty($config_files)) {
      continue;
    }
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    // @todo A table caption would be more appropriate, but does not have the
    //   visual importance of a heading.
    $form[$config_change_type]['heading'] = array(
      '#theme' => 'html_tag__h3',
      '#tag' => 'h3',
    );
    switch ($config_change_type) {
      case 'create':
        $form[$config_change_type]['heading']['#value'] = format_plural(count($config_files), '@count new', '@count new');
        break;

      case 'change':
        $form[$config_change_type]['heading']['#value'] = format_plural(count($config_files), '@count changed', '@count changed');
        break;

      case 'delete':
        $form[$config_change_type]['heading']['#value'] = format_plural(count($config_files), '@count removed', '@count removed');
        break;
    }
    $form[$config_change_type]['list'] = array(
      '#theme' => 'table',
71
      '#header' => array('Name', 'Operations'),
72
    );
73

74
    foreach ($config_files as $config_file) {
75 76 77
      $links['view_diff'] = array(
        'title' => t('View differences'),
        'href' => 'admin/config/development/sync/diff/' . $config_file,
78 79
        'attributes' => array(
          'class' => array('use-ajax'),
80 81 82 83
          'data-accepts' => 'application/vnd.drupal-modal',
          'data-dialog-options' => json_encode(array(
            'width' => 700
          )),
84
        ),
85 86 87 88 89 90 91 92 93 94
      );
      $form[$config_change_type]['list']['#rows'][] = array(
        'name' => $config_file,
        'operations' => array(
          'data' => array(
            '#type' => 'operations',
            '#links' => $links,
          ),
        ),
      );
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    }
  }
}

/**
 * Form constructor for configuration import form.
 *
 * @see config_admin_import_form_submit()
 * @see config_import()
 */
function config_admin_import_form($form, &$form_state) {
  // Retrieve a list of differences between last known state and active store.
  $source_storage = drupal_container()->get('config.storage.staging');
  $target_storage = drupal_container()->get('config.storage');

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Import all'),
  );
115 116 117

  config_admin_sync_form($form, $form_state, $source_storage, $target_storage);

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  return $form;
}

/**
 * Form submission handler for config_admin_import_form().
 */
function config_admin_import_form_submit($form, &$form_state) {
  if (!lock()->lockMayBeAvailable(CONFIG_IMPORT_LOCK)) {
    drupal_set_message(t('Another request may be synchronizing configuration already.'));
  }
  else if (config_import()) {
    // Once a sync completes, we empty the staging directory. This prevents
    // changes from being accidentally overwritten by stray files getting
    // imported later.
    $source_storage = drupal_container()->get('config.storage.staging');
    foreach ($source_storage->listAll() as $name) {
      $source_storage->delete($name);
    }

    drupal_flush_all_caches();

    drupal_set_message(t('The configuration was imported successfully.'));
  }
  else {
    drupal_set_message(t('The import failed due to an error. Any errors have been logged.'), 'error');
  }
}