diff --git a/config_inspector.routing.yml b/config_inspector.routing.yml index 96d1ed6..377090a 100644 --- a/config_inspector.routing.yml +++ b/config_inspector.routing.yml @@ -5,6 +5,12 @@ config_inspector.overview: _title: 'Configuration inspector' requirements: _permission: 'inspect configuration' +config_inspector.download: + path: 'admin/config/development/configuration/inspect/{name}/download' + defaults: + _controller: '\Drupal\config_inspector\Controller\ConfigInspectorController::download' + requirements: + _permission: 'inspect configuration' config_inspector.list_page: path: '/admin/reports/config-inspector/{name}/list' defaults: diff --git a/src/ConfigInspectorManager.php b/src/ConfigInspectorManager.php index 364c75a..75f261f 100644 --- a/src/ConfigInspectorManager.php +++ b/src/ConfigInspectorManager.php @@ -6,6 +6,7 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\TypedConfigManagerInterface; use Drupal\Core\Config\Schema\Element; use Drupal\Core\Config\Schema\SchemaCheckTrait; +use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\DefaultPluginManager; /** @@ -32,7 +33,15 @@ class ConfigInspectorManager extends DefaultPluginManager { /** * {@inheritdoc} */ - public function __construct(ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typed_config_manager) { + public function __construct(ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typed_config_manager, $subdir, \Traversable $namespaces, ModuleHandlerInterface $module_handler, $plugin_interface = NULL, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin', array $additional_annotation_namespaces = []) { + parent::__construct($subdir, $namespaces, $module_handler, $plugin_interface, $plugin_definition_annotation_name, $additional_annotation_namespaces); + $this->subdir = $subdir; + $this->namespaces = $namespaces; + $this->pluginDefinitionAnnotationName = $plugin_definition_annotation_name; + $this->pluginInterface = $plugin_interface; + $this->moduleHandler = $module_handler; + $this->additionalAnnotationNamespaces = $additional_annotation_namespaces; + $this->configFactory = $config_factory; $this->typedConfigManager = $typed_config_manager; } @@ -122,6 +131,10 @@ class ConfigInspectorManager extends DefaultPluginManager { * @param string $config_name * Configuration name. * + * @return array|bool + * FALSE if no schema found. List of errors if any found. TRUE if fully + * valid. + * * @throws \Drupal\Core\Config\Schema\SchemaIncompleteException */ public function checkValues($config_name) { diff --git a/src/Controller/ConfigInspectorController.php b/src/Controller/ConfigInspectorController.php index 0fbf1ec..e4aa643 100644 --- a/src/Controller/ConfigInspectorController.php +++ b/src/Controller/ConfigInspectorController.php @@ -10,9 +10,12 @@ use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Link; +use Drupal\Core\Url; +use Drupal\Core\Serialization\Yaml; use Drupal\Core\StringTranslation\TranslationManager; use Drupal\Core\TypedData\TypedDataInterface; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\BinaryFileResponse; /** * Defines a controller for the config_inspector module. @@ -125,6 +128,7 @@ class ConfigInspectorController extends ControllerBase { 'tree' => $this->t('Tree'), 'form' => $this->t('Form'), 'raw' => $this->t('Raw data'), + 'download' => $this->t('Download'), ], '#attributes' => [ 'class' => [ @@ -148,7 +152,11 @@ class ConfigInspectorController extends ControllerBase { 'list' => ['#markup' => $this->t('N/A')], 'tree' => ['#markup' => $this->t('N/A')], 'form' => ['#markup' => $this->t('N/A')], - 'raw' => Link::createFromRoute($this->t('Raw data'), 'config_inspector.raw_page', ['name' => $name])->toRenderable(), + 'raw' => Link::createFromRoute($this->t('Raw data'), 'config_inspector.raw_page', ['name' => $name]) + ->toRenderable(), + 'download' => [ + '#markup' => Link::createFromRoute($this->t('Download'), new Url('config_inspector.download', ['name' => $name])) + ], ]; } else { @@ -167,10 +175,12 @@ class ConfigInspectorController extends ControllerBase { 'data-has-errors' => is_array($result), ], ], - 'list' => Link::createFromRoute($this->t('List'), 'config_inspector.list_page', ['name' => $name])->toRenderable(), - 'tree' => Link::createFromRoute($this->t('Tree'), 'config_inspector.tree_page', ['name' => $name])->toRenderable(), - 'form' => Link::createFromRoute($this->t('Form'), 'config_inspector.form_page', ['name' => $name])->toRenderable(), - 'raw' => Link::createFromRoute($this->t('Raw data'), 'config_inspector.raw_page', ['name' => $name])->toRenderable(), + 'list' => Link::createFromRoute($this->t('List'), 'config_inspector.list_page', ['name' => $name])->toRenderable(), + 'tree' => Link::createFromRoute($this->t('Tree'), 'config_inspector.tree_page', ['name' => $name])->toRenderable(), + 'form' => Link::createFromRoute($this->t('Form'), 'config_inspector.form_page', ['name' => $name])->toRenderable(), + 'raw' => Link::createFromRoute($this->t('Raw data'), 'config_inspector.raw_page', ['name' => $name])->toRenderable(), + 'download' => [ + '#markup' => Link::createFromRoute($this->t('Download'), new Url('config_inspector.download', ['name' => $name]))], ]; } } @@ -220,7 +230,7 @@ class ConfigInspectorController extends ControllerBase { */ public function getForm($name) { $config_schema = $this->configInspectorManager->getConfigSchema($name); - $output = $this->formBuilder->getForm('\Drupal\config_inspector\Form\ConfigInspectorItemForm', $config_schema); + $output = $this->formBuilder->getForm($config_schema); $output['#title'] = $this->t('Raw configuration data for %name', ['%name' => $name]); return $output; } @@ -256,6 +266,24 @@ class ConfigInspectorController extends ControllerBase { return $output; } + /** + * Download raw data. + * + * @param string $name + * Configuration name. + * + * @return \Symfony\Component\HttpFoundation\BinaryFileResponse + */ + public function download($name) { + $data = $this->configInspectorManager->getConfigData($name); + $data = Yaml::encode($data); + $headers = [ + 'Content-disposition' => 'attachment; filename="' . $name . '.yml"', + ]; + + return new BinaryFileResponse($data, 200, $headers); + } + /** * Format config schema as list table. * diff --git a/src/Form/ConfigInspectorItemForm.php b/src/Form/ConfigInspectorItemForm.php index aac5fbb..e633807 100644 --- a/src/Form/ConfigInspectorItemForm.php +++ b/src/Form/ConfigInspectorItemForm.php @@ -40,7 +40,7 @@ class ConfigInspectorItemForm extends FormBase { $build = []; foreach ($schema as $key => $element) { $definition = $element->getDataDefinition(); - $label = $definition['label'] ?: t('N/A'); + $label = $definition['label'] ?: $this->t('N/A'); if ($element instanceof ArrayElement) { $build[$key] = [ '#type' => 'details',