From 92d8df0276d5a557d387c476e2f9f02f3c7a7927 Mon Sep 17 00:00:00 2001 From: miroslav-lee Date: Sat, 14 Mar 2020 17:06:37 +0100 Subject: [PATCH] Add GUI for the Export command --- README.md | 16 +-- default_content_deploy.links.menu.yml | 6 ++ default_content_deploy.links.task.yml | 5 + default_content_deploy.routing.yml | 10 ++ src/Form/ExportForm.php | 137 ++++++++++++++++++++++++++ 5 files changed, 168 insertions(+), 6 deletions(-) create mode 100644 src/Form/ExportForm.php diff --git a/README.md b/README.md index bb3a222..963e846 100644 --- a/README.md +++ b/README.md @@ -243,12 +243,6 @@ will be reverted to the state defined in a content directory. drush dcdi --force-override drush dcdi --force-override --folder='../content' -### Run import process form UI - -If you can not use Drush, go to page `/admin/config/development/dcd/import`. -Check "Import content" permission. - - ## drush default-content-deploy:uuid-info, drush dcd-uuid-info Display UUID value of Entity. @@ -266,6 +260,16 @@ Displays all current content entity types. drush dcd-entity-list +# GUI + +## Settings +Go to the `/admin/config/development/dcd` page. + +## Export +Go to the `/admin/config/development/dcd/export` page. + +## Import +Go to the `/admin/config/development/dcd/import` page. # Team workflow - how to synchronize configuration and content between sites diff --git a/default_content_deploy.links.menu.yml b/default_content_deploy.links.menu.yml index 3e77178..56087ee 100644 --- a/default_content_deploy.links.menu.yml +++ b/default_content_deploy.links.menu.yml @@ -9,3 +9,9 @@ default_content_deploy.import: description: 'Perform content import (drush dcdi) via UI.' parent: default_content_deploy.settings route_name: default_content_deploy.import + +default_content_deploy.export: + title: 'Export' + description: 'Perform content export (drush dcde,dcder,dcdes) via UI.' + parent: default_content_deploy.settings + route_name: default_content_deploy.export diff --git a/default_content_deploy.links.task.yml b/default_content_deploy.links.task.yml index 9471c82..b9a3b8e 100644 --- a/default_content_deploy.links.task.yml +++ b/default_content_deploy.links.task.yml @@ -7,3 +7,8 @@ default_content_deploy.import: base_route: default_content_deploy.settings route_name: default_content_deploy.import title: 'Import' + +default_content_deploy.export: + base_route: default_content_deploy.settings + route_name: default_content_deploy.export + title: 'Export' diff --git a/default_content_deploy.routing.yml b/default_content_deploy.routing.yml index bcb78a4..ee600d7 100644 --- a/default_content_deploy.routing.yml +++ b/default_content_deploy.routing.yml @@ -17,3 +17,13 @@ default_content_deploy.import: _permission: 'default content deploy import' options: _admin_route: TRUE + +default_content_deploy.export: + path: '/admin/config/development/dcd/export' + defaults: + _form: 'Drupal\default_content_deploy\Form\ExportForm' + _title: 'Default Content Deploy - Export' + requirements: + _permission: 'default content deploy export' + options: + _admin_route: TRUE diff --git a/src/Form/ExportForm.php b/src/Form/ExportForm.php new file mode 100644 index 0000000..606982b --- /dev/null +++ b/src/Form/ExportForm.php @@ -0,0 +1,137 @@ +exporter = $exporter; + $this->messenger = $messenger; + $this->deployManager = $deploy_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('default_content_deploy.exporter'), + $container->get('messenger'), + $container->get('default_content_deploy.manager') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'dcd_export_form'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $form['mode'] = [ + '#type' => 'select', + '#title' => $this->t('Mode'), + '#description' => $this->t('Mode of the export (export all content, with reference etc). Read more in the documentation.'), + '#required' => TRUE, + '#options' => [ + 'all' => $this->t('All content'), + ], + '#default_value' => 'all', + ]; + + $form['folder'] = [ + '#type' => 'textfield', + '#title' => $this->t('Folder'), + '#description' => $this->t('All existing content will be exported form this folder.'), + '#default_value' => $this->deployManager->getContentFolder(), + ]; + + $form['force_update'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Force update'), + '#description' => $this->t('All existing content will be overridden (locally updated default content will be reverted to the state defined on the site).'), + '#default_value' => FALSE, + ]; + + $form['export'] = [ + '#type' => 'submit', + '#value' => $this->t('Export content'), + ]; + + return $form; + } + + /** + * Form submission handler. + * + * @param array $form + * An associative array containing the structure of the form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * + * @throws \Exception + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $mode = $form_state->getValue('mode'); + $folder = $form_state->getValue('folder'); + $force_update = $form_state->getValue('force_update'); + + $this->exporter->setFolder($folder); + $this->exporter->setMode($mode); + $this->exporter->setForceUpdate($force_update); + $this->exporter->export(); + $this->addResultMessage(); + } + + /** + * Add a message with exporting results. + */ + private function addResultMessage() { + $result = $this->exporter->getResult(); + + foreach ($result as $entity_type => $value) { + $this->messenger->addMessage($this->t('Exported @count entities of the "@entity_type" entity type.', [ + '@count' => count($value), + '@entity_type' => $entity_type, MB_CASE_TITLE, + ])); + } + + return $this; + } + +} -- GitLab